@mo36924/babel-plugin-graphql-tagged-template 1.5.0 → 1.5.21

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 mo36924
3
+ Copyright (c) 2022 mo36924
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.cjs CHANGED
@@ -1,40 +1,50 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('fs');
4
- var base52 = require('@mo36924/base52');
5
- var graphqlSchema = require('@mo36924/graphql-schema');
6
- var graphql = require('graphql');
3
+ const graphqlBuild = require('@mo36924/graphql-build');
4
+ const graphqlConfig = require('@mo36924/graphql-config');
5
+ const graphqlModel = require('@mo36924/graphql-model');
6
+ const graphql = require('graphql');
7
7
 
8
- var index = ({ types: t }, options) => {
9
- let schema;
10
- if (options.model) {
11
- const model = fs.readFileSync(options.model || "index.graphql", "utf8");
12
- schema = graphqlSchema.buildSchemaModel(model);
13
- }
14
- else if (typeof options.schema === "string") {
15
- const graphqlSchema$1 = fs.readFileSync(options.schema || "index.graphql", "utf8");
16
- schema = graphqlSchema.buildSchema(graphqlSchema$1);
17
- }
18
- else if (options.schema) {
19
- schema = options.schema;
20
- }
8
+ let _schema;
9
+ const index = ({ types: t }, options) => {
10
+ let schema = options.schema
11
+ ? typeof options.schema === "object"
12
+ ? options.schema
13
+ : graphql.buildSchema(options.schema)
14
+ : options.model
15
+ ? typeof options.model === "object"
16
+ ? graphqlBuild.buildASTSchema(options.model)
17
+ : graphqlModel.buildModel(options.model).schema
18
+ : (_schema ||= graphqlConfig.config().schema);
21
19
  return {
20
+ name: "graphql-tagged-template",
22
21
  visitor: {
23
- TaggedTemplateExpression(path) {
22
+ TaggedTemplateExpression(path, state) {
24
23
  const { tag, quasi: { quasis, expressions }, } = path.node;
25
24
  if (!t.isIdentifier(tag)) {
26
25
  return;
27
26
  }
28
27
  const name = tag.name;
29
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
30
- return;
28
+ let operation = "";
29
+ switch (name) {
30
+ case "gql":
31
+ case "query":
32
+ case "useQuery":
33
+ break;
34
+ case "mutation":
35
+ case "useMutation":
36
+ operation = "mutation";
37
+ break;
38
+ case "subscription":
39
+ case "useSubscription":
40
+ operation = "subscription";
41
+ break;
42
+ default:
43
+ return;
31
44
  }
32
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
45
+ let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);
33
46
  for (let i = 0; i < expressions.length; i++) {
34
- query += `$${base52.encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
35
- }
36
- if (name === "mutation" || name === "subscription") {
37
- query = name + query;
47
+ query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
38
48
  }
39
49
  let documentNode;
40
50
  try {
@@ -51,16 +61,16 @@ var index = ({ types: t }, options) => {
51
61
  },
52
62
  }));
53
63
  if (values.length) {
54
- const variables = `(${values.map((value, i) => `$${base52.encode(i)}:${value}`).join()})`;
55
- if (name === "query") {
56
- query = name + variables + query;
64
+ const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;
65
+ if (operation) {
66
+ query = operation + variables + query.slice(operation.length);
57
67
  }
58
- else if (name === "mutation" || name === "subscription") {
59
- query = name + variables + query.slice(name.length);
68
+ else if (name !== "gql") {
69
+ query = "query" + variables + query;
60
70
  }
61
71
  }
62
72
  try {
63
- documentNode = graphql.parse(query);
73
+ documentNode = graphql.parse(query, { noLocation: true });
64
74
  }
65
75
  catch (err) {
66
76
  throw path.buildCodeFrameError(String(err));
@@ -69,9 +79,19 @@ var index = ({ types: t }, options) => {
69
79
  if (errors.length) {
70
80
  throw path.buildCodeFrameError(errors[0].message);
71
81
  }
72
- const args = [t.stringLiteral(graphql.stripIgnoredCharacters(query))];
82
+ const args = [];
83
+ if (options.parse) {
84
+ const id = path.scope.generateUid("gql");
85
+ path.scope.getProgramParent().push({ kind: "let", id: t.identifier(id) });
86
+ args.push(t.assignmentExpression("||=", t.identifier(id), t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
87
+ t.stringLiteral(JSON.stringify(documentNode)),
88
+ ])));
89
+ }
90
+ else {
91
+ args.push(t.stringLiteral(graphql.stripIgnoredCharacters(query)));
92
+ }
73
93
  if (expressions.length) {
74
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(base52.encode(i)), expression))));
94
+ args.push(t.arrayExpression(expressions));
75
95
  }
76
96
  path.replaceWith(t.callExpression(t.identifier(name), args));
77
97
  },
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from \"fs\";\nimport type { default as babel, PluginObj, types as t } from \"@babel/core\";\nimport { encode } from \"@mo36924/base52\";\nimport { buildSchema, buildSchemaModel } from \"@mo36924/graphql-schema\";\nimport {\n DocumentNode,\n GraphQLInputType,\n GraphQLSchema,\n parse,\n stripIgnoredCharacters,\n TypeInfo,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n model: string;\n schema: string | GraphQLSchema;\n};\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema: GraphQLSchema;\n\n if (options.model) {\n const model = readFileSync(options.model || \"index.graphql\", \"utf8\");\n schema = buildSchemaModel(model);\n } else if (typeof options.schema === \"string\") {\n const graphqlSchema = readFileSync(options.schema || \"index.graphql\", \"utf8\");\n schema = buildSchema(graphqlSchema);\n } else if (options.schema) {\n schema = options.schema;\n }\n\n return {\n visitor: {\n TaggedTemplateExpression(path) {\n const {\n tag,\n quasi: { quasis, expressions },\n } = path.node;\n\n if (!t.isIdentifier(tag)) {\n return;\n }\n\n const name = tag.name;\n\n if (name !== \"gql\" && name !== \"query\" && name !== \"mutation\" && name !== \"subscription\") {\n return;\n }\n\n let query = quasis[0].value.cooked ?? quasis[0].value.raw;\n\n for (let i = 0; i < expressions.length; i++) {\n query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;\n }\n\n if (name === \"mutation\" || name === \"subscription\") {\n query = name + query;\n }\n\n let documentNode: DocumentNode;\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const values: GraphQLInputType[] = [];\n const typeInfo = new TypeInfo(schema);\n\n visit(\n documentNode,\n visitWithTypeInfo(typeInfo, {\n Variable() {\n values.push(typeInfo.getInputType()!);\n },\n }),\n );\n\n if (values.length) {\n const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;\n\n if (name === \"query\") {\n query = name + variables + query;\n } else if (name === \"mutation\" || name === \"subscription\") {\n query = name + variables + query.slice(name.length);\n }\n }\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const errors = validate(schema, documentNode);\n\n if (errors.length) {\n throw path.buildCodeFrameError(errors[0].message);\n }\n\n const args: t.Expression[] = [t.stringLiteral(stripIgnoredCharacters(query))];\n\n if (expressions.length) {\n args.push(\n t.objectExpression(\n expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression as any)),\n ),\n );\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), args));\n },\n },\n };\n};\n"],"names":["readFileSync","buildSchemaModel","graphqlSchema","buildSchema","encode","parse","TypeInfo","visit","visitWithTypeInfo","validate","stripIgnoredCharacters"],"mappings":";;;;;;;AAqBA,YAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB;IAC1D,IAAI,MAAqB,CAAC;IAE1B,IAAI,OAAO,CAAC,KAAK,EAAE;QACjB,MAAM,KAAK,GAAGA,eAAY,CAAC,OAAO,CAAC,KAAK,IAAI,eAAe,EAAE,MAAM,CAAC,CAAC;QACrE,MAAM,GAAGC,8BAAgB,CAAC,KAAK,CAAC,CAAC;KAClC;SAAM,IAAI,OAAO,OAAO,CAAC,MAAM,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MAAM,EAAE;oBACjB,MAAM,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;iBACnD;gBAED,MAAM,IAAI,GAAmB,CAAC,CAAC,CAAC,aAAa,CAACC,8BAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAE9E,IAAI,WAAW,CAAC,MAAM,EAAE;oBACtB,IAAI,CAAC,IAAI,CACP,CAAC,CAAC,gBAAgB,CAChB,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAACN,aAAM,CAAC,CAAC,CAAC,CAAC,EAAE,UAAiB,CAAC,CAAC,CACjG,CACF,CAAC;iBACH;gBAED,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;aAC9D;SACF;KACF,CAAC;AACJ,CAAC;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import babel, { PluginObj, types as t } from \"@babel/core\";\nimport { buildASTSchema } from \"@mo36924/graphql-build\";\nimport { config } from \"@mo36924/graphql-config\";\nimport { buildModel } from \"@mo36924/graphql-model\";\nimport {\n DocumentNode,\n GraphQLInputType,\n GraphQLSchema,\n TypeInfo,\n buildSchema,\n parse,\n stripIgnoredCharacters,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n model?: string | DocumentNode;\n schema?: string | GraphQLSchema;\n parse?: boolean;\n};\n\nlet _schema: GraphQLSchema;\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema = options.schema\n ? typeof options.schema === \"object\"\n ? options.schema\n : buildSchema(options.schema)\n : options.model\n ? typeof options.model === \"object\"\n ? buildASTSchema(options.model)\n : buildModel(options.model).schema\n : (_schema ||= config().schema);\n\n return {\n name: \"graphql-tagged-template\",\n visitor: {\n TaggedTemplateExpression(path, state) {\n const {\n tag,\n quasi: { quasis, expressions },\n } = path.node;\n\n if (!t.isIdentifier(tag)) {\n return;\n }\n\n const name = tag.name;\n let operation = \"\";\n\n switch (name) {\n case \"gql\":\n case \"query\":\n case \"useQuery\":\n break;\n case \"mutation\":\n case \"useMutation\":\n operation = \"mutation\";\n break;\n case \"subscription\":\n case \"useSubscription\":\n operation = \"subscription\";\n break;\n default:\n return;\n }\n\n let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);\n\n for (let i = 0; i < expressions.length; i++) {\n query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;\n }\n\n let documentNode: DocumentNode;\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const values: GraphQLInputType[] = [];\n const typeInfo = new TypeInfo(schema);\n\n visit(\n documentNode,\n visitWithTypeInfo(typeInfo, {\n Variable() {\n values.push(typeInfo.getInputType()!);\n },\n }),\n );\n\n if (values.length) {\n const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;\n\n if (operation) {\n query = operation + variables + query.slice(operation.length);\n } else if (name !== \"gql\") {\n query = \"query\" + variables + query;\n }\n }\n\n try {\n documentNode = parse(query, { noLocation: true });\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const errors = validate(schema, documentNode);\n\n if (errors.length) {\n throw path.buildCodeFrameError(errors[0].message);\n }\n\n const args: t.Expression[] = [];\n\n if (options.parse) {\n const id = path.scope.generateUid(\"gql\");\n path.scope.getProgramParent().push({ kind: \"let\", id: t.identifier(id) });\n\n args.push(\n t.assignmentExpression(\n \"||=\",\n t.identifier(id),\n t.callExpression(t.memberExpression(t.identifier(\"JSON\"), t.identifier(\"parse\")), [\n t.stringLiteral(JSON.stringify(documentNode)),\n ]),\n ),\n );\n } else {\n args.push(t.stringLiteral(stripIgnoredCharacters(query)));\n }\n\n if (expressions.length) {\n args.push(t.arrayExpression(expressions as t.Expression[]));\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), args));\n },\n },\n };\n};\n"],"names":["buildSchema","buildASTSchema","buildModel","config","parse","TypeInfo","visit","visitWithTypeInfo","validate","stripIgnoredCharacters"],"mappings":";;;;;;;AAuBA,IAAI,OAAsB,CAAC;AAE3B,cAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB,KAAe;AACzE,IAAA,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM;AACzB,UAAE,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ;cAChC,OAAO,CAAC,MAAM;AAChB,cAAEA,mBAAW,CAAC,OAAO,CAAC,MAAM,CAAC;UAC7B,OAAO,CAAC,KAAK;AACf,cAAE,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ;AACjC,kBAAEC,2BAAc,CAAC,OAAO,CAAC,KAAK,CAAC;kBAC7BC,uBAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM;eACjC,OAAO,KAAKC,oBAAM,EAAE,CAAC,MAAM,CAAC,CAAC;IAElC,OAAO;AACL,QAAA,IAAI,EAAE,yBAAyB;AAC/B,QAAA,OAAO,EAAE;YACP,wBAAwB,CAAC,IAAI,EAAE,KAAK,EAAA;AAClC,gBAAA,MAAM,EACJ,GAAG,EACH,KAAK,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,GAC/B,GAAG,IAAI,CAAC,IAAI,CAAC;AAEd,gBAAA,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE;oBACxB,OAAO;AACR,iBAAA;AAED,gBAAA,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;gBACtB,IAAI,SAAS,GAAG,EAAE,CAAC;AAEnB,gBAAA,QAAQ,IAAI;AACV,oBAAA,KAAK,KAAK,CAAC;AACX,oBAAA,KAAK,OAAO,CAAC;AACb,oBAAA,KAAK,UAAU;wBACb,MAAM;AACR,oBAAA,KAAK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U,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AAE1E,oBAAA,IAAI,CAAC,IAAI,CACP,CAAC,CAAC,oBAAoB,CACpB,KAAK,EACL,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC,EAChB,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,EAAE;wBAChF,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;qBAC9C,CAAC,CACH,CACF,CAAC;AACH,iBAAA;AAAM,qBAAA;AACL,oBAAA,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAACC,8BAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAC3D,iBAAA;gBAED,IAAI,WAAW,CAAC,MAAM,EAAE;oBACtB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,eAAe,CAAC,WAA6B,CAAC,CAAC,CAAC;AAC7D,iBAAA;AAED,gBAAA,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;aAC9D;AACF,SAAA;KACF,CAAC;AACJ,CAAC;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import babel, { PluginObj } from '@babel/core';
2
- import { GraphQLSchema } from 'graphql';
2
+ import { DocumentNode, GraphQLSchema } from 'graphql';
3
3
 
4
4
  declare type Options = {
5
- model: string;
6
- schema: string | GraphQLSchema;
5
+ model?: string | DocumentNode;
6
+ schema?: string | GraphQLSchema;
7
+ parse?: boolean;
7
8
  };
8
9
  declare const _default: ({ types: t }: typeof babel, options: Options) => PluginObj;
9
10
 
package/dist/index.js CHANGED
@@ -1,38 +1,48 @@
1
- import { readFileSync } from 'fs';
2
- import { encode } from '@mo36924/base52';
3
- import { buildSchemaModel, buildSchema } from '@mo36924/graphql-schema';
4
- import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
1
+ import { buildASTSchema } from '@mo36924/graphql-build';
2
+ import { config } from '@mo36924/graphql-config';
3
+ import { buildModel } from '@mo36924/graphql-model';
4
+ import { buildSchema, parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
5
5
 
6
- var index = ({ types: t }, options) => {
7
- let schema;
8
- if (options.model) {
9
- const model = readFileSync(options.model || "index.graphql", "utf8");
10
- schema = buildSchemaModel(model);
11
- }
12
- else if (typeof options.schema === "string") {
13
- const graphqlSchema = readFileSync(options.schema || "index.graphql", "utf8");
14
- schema = buildSchema(graphqlSchema);
15
- }
16
- else if (options.schema) {
17
- schema = options.schema;
18
- }
6
+ let _schema;
7
+ const index = ({ types: t }, options) => {
8
+ let schema = options.schema
9
+ ? typeof options.schema === "object"
10
+ ? options.schema
11
+ : buildSchema(options.schema)
12
+ : options.model
13
+ ? typeof options.model === "object"
14
+ ? buildASTSchema(options.model)
15
+ : buildModel(options.model).schema
16
+ : (_schema ||= config().schema);
19
17
  return {
18
+ name: "graphql-tagged-template",
20
19
  visitor: {
21
- TaggedTemplateExpression(path) {
20
+ TaggedTemplateExpression(path, state) {
22
21
  const { tag, quasi: { quasis, expressions }, } = path.node;
23
22
  if (!t.isIdentifier(tag)) {
24
23
  return;
25
24
  }
26
25
  const name = tag.name;
27
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
28
- return;
26
+ let operation = "";
27
+ switch (name) {
28
+ case "gql":
29
+ case "query":
30
+ case "useQuery":
31
+ break;
32
+ case "mutation":
33
+ case "useMutation":
34
+ operation = "mutation";
35
+ break;
36
+ case "subscription":
37
+ case "useSubscription":
38
+ operation = "subscription";
39
+ break;
40
+ default:
41
+ return;
29
42
  }
30
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
43
+ let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);
31
44
  for (let i = 0; i < expressions.length; i++) {
32
- query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
33
- }
34
- if (name === "mutation" || name === "subscription") {
35
- query = name + query;
45
+ query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
36
46
  }
37
47
  let documentNode;
38
48
  try {
@@ -49,16 +59,16 @@ var index = ({ types: t }, options) => {
49
59
  },
50
60
  }));
51
61
  if (values.length) {
52
- const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
53
- if (name === "query") {
54
- query = name + variables + query;
62
+ const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;
63
+ if (operation) {
64
+ query = operation + variables + query.slice(operation.length);
55
65
  }
56
- else if (name === "mutation" || name === "subscription") {
57
- query = name + variables + query.slice(name.length);
66
+ else if (name !== "gql") {
67
+ query = "query" + variables + query;
58
68
  }
59
69
  }
60
70
  try {
61
- documentNode = parse(query);
71
+ documentNode = parse(query, { noLocation: true });
62
72
  }
63
73
  catch (err) {
64
74
  throw path.buildCodeFrameError(String(err));
@@ -67,9 +77,19 @@ var index = ({ types: t }, options) => {
67
77
  if (errors.length) {
68
78
  throw path.buildCodeFrameError(errors[0].message);
69
79
  }
70
- const args = [t.stringLiteral(stripIgnoredCharacters(query))];
80
+ const args = [];
81
+ if (options.parse) {
82
+ const id = path.scope.generateUid("gql");
83
+ path.scope.getProgramParent().push({ kind: "let", id: t.identifier(id) });
84
+ args.push(t.assignmentExpression("||=", t.identifier(id), t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
85
+ t.stringLiteral(JSON.stringify(documentNode)),
86
+ ])));
87
+ }
88
+ else {
89
+ args.push(t.stringLiteral(stripIgnoredCharacters(query)));
90
+ }
71
91
  if (expressions.length) {
72
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
92
+ args.push(t.arrayExpression(expressions));
73
93
  }
74
94
  path.replaceWith(t.callExpression(t.identifier(name), args));
75
95
  },
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from \"fs\";\nimport type { default as babel, PluginObj, types as t } from \"@babel/core\";\nimport { encode } from \"@mo36924/base52\";\nimport { buildSchema, buildSchemaModel } from \"@mo36924/graphql-schema\";\nimport {\n DocumentNode,\n GraphQLInputType,\n GraphQLSchema,\n parse,\n stripIgnoredCharacters,\n TypeInfo,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n model: string;\n schema: string | GraphQLSchema;\n};\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema: GraphQLSchema;\n\n if (options.model) {\n const model = readFileSync(options.model || \"index.graphql\", \"utf8\");\n schema = buildSchemaModel(model);\n } else if (typeof options.schema === \"string\") {\n const graphqlSchema = readFileSync(options.schema || \"index.graphql\", \"utf8\");\n schema = buildSchema(graphqlSchema);\n } else if (options.schema) {\n schema = options.schema;\n }\n\n return {\n visitor: {\n TaggedTemplateExpression(path) {\n const {\n tag,\n quasi: { quasis, expressions },\n } = path.node;\n\n if (!t.isIdentifier(tag)) {\n return;\n }\n\n const name = tag.name;\n\n if (name !== \"gql\" && name !== \"query\" && name !== \"mutation\" && name !== \"subscription\") {\n return;\n }\n\n let query = quasis[0].value.cooked ?? quasis[0].value.raw;\n\n for (let i = 0; i < expressions.length; i++) {\n query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;\n }\n\n if (name === \"mutation\" || name === \"subscription\") {\n query = name + query;\n }\n\n let documentNode: DocumentNode;\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const values: GraphQLInputType[] = [];\n const typeInfo = new TypeInfo(schema);\n\n visit(\n documentNode,\n visitWithTypeInfo(typeInfo, {\n Variable() {\n values.push(typeInfo.getInputType()!);\n },\n }),\n );\n\n if (values.length) {\n const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;\n\n if (name === \"query\") {\n query = name + variables + query;\n } else if (name === \"mutation\" || name === \"subscription\") {\n query = name + variables + query.slice(name.length);\n }\n }\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const errors = validate(schema, documentNode);\n\n if (errors.length) {\n throw path.buildCodeFrameError(errors[0].message);\n }\n\n const args: t.Expression[] = [t.stringLiteral(stripIgnoredCharacters(query))];\n\n if (expressions.length) {\n args.push(\n t.objectExpression(\n expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression as any)),\n ),\n );\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), args));\n },\n },\n };\n};\n"],"names":[],"mappings":";;;;;AAqBA,YAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB;IAC1D,IAAI,MAAqB,CAAC;IAE1B,IAAI,OAAO,CAAC,KAAK,EAAE;QACjB,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,IAAI,eAAe,EAAE,MAAM,CAAC,CAAC;QACrE,MAAM,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;KAClC;SAAM,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;QAC7C,MAAM,aAAa,GAAG,YAAY,CAAC,OAAO,CAAC,MAAM,IAAI,eAAe,EAAE,MAAM,CAAC,CAAC;QAC9E,MAAM,GAAG,WAAW,CAAC,aAAa,CAAC,CAAC;KACrC;SAAM,IAAI,OAAO,CAAC,MAAM,EAAE;QACzB,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;KACzB;IAED,OAAO;QACL,OAAO,EAAE;YACP,wBAAwB,CAAC,IAAI;gBAC3B,MAAM,EACJ,GAAG,EACH,KAAK,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,GAC/B,GAAG,IAAI,CAAC,IAAI,C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C,CAAC,EAAE,UAAiB,CAAC,CAAC,CACjG,CACF,CAAC;iBACH;gBAED,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;aAC9D;SACF;KACF,CAAC;AACJ,CAAC;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import babel, { PluginObj, types as t } from \"@babel/core\";\nimport { buildASTSchema } from \"@mo36924/graphql-build\";\nimport { config } from \"@mo36924/graphql-config\";\nimport { buildModel } from \"@mo36924/graphql-model\";\nimport {\n DocumentNode,\n GraphQLInputType,\n GraphQLSchema,\n TypeInfo,\n buildSchema,\n parse,\n stripIgnoredCharacters,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n model?: string | DocumentNode;\n schema?: string | GraphQLSchema;\n parse?: boolean;\n};\n\nlet _schema: GraphQLSchema;\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema = options.schema\n ? typeof options.schema === \"object\"\n ? options.schema\n : buildSchema(options.schema)\n : options.model\n ? typeof options.model === \"object\"\n ? buildASTSchema(options.model)\n : buildModel(options.model).schema\n : (_schema ||= config().schema);\n\n return {\n name: \"graphql-tagged-template\",\n visitor: {\n TaggedTemplateExpression(path, state) {\n const {\n tag,\n quasi: { quasis, expressions },\n } = path.node;\n\n if (!t.isIdentifier(tag)) {\n return;\n }\n\n const name = tag.name;\n let operation = \"\";\n\n switch (name) {\n case \"gql\":\n case \"query\":\n case \"useQuery\":\n break;\n case \"mutation\":\n case \"useMutation\":\n operation = \"mutation\";\n break;\n case \"subscription\":\n case \"useSubscription\":\n operation = \"subscription\";\n break;\n default:\n return;\n }\n\n let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);\n\n for (let i = 0; i < expressions.length; i++) {\n query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;\n }\n\n let documentNode: DocumentNode;\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const values: GraphQLInputType[] = [];\n const typeInfo = new TypeInfo(schema);\n\n visit(\n documentNode,\n visitWithTypeInfo(typeInfo, {\n Variable() {\n values.push(typeInfo.getInputType()!);\n },\n }),\n );\n\n if (values.length) {\n const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;\n\n if (operation) {\n query = operation + variables + query.slice(operation.length);\n } else if (name !== \"gql\") {\n query = \"query\" + variables + query;\n }\n }\n\n try {\n documentNode = parse(query, { noLocation: true });\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const errors = validate(schema, documentNode);\n\n if (errors.length) {\n throw path.buildCodeFrameError(errors[0].message);\n }\n\n const args: t.Expression[] = [];\n\n if (options.parse) {\n const id = path.scope.generateUid(\"gql\");\n path.scope.getProgramParent().push({ kind: \"let\", id: t.identifier(id) });\n\n args.push(\n t.assignmentExpression(\n \"||=\",\n t.identifier(id),\n t.callExpression(t.memberExpression(t.identifier(\"JSON\"), t.identifier(\"parse\")), [\n t.stringLiteral(JSON.stringify(documentNode)),\n ]),\n ),\n );\n } else {\n args.push(t.stringLiteral(stripIgnoredCharacters(query)));\n }\n\n if (expressions.length) {\n args.push(t.arrayExpression(expressions as t.Expression[]));\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), args));\n },\n },\n };\n};\n"],"names":[],"mappings":";;;;;AAuBA,IAAI,OAAsB,CAAC;AAE3B,cAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB,KAAe;AACzE,IAAA,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM;AACzB,UAAE,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ;cAChC,OAAO,CAAC,MAAM;AAChB,cAAE,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC;UAC7B,OAAO,CAAC,KAAK;AACf,cAAE,OAAO,OAAO,CAAC,KAAK,KAAK,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C,CAAC;AAC3D,iBAAA;gBAED,IAAI,WAAW,CAAC,MAAM,EAAE;oBACtB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,eAAe,CAAC,WAA6B,CAAC,CAAC,CAAC;AAC7D,iBAAA;AAED,gBAAA,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;aAC9D;AACF,SAAA;KACF,CAAC;AACJ,CAAC;;;;"}
package/dist/index.mjs CHANGED
@@ -1,38 +1,48 @@
1
- import { readFileSync } from 'fs';
2
- import { encode } from '@mo36924/base52';
3
- import { buildSchemaModel, buildSchema } from '@mo36924/graphql-schema';
4
- import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
1
+ import { buildASTSchema } from '@mo36924/graphql-build';
2
+ import { config } from '@mo36924/graphql-config';
3
+ import { buildModel } from '@mo36924/graphql-model';
4
+ import { buildSchema, parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
5
5
 
6
- var index = ({ types: t }, options) => {
7
- let schema;
8
- if (options.model) {
9
- const model = readFileSync(options.model || "index.graphql", "utf8");
10
- schema = buildSchemaModel(model);
11
- }
12
- else if (typeof options.schema === "string") {
13
- const graphqlSchema = readFileSync(options.schema || "index.graphql", "utf8");
14
- schema = buildSchema(graphqlSchema);
15
- }
16
- else if (options.schema) {
17
- schema = options.schema;
18
- }
6
+ let _schema;
7
+ const index = ({ types: t }, options) => {
8
+ let schema = options.schema
9
+ ? typeof options.schema === "object"
10
+ ? options.schema
11
+ : buildSchema(options.schema)
12
+ : options.model
13
+ ? typeof options.model === "object"
14
+ ? buildASTSchema(options.model)
15
+ : buildModel(options.model).schema
16
+ : (_schema ||= config().schema);
19
17
  return {
18
+ name: "graphql-tagged-template",
20
19
  visitor: {
21
- TaggedTemplateExpression(path) {
20
+ TaggedTemplateExpression(path, state) {
22
21
  const { tag, quasi: { quasis, expressions }, } = path.node;
23
22
  if (!t.isIdentifier(tag)) {
24
23
  return;
25
24
  }
26
25
  const name = tag.name;
27
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
28
- return;
26
+ let operation = "";
27
+ switch (name) {
28
+ case "gql":
29
+ case "query":
30
+ case "useQuery":
31
+ break;
32
+ case "mutation":
33
+ case "useMutation":
34
+ operation = "mutation";
35
+ break;
36
+ case "subscription":
37
+ case "useSubscription":
38
+ operation = "subscription";
39
+ break;
40
+ default:
41
+ return;
29
42
  }
30
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
43
+ let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);
31
44
  for (let i = 0; i < expressions.length; i++) {
32
- query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
33
- }
34
- if (name === "mutation" || name === "subscription") {
35
- query = name + query;
45
+ query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
36
46
  }
37
47
  let documentNode;
38
48
  try {
@@ -49,16 +59,16 @@ var index = ({ types: t }, options) => {
49
59
  },
50
60
  }));
51
61
  if (values.length) {
52
- const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
53
- if (name === "query") {
54
- query = name + variables + query;
62
+ const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;
63
+ if (operation) {
64
+ query = operation + variables + query.slice(operation.length);
55
65
  }
56
- else if (name === "mutation" || name === "subscription") {
57
- query = name + variables + query.slice(name.length);
66
+ else if (name !== "gql") {
67
+ query = "query" + variables + query;
58
68
  }
59
69
  }
60
70
  try {
61
- documentNode = parse(query);
71
+ documentNode = parse(query, { noLocation: true });
62
72
  }
63
73
  catch (err) {
64
74
  throw path.buildCodeFrameError(String(err));
@@ -67,9 +77,19 @@ var index = ({ types: t }, options) => {
67
77
  if (errors.length) {
68
78
  throw path.buildCodeFrameError(errors[0].message);
69
79
  }
70
- const args = [t.stringLiteral(stripIgnoredCharacters(query))];
80
+ const args = [];
81
+ if (options.parse) {
82
+ const id = path.scope.generateUid("gql");
83
+ path.scope.getProgramParent().push({ kind: "let", id: t.identifier(id) });
84
+ args.push(t.assignmentExpression("||=", t.identifier(id), t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
85
+ t.stringLiteral(JSON.stringify(documentNode)),
86
+ ])));
87
+ }
88
+ else {
89
+ args.push(t.stringLiteral(stripIgnoredCharacters(query)));
90
+ }
71
91
  if (expressions.length) {
72
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
92
+ args.push(t.arrayExpression(expressions));
73
93
  }
74
94
  path.replaceWith(t.callExpression(t.identifier(name), args));
75
95
  },
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from \"fs\";\nimport type { default as babel, PluginObj, types as t } from \"@babel/core\";\nimport { encode } from \"@mo36924/base52\";\nimport { buildSchema, buildSchemaModel } from \"@mo36924/graphql-schema\";\nimport {\n DocumentNode,\n GraphQLInputType,\n GraphQLSchema,\n parse,\n stripIgnoredCharacters,\n TypeInfo,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n model: string;\n schema: string | GraphQLSchema;\n};\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema: GraphQLSchema;\n\n if (options.model) {\n const model = readFileSync(options.model || \"index.graphql\", \"utf8\");\n schema = buildSchemaModel(model);\n } else if (typeof options.schema === \"string\") {\n const graphqlSchema = readFileSync(options.schema || \"index.graphql\", \"utf8\");\n schema = buildSchema(graphqlSchema);\n } else if (options.schema) {\n schema = options.schema;\n }\n\n return {\n visitor: {\n TaggedTemplateExpression(path) {\n const {\n tag,\n quasi: { quasis, expressions },\n } = path.node;\n\n if (!t.isIdentifier(tag)) {\n return;\n }\n\n const name = tag.name;\n\n if (name !== \"gql\" && name !== \"query\" && name !== \"mutation\" && name !== \"subscription\") {\n return;\n }\n\n let query = quasis[0].value.cooked ?? quasis[0].value.raw;\n\n for (let i = 0; i < expressions.length; i++) {\n query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;\n }\n\n if (name === \"mutation\" || name === \"subscription\") {\n query = name + query;\n }\n\n let documentNode: DocumentNode;\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const values: GraphQLInputType[] = [];\n const typeInfo = new TypeInfo(schema);\n\n visit(\n documentNode,\n visitWithTypeInfo(typeInfo, {\n Variable() {\n values.push(typeInfo.getInputType()!);\n },\n }),\n );\n\n if (values.length) {\n const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;\n\n if (name === \"query\") {\n query = name + variables + query;\n } else if (name === \"mutation\" || name === \"subscription\") {\n query = name + variables + query.slice(name.length);\n }\n }\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const errors = validate(schema, documentNode);\n\n if (errors.length) {\n throw path.buildCodeFrameError(errors[0].message);\n }\n\n const args: t.Expression[] = [t.stringLiteral(stripIgnoredCharacters(query))];\n\n if (expressions.length) {\n args.push(\n t.objectExpression(\n expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression as any)),\n ),\n );\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), args));\n },\n },\n };\n};\n"],"names":[],"mappings":";;;;;AAqBA,YAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB;IAC1D,IAAI,MAAqB,CAAC;IAE1B,IAAI,OAAO,CAAC,KAAK,EAAE;QACjB,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,IAAI,eAAe,EAAE,MAAM,CAAC,CAAC;QACrE,MAAM,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;KAClC;SAAM,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;Q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mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;iBACnD;gBAED,MAAM,IAAI,GAAmB,CAAC,CAAC,CAAC,aAAa,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAE9E,IAAI,WAAW,CAAC,MAAM,EAAE;oBACtB,IAAI,CAAC,IAAI,CACP,CAAC,CAAC,gBAAgB,CAChB,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,UAAiB,CAAC,CAAC,CACjG,CACF,CAAC;iBACH;gBAED,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;aAC9D;SACF;KACF,CAAC;AACJ,CAAC;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import babel, { PluginObj, types as t } from \"@babel/core\";\nimport { buildASTSchema } from \"@mo36924/graphql-build\";\nimport { config } from \"@mo36924/graphql-config\";\nimport { buildModel } from \"@mo36924/graphql-model\";\nimport {\n DocumentNode,\n GraphQLInputType,\n GraphQLSchema,\n TypeInfo,\n buildSchema,\n parse,\n stripIgnoredCharacters,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n model?: string | DocumentNode;\n schema?: string | GraphQLSchema;\n parse?: boolean;\n};\n\nlet _schema: GraphQLSchema;\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema = options.schema\n ? typeof options.schema === \"object\"\n ? options.schema\n : buildSchema(options.schema)\n : options.model\n ? typeof options.model === \"object\"\n ? buildASTSchema(options.model)\n : buildModel(options.model).schema\n : (_schema ||= config().schema);\n\n return {\n name: \"graphql-tagged-template\",\n visitor: {\n TaggedTemplateExpression(path, state) {\n const {\n tag,\n quasi: { quasis, expressions },\n } = path.node;\n\n if (!t.isIdentifier(tag)) {\n return;\n }\n\n const name = tag.name;\n let operation = \"\";\n\n switch (name) {\n case \"gql\":\n case \"query\":\n case \"useQuery\":\n break;\n case \"mutation\":\n case \"useMutation\":\n operation = \"mutation\";\n break;\n case \"subscription\":\n case \"useSubscription\":\n operation = \"subscription\";\n break;\n default:\n return;\n }\n\n let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);\n\n for (let i = 0; i < expressions.length; i++) {\n query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;\n }\n\n let documentNode: DocumentNode;\n\n try {\n documentNode = parse(query);\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const values: GraphQLInputType[] = [];\n const typeInfo = new TypeInfo(schema);\n\n visit(\n documentNode,\n visitWithTypeInfo(typeInfo, {\n Variable() {\n values.push(typeInfo.getInputType()!);\n },\n }),\n );\n\n if (values.length) {\n const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;\n\n if (operation) {\n query = operation + variables + query.slice(operation.length);\n } else if (name !== \"gql\") {\n query = \"query\" + variables + query;\n }\n }\n\n try {\n documentNode = parse(query, { noLocation: true });\n } catch (err) {\n throw path.buildCodeFrameError(String(err));\n }\n\n const errors = validate(schema, documentNode);\n\n if (errors.length) {\n throw path.buildCodeFrameError(errors[0].message);\n }\n\n const args: t.Expression[] = [];\n\n if (options.parse) {\n const id = path.scope.generateUid(\"gql\");\n path.scope.getProgramParent().push({ kind: \"let\", id: t.identifier(id) });\n\n args.push(\n t.assignmentExpression(\n \"||=\",\n t.identifier(id),\n t.callExpression(t.memberExpression(t.identifier(\"JSON\"), t.identifier(\"parse\")), [\n t.stringLiteral(JSON.stringify(documentNode)),\n ]),\n ),\n );\n } else {\n args.push(t.stringLiteral(stripIgnoredCharacters(query)));\n }\n\n if (expressions.length) {\n args.push(t.arrayExpression(expressions as t.Expression[]));\n }\n\n path.replaceWith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,CAAC,EAAE,CAAC,EAChB,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,EAAE;wBAChF,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;qBAC9C,CAAC,CACH,CACF,CAAC;AACH,iBAAA;AAAM,qBAAA;AACL,oBAAA,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAC3D,iBAAA;gBAED,IAAI,WAAW,CAAC,MAAM,EAAE;oBACtB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,eAAe,CAAC,WAA6B,CAAC,CAAC,CAAC;AAC7D,iBAAA;AAED,gBAAA,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;aAC9D;AACF,SAAA;KACF,CAAC;AACJ,CAAC;;;;"}
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@mo36924/babel-plugin-graphql-tagged-template",
3
- "version": "1.5.0",
3
+ "version": "1.5.21",
4
4
  "description": "babel-plugin-graphql-tagged-template",
5
5
  "keywords": [],
6
- "homepage": "https://github.com/mo36924/monorepo#readme",
6
+ "homepage": "https://github.com/mo36924/graphql#readme",
7
7
  "bugs": {
8
- "url": "https://github.com/mo36924/monorepo/issues"
8
+ "url": "https://github.com/mo36924/graphql/issues"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "git+https://github.com/mo36924/monorepo.git",
12
+ "url": "git+https://github.com/mo36924/graphql.git",
13
13
  "directory": "packages/babel-plugin-graphql-tagged-template"
14
14
  },
15
15
  "license": "MIT",
16
- "author": "mo36924 <mo36924@users.noreply.github.com>",
16
+ "author": "mo36924",
17
17
  "exports": {
18
18
  ".": {
19
19
  "types": "./dist/index.d.ts",
@@ -23,23 +23,21 @@
23
23
  "default": "./dist/index.cjs"
24
24
  }
25
25
  },
26
- "main": "./index.cjs",
27
- "module": "./index.mjs",
28
- "typesVersions": {
29
- "*": {
30
- "*": [
31
- "dist/*.d.ts"
32
- ]
33
- }
34
- },
26
+ "main": "./dist/index.cjs",
27
+ "module": "./dist/index.mjs",
28
+ "types": "./dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
35
32
  "dependencies": {
36
- "@babel/core": "^7.15.0",
37
- "@mo36924/base52": "^1.5.0",
38
- "@mo36924/graphql-schema": "^1.5.0",
39
- "graphql": "^15.5.0"
33
+ "@babel/core": "^7.17.8",
34
+ "@mo36924/graphql-build": "^1.5.21",
35
+ "@mo36924/graphql-config": "^1.5.21",
36
+ "@mo36924/graphql-model": "^1.5.21",
37
+ "graphql": "^16.3.0"
40
38
  },
41
39
  "publishConfig": {
42
40
  "access": "public"
43
41
  },
44
- "gitHead": "3998143a19e84538b9226e4617155e8f49ac5588"
42
+ "gitHead": "943062323c8119e2b226abdfbe8a9145870f6042"
45
43
  }
package/src/index.test.ts DELETED
@@ -1,91 +0,0 @@
1
- import { TransformOptions, transformSync } from "@babel/core";
2
- import { describe, expect, test } from "@jest/globals";
3
- import { buildSchema } from "graphql";
4
- import plugin, { Options } from "./index";
5
-
6
- const schema = buildSchema(`
7
- scalar Unknown
8
- type Query {
9
- user(offset: Int): User
10
- }
11
- type Mutation {
12
- create(name: String): User
13
- }
14
- type User {
15
- name: String
16
- }
17
- `);
18
-
19
- const options: TransformOptions = {
20
- babelrc: false,
21
- configFile: false,
22
- plugins: [[plugin, { schema } as Options]],
23
- };
24
-
25
- const transform = (code: string) => transformSync(code, options);
26
-
27
- describe("babel-plugin-graphql-tagged-template", () => {
28
- test("gql query", () => {
29
- const result = transform(`
30
- const params = gql\`{
31
- user(offset: 2) {
32
- name
33
- }
34
- }\`
35
- `);
36
-
37
- expect(result).toMatchInlineSnapshot(`const params = gql("{user(offset:2){name}}");`);
38
- });
39
-
40
- test("gql mutation", () => {
41
- const result = transform(`
42
- const params = gql\`mutation($name: String!){
43
- create(name: $name) {
44
- name
45
- }
46
- }\`
47
- `);
48
-
49
- expect(result).toMatchInlineSnapshot(`const params = gql("mutation($name:String!){create(name:$name){name}}");`);
50
- });
51
-
52
- test("query", () => {
53
- const result = transform(`
54
- const offset = 2
55
- query\`
56
- {
57
- user(offset: \${offset}) {
58
- name
59
- }
60
- }
61
- \`
62
- `);
63
-
64
- expect(result).toMatchInlineSnapshot(`
65
- const offset = 2;
66
- query("query($a:Int){user(offset:$a){name}}", {
67
- a: offset,
68
- });
69
- `);
70
- });
71
-
72
- test("mutation", () => {
73
- const result = transform(`
74
- const name = "hoge";
75
- mutation\`
76
- {
77
- create(name: \${name}) {
78
- name
79
- }
80
- }
81
- \`
82
- `);
83
-
84
- expect(result).toMatchInlineSnapshot(`
85
- const name = "hoge";
86
- mutation("mutation($a:String){create(name:$a){name}}", {
87
- a: name,
88
- });
89
- `);
90
- });
91
- });
package/src/index.ts DELETED
@@ -1,119 +0,0 @@
1
- import { readFileSync } from "fs";
2
- import type { default as babel, PluginObj, types as t } from "@babel/core";
3
- import { encode } from "@mo36924/base52";
4
- import { buildSchema, buildSchemaModel } from "@mo36924/graphql-schema";
5
- import {
6
- DocumentNode,
7
- GraphQLInputType,
8
- GraphQLSchema,
9
- parse,
10
- stripIgnoredCharacters,
11
- TypeInfo,
12
- validate,
13
- visit,
14
- visitWithTypeInfo,
15
- } from "graphql";
16
-
17
- export type Options = {
18
- model: string;
19
- schema: string | GraphQLSchema;
20
- };
21
-
22
- export default ({ types: t }: typeof babel, options: Options): PluginObj => {
23
- let schema: GraphQLSchema;
24
-
25
- if (options.model) {
26
- const model = readFileSync(options.model || "index.graphql", "utf8");
27
- schema = buildSchemaModel(model);
28
- } else if (typeof options.schema === "string") {
29
- const graphqlSchema = readFileSync(options.schema || "index.graphql", "utf8");
30
- schema = buildSchema(graphqlSchema);
31
- } else if (options.schema) {
32
- schema = options.schema;
33
- }
34
-
35
- return {
36
- visitor: {
37
- TaggedTemplateExpression(path) {
38
- const {
39
- tag,
40
- quasi: { quasis, expressions },
41
- } = path.node;
42
-
43
- if (!t.isIdentifier(tag)) {
44
- return;
45
- }
46
-
47
- const name = tag.name;
48
-
49
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
50
- return;
51
- }
52
-
53
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
54
-
55
- for (let i = 0; i < expressions.length; i++) {
56
- query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
57
- }
58
-
59
- if (name === "mutation" || name === "subscription") {
60
- query = name + query;
61
- }
62
-
63
- let documentNode: DocumentNode;
64
-
65
- try {
66
- documentNode = parse(query);
67
- } catch (err) {
68
- throw path.buildCodeFrameError(String(err));
69
- }
70
-
71
- const values: GraphQLInputType[] = [];
72
- const typeInfo = new TypeInfo(schema);
73
-
74
- visit(
75
- documentNode,
76
- visitWithTypeInfo(typeInfo, {
77
- Variable() {
78
- values.push(typeInfo.getInputType()!);
79
- },
80
- }),
81
- );
82
-
83
- if (values.length) {
84
- const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
85
-
86
- if (name === "query") {
87
- query = name + variables + query;
88
- } else if (name === "mutation" || name === "subscription") {
89
- query = name + variables + query.slice(name.length);
90
- }
91
- }
92
-
93
- try {
94
- documentNode = parse(query);
95
- } catch (err) {
96
- throw path.buildCodeFrameError(String(err));
97
- }
98
-
99
- const errors = validate(schema, documentNode);
100
-
101
- if (errors.length) {
102
- throw path.buildCodeFrameError(errors[0].message);
103
- }
104
-
105
- const args: t.Expression[] = [t.stringLiteral(stripIgnoredCharacters(query))];
106
-
107
- if (expressions.length) {
108
- args.push(
109
- t.objectExpression(
110
- expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression as any)),
111
- ),
112
- );
113
- }
114
-
115
- path.replaceWith(t.callExpression(t.identifier(name), args));
116
- },
117
- },
118
- };
119
- };