@mo36924/babel-plugin-graphql-tagged-template 1.6.1 → 1.6.5

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs CHANGED
@@ -1,24 +1,30 @@
1
1
  'use strict';
2
2
 
3
3
  const fs = require('fs');
4
- const base52 = require('@mo36924/base52');
5
- const graphqlSchema = require('@mo36924/graphql-schema');
4
+ const graphqlBuild = require('@mo36924/graphql-build');
5
+ const graphqlConfig = require('@mo36924/graphql-config');
6
+ const graphqlModel = require('@mo36924/graphql-model');
6
7
  const graphql = require('graphql');
7
8
 
8
9
  const index = ({ types: t }, options) => {
9
10
  let schema;
10
- if (options.model) {
11
- const model = fs.readFileSync(options.model || "index.graphql", "utf8");
12
- schema = graphqlSchema.buildSchemaModel(model);
11
+ if (typeof options.schema === "string") {
12
+ schema = graphqlBuild.buildASTSchema(graphql.parse(fs.readFileSync(options.schema, "utf-8")));
13
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) {
14
+ else if (options.schema && typeof options.schema === "object") {
19
15
  schema = options.schema;
20
16
  }
17
+ else if (typeof options.model === "string") {
18
+ schema = graphqlModel.buildModel(fs.readFileSync(options.model, "utf-8")).schema;
19
+ }
20
+ else if (options.model && typeof options.model === "object") {
21
+ schema = graphqlBuild.buildASTSchema(options.model);
22
+ }
23
+ else {
24
+ schema = graphqlConfig.config().schema;
25
+ }
21
26
  return {
27
+ name: "graphql-tagged-template",
22
28
  visitor: {
23
29
  TaggedTemplateExpression(path) {
24
30
  const { tag, quasi: { quasis, expressions }, } = path.node;
@@ -26,15 +32,26 @@ const index = ({ types: t }, options) => {
26
32
  return;
27
33
  }
28
34
  const name = tag.name;
29
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
30
- return;
35
+ let operation = "";
36
+ switch (name) {
37
+ case "gql":
38
+ case "query":
39
+ case "useQuery":
40
+ break;
41
+ case "mutation":
42
+ case "useMutation":
43
+ operation = "mutation";
44
+ break;
45
+ case "subscription":
46
+ case "useSubscription":
47
+ operation = "subscription";
48
+ break;
49
+ default:
50
+ return;
31
51
  }
32
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
52
+ let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);
33
53
  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;
54
+ query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
38
55
  }
39
56
  let documentNode;
40
57
  try {
@@ -51,16 +68,16 @@ const index = ({ types: t }, options) => {
51
68
  },
52
69
  }));
53
70
  if (values.length) {
54
- const variables = `(${values.map((value, i) => `$${base52.encode(i)}:${value}`).join()})`;
55
- if (name === "query") {
56
- query = name + variables + query;
71
+ const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;
72
+ if (operation) {
73
+ query = operation + variables + query.slice(operation.length);
57
74
  }
58
- else if (name === "mutation" || name === "subscription") {
59
- query = name + variables + query.slice(name.length);
75
+ else if (name !== "gql") {
76
+ query = "query" + variables + query;
60
77
  }
61
78
  }
62
79
  try {
63
- documentNode = graphql.parse(query);
80
+ documentNode = graphql.parse(query, { noLocation: true });
64
81
  }
65
82
  catch (err) {
66
83
  throw path.buildCodeFrameError(String(err));
@@ -69,11 +86,21 @@ const index = ({ types: t }, options) => {
69
86
  if (errors.length) {
70
87
  throw path.buildCodeFrameError(errors[0].message);
71
88
  }
72
- const args = [t.stringLiteral(graphql.stripIgnoredCharacters(query))];
89
+ const properties = [];
90
+ if (options.parse) {
91
+ const id = path.scope.generateUid("gql");
92
+ path.scope.getProgramParent().push({ kind: "let", id: t.identifier(id) });
93
+ properties.push(t.objectProperty(t.identifier("document"), t.assignmentExpression("||=", t.identifier(id), t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
94
+ t.stringLiteral(JSON.stringify(documentNode)),
95
+ ]))));
96
+ }
97
+ else {
98
+ properties.push(t.objectProperty(t.identifier("query"), t.stringLiteral(graphql.stripIgnoredCharacters(query))));
99
+ }
73
100
  if (expressions.length) {
74
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(base52.encode(i)), expression))));
101
+ properties.push(t.objectProperty(t.identifier("variables"), t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier("_" + i), expression)))));
75
102
  }
76
- path.replaceWith(t.callExpression(t.identifier(name), args));
103
+ path.replaceWith(t.callExpression(t.identifier(name), [t.objectExpression(properties)]));
77
104
  },
78
105
  },
79
106
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from \"fs\";\nimport type { PluginObj, default as babel, 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 TypeInfo,\n parse,\n stripIgnoredCharacters,\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,cAAe,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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AAE,YAAY,CAAC,CAAC;gBAE9C,IAAI,MAAM,CAAC,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 { readFileSync } from \"fs\";\nimport 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 parse,\n stripIgnoredCharacters,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n parse?: boolean;\n model?: string | DocumentNode;\n schema?: string | GraphQLSchema;\n};\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema: GraphQLSchema;\n\n if (typeof options.schema === \"string\") {\n schema = buildASTSchema(parse(readFileSync(options.schema, \"utf-8\")));\n } else if (options.schema && typeof options.schema === \"object\") {\n schema = options.schema;\n } else if (typeof options.model === \"string\") {\n schema = buildModel(readFileSync(options.model, \"utf-8\")).schema;\n } else if (options.model && typeof options.model === \"object\") {\n schema = buildASTSchema(options.model);\n } else {\n schema = config().schema;\n }\n\n return {\n name: \"graphql-tagged-template\",\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 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 properties: t.ObjectProperty[] = [];\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 properties.push(\n t.objectProperty(\n t.identifier(\"document\"),\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 );\n } else {\n properties.push(t.objectProperty(t.identifier(\"query\"), t.stringLiteral(stripIgnoredCharacters(query))));\n }\n\n if (expressions.length) {\n properties.push(\n t.objectProperty(\n t.identifier(\"variables\"),\n t.objectExpression(\n expressions.map((expression, i) => t.objectProperty(t.identifier(\"_\" + i), expression as t.Expression)),\n ),\n ),\n );\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), [t.objectExpression(properties)]));\n },\n },\n };\n};\n"],"names":["buildASTSchema","parse","readFileSync","buildModel","config","TypeInfo","visit","visitWithTypeInfo","validate","stripIgnoredCharacters"],"mappings":";;;;;;;;AAuBA,cAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB;IAC1D,IAAI,MAAqB,CAAC;IAE1B,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;QACtC,MAAM,GAAGA,2BAAc,CAACC,aAAK,CAACC,eAAY,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;KACvE;SAAM,IAAI,OAAO,CAAC,MAAM,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;QAC/D,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;KACzB;SAAM,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ,EAAE;QAC5C,MAAM,GAAGC,uBAAU,CAACD,eAAY,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;KAClE;SAAM,IAAI,OAAO,CAAC,KAAK,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ,EAAE;QAC7D,MAAM,GAAGF,2BAAc,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;KACxC;SAAM;QACL,MAAM,GAAGI,oBAA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ACxG,CACF,CACF,CAAC;iBACH;gBAED,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;aAC1F;SACF;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
+ parse?: boolean;
6
+ model?: string | DocumentNode;
7
+ schema?: string | GraphQLSchema;
7
8
  };
8
9
  declare const _default: ({ types: t }: typeof babel, options: Options) => PluginObj;
9
10
 
package/dist/index.js CHANGED
@@ -1,22 +1,28 @@
1
1
  import { readFileSync } from 'fs';
2
- import { encode } from '@mo36924/base52';
3
- import { buildSchemaModel, buildSchema } from '@mo36924/graphql-schema';
2
+ import { buildASTSchema } from '@mo36924/graphql-build';
3
+ import { config } from '@mo36924/graphql-config';
4
+ import { buildModel } from '@mo36924/graphql-model';
4
5
  import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
5
6
 
6
7
  const index = ({ types: t }, options) => {
7
8
  let schema;
8
- if (options.model) {
9
- const model = readFileSync(options.model || "index.graphql", "utf8");
10
- schema = buildSchemaModel(model);
9
+ if (typeof options.schema === "string") {
10
+ schema = buildASTSchema(parse(readFileSync(options.schema, "utf-8")));
11
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) {
12
+ else if (options.schema && typeof options.schema === "object") {
17
13
  schema = options.schema;
18
14
  }
15
+ else if (typeof options.model === "string") {
16
+ schema = buildModel(readFileSync(options.model, "utf-8")).schema;
17
+ }
18
+ else if (options.model && typeof options.model === "object") {
19
+ schema = buildASTSchema(options.model);
20
+ }
21
+ else {
22
+ schema = config().schema;
23
+ }
19
24
  return {
25
+ name: "graphql-tagged-template",
20
26
  visitor: {
21
27
  TaggedTemplateExpression(path) {
22
28
  const { tag, quasi: { quasis, expressions }, } = path.node;
@@ -24,15 +30,26 @@ const index = ({ types: t }, options) => {
24
30
  return;
25
31
  }
26
32
  const name = tag.name;
27
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
28
- return;
33
+ let operation = "";
34
+ switch (name) {
35
+ case "gql":
36
+ case "query":
37
+ case "useQuery":
38
+ break;
39
+ case "mutation":
40
+ case "useMutation":
41
+ operation = "mutation";
42
+ break;
43
+ case "subscription":
44
+ case "useSubscription":
45
+ operation = "subscription";
46
+ break;
47
+ default:
48
+ return;
29
49
  }
30
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
50
+ let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);
31
51
  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;
52
+ query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
36
53
  }
37
54
  let documentNode;
38
55
  try {
@@ -49,16 +66,16 @@ const index = ({ types: t }, options) => {
49
66
  },
50
67
  }));
51
68
  if (values.length) {
52
- const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
53
- if (name === "query") {
54
- query = name + variables + query;
69
+ const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;
70
+ if (operation) {
71
+ query = operation + variables + query.slice(operation.length);
55
72
  }
56
- else if (name === "mutation" || name === "subscription") {
57
- query = name + variables + query.slice(name.length);
73
+ else if (name !== "gql") {
74
+ query = "query" + variables + query;
58
75
  }
59
76
  }
60
77
  try {
61
- documentNode = parse(query);
78
+ documentNode = parse(query, { noLocation: true });
62
79
  }
63
80
  catch (err) {
64
81
  throw path.buildCodeFrameError(String(err));
@@ -67,11 +84,21 @@ const index = ({ types: t }, options) => {
67
84
  if (errors.length) {
68
85
  throw path.buildCodeFrameError(errors[0].message);
69
86
  }
70
- const args = [t.stringLiteral(stripIgnoredCharacters(query))];
87
+ const properties = [];
88
+ if (options.parse) {
89
+ const id = path.scope.generateUid("gql");
90
+ path.scope.getProgramParent().push({ kind: "let", id: t.identifier(id) });
91
+ properties.push(t.objectProperty(t.identifier("document"), t.assignmentExpression("||=", t.identifier(id), t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
92
+ t.stringLiteral(JSON.stringify(documentNode)),
93
+ ]))));
94
+ }
95
+ else {
96
+ properties.push(t.objectProperty(t.identifier("query"), t.stringLiteral(stripIgnoredCharacters(query))));
97
+ }
71
98
  if (expressions.length) {
72
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
99
+ properties.push(t.objectProperty(t.identifier("variables"), t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier("_" + i), expression)))));
73
100
  }
74
- path.replaceWith(t.callExpression(t.identifier(name), args));
101
+ path.replaceWith(t.callExpression(t.identifier(name), [t.objectExpression(properties)]));
75
102
  },
76
103
  },
77
104
  };
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 { PluginObj, default as babel, 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 TypeInfo,\n parse,\n stripIgnoredCharacters,\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.call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S,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;qBACrD;iBACF;gBAED,IAAI;oBACF,YAAY,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;iBAC7B;gBAAC,OAAO,GAAG,EAAE;oBACZ,MAAM,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;iBAC7C;gBAED,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;gBAE9C,IAAI,MAAM,CAAC,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,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.js","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from \"fs\";\nimport 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 parse,\n stripIgnoredCharacters,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n parse?: boolean;\n model?: string | DocumentNode;\n schema?: string | GraphQLSchema;\n};\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema: GraphQLSchema;\n\n if (typeof options.schema === \"string\") {\n schema = buildASTSchema(parse(readFileSync(options.schema, \"utf-8\")));\n } else if (options.schema && typeof options.schema === \"object\") {\n schema = options.schema;\n } else if (typeof options.model === \"string\") {\n schema = buildModel(readFileSync(options.model, \"utf-8\")).schema;\n } else if (options.model && typeof options.model === \"object\") {\n schema = buildASTSchema(options.model);\n } else {\n schema = config().schema;\n }\n\n return {\n name: \"graphql-tagged-template\",\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 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 properties: t.ObjectProperty[] = [];\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 properties.push(\n t.objectProperty(\n t.identifier(\"document\"),\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 );\n } else {\n properties.push(t.objectProperty(t.identifier(\"query\"), t.stringLiteral(stripIgnoredCharacters(query))));\n }\n\n if (expressions.length) {\n properties.push(\n t.objectProperty(\n t.identifier(\"variables\"),\n t.objectExpression(\n expressions.map((expression, i) => t.objectProperty(t.identifier(\"_\" + i), expression as t.Expression)),\n ),\n ),\n );\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), [t.objectExpression(properties)]));\n },\n },\n };\n};\n"],"names":[],"mappings":";;;;;;AAuBA,cAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB;IAC1D,IAAI,MAAqB,CAAC;IAE1B,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;QACtC,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;KACvE;SAAM,IAAI,OAAO,CAAC,MAAM,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;QAC/D,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;KACzB;SAAM,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ,EAAE;QAC5C,MAAM,GAAG,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,OAA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,CAAC,WAAW,CAAC,EACzB,CAAC,CAAC,gBAAgB,CAChB,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC,EAAE,UAA0B,CAAC,CAAC,CACxG,CACF,CACF,CAAC;iBACH;gBAED,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;aAC1F;SACF;KACF,CAAC;AACJ,CAAC;;;;"}
package/dist/index.mjs CHANGED
@@ -1,22 +1,28 @@
1
1
  import { readFileSync } from 'fs';
2
- import { encode } from '@mo36924/base52';
3
- import { buildSchemaModel, buildSchema } from '@mo36924/graphql-schema';
2
+ import { buildASTSchema } from '@mo36924/graphql-build';
3
+ import { config } from '@mo36924/graphql-config';
4
+ import { buildModel } from '@mo36924/graphql-model';
4
5
  import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
5
6
 
6
7
  const index = ({ types: t }, options) => {
7
8
  let schema;
8
- if (options.model) {
9
- const model = readFileSync(options.model || "index.graphql", "utf8");
10
- schema = buildSchemaModel(model);
9
+ if (typeof options.schema === "string") {
10
+ schema = buildASTSchema(parse(readFileSync(options.schema, "utf-8")));
11
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) {
12
+ else if (options.schema && typeof options.schema === "object") {
17
13
  schema = options.schema;
18
14
  }
15
+ else if (typeof options.model === "string") {
16
+ schema = buildModel(readFileSync(options.model, "utf-8")).schema;
17
+ }
18
+ else if (options.model && typeof options.model === "object") {
19
+ schema = buildASTSchema(options.model);
20
+ }
21
+ else {
22
+ schema = config().schema;
23
+ }
19
24
  return {
25
+ name: "graphql-tagged-template",
20
26
  visitor: {
21
27
  TaggedTemplateExpression(path) {
22
28
  const { tag, quasi: { quasis, expressions }, } = path.node;
@@ -24,15 +30,26 @@ const index = ({ types: t }, options) => {
24
30
  return;
25
31
  }
26
32
  const name = tag.name;
27
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
28
- return;
33
+ let operation = "";
34
+ switch (name) {
35
+ case "gql":
36
+ case "query":
37
+ case "useQuery":
38
+ break;
39
+ case "mutation":
40
+ case "useMutation":
41
+ operation = "mutation";
42
+ break;
43
+ case "subscription":
44
+ case "useSubscription":
45
+ operation = "subscription";
46
+ break;
47
+ default:
48
+ return;
29
49
  }
30
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
50
+ let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);
31
51
  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;
52
+ query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
36
53
  }
37
54
  let documentNode;
38
55
  try {
@@ -49,16 +66,16 @@ const index = ({ types: t }, options) => {
49
66
  },
50
67
  }));
51
68
  if (values.length) {
52
- const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
53
- if (name === "query") {
54
- query = name + variables + query;
69
+ const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;
70
+ if (operation) {
71
+ query = operation + variables + query.slice(operation.length);
55
72
  }
56
- else if (name === "mutation" || name === "subscription") {
57
- query = name + variables + query.slice(name.length);
73
+ else if (name !== "gql") {
74
+ query = "query" + variables + query;
58
75
  }
59
76
  }
60
77
  try {
61
- documentNode = parse(query);
78
+ documentNode = parse(query, { noLocation: true });
62
79
  }
63
80
  catch (err) {
64
81
  throw path.buildCodeFrameError(String(err));
@@ -67,11 +84,21 @@ const index = ({ types: t }, options) => {
67
84
  if (errors.length) {
68
85
  throw path.buildCodeFrameError(errors[0].message);
69
86
  }
70
- const args = [t.stringLiteral(stripIgnoredCharacters(query))];
87
+ const properties = [];
88
+ if (options.parse) {
89
+ const id = path.scope.generateUid("gql");
90
+ path.scope.getProgramParent().push({ kind: "let", id: t.identifier(id) });
91
+ properties.push(t.objectProperty(t.identifier("document"), t.assignmentExpression("||=", t.identifier(id), t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [
92
+ t.stringLiteral(JSON.stringify(documentNode)),
93
+ ]))));
94
+ }
95
+ else {
96
+ properties.push(t.objectProperty(t.identifier("query"), t.stringLiteral(stripIgnoredCharacters(query))));
97
+ }
71
98
  if (expressions.length) {
72
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
99
+ properties.push(t.objectProperty(t.identifier("variables"), t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier("_" + i), expression)))));
73
100
  }
74
- path.replaceWith(t.callExpression(t.identifier(name), args));
101
+ path.replaceWith(t.callExpression(t.identifier(name), [t.objectExpression(properties)]));
75
102
  },
76
103
  },
77
104
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from \"fs\";\nimport type { PluginObj, default as babel, 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 TypeInfo,\n parse,\n stripIgnoredCharacters,\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,cAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB;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KAAK,CAAC,CAAC;iBAC7B;gBAAC,OAAO,GAAG,EAAE;oBACZ,MAAM,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;iBAC7C;gBAED,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;gBAE9C,IAAI,MAAM,CAAC,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,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 { readFileSync } from \"fs\";\nimport 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 parse,\n stripIgnoredCharacters,\n validate,\n visit,\n visitWithTypeInfo,\n} from \"graphql\";\n\nexport type Options = {\n parse?: boolean;\n model?: string | DocumentNode;\n schema?: string | GraphQLSchema;\n};\n\nexport default ({ types: t }: typeof babel, options: Options): PluginObj => {\n let schema: GraphQLSchema;\n\n if (typeof options.schema === \"string\") {\n schema = buildASTSchema(parse(readFileSync(options.schema, \"utf-8\")));\n } else if (options.schema && typeof options.schema === \"object\") {\n schema = options.schema;\n } else if (typeof options.model === \"string\") {\n schema = buildModel(readFileSync(options.model, \"utf-8\")).schema;\n } else if (options.model && typeof options.model === \"object\") {\n schema = buildASTSchema(options.model);\n } else {\n schema = config().schema;\n }\n\n return {\n name: \"graphql-tagged-template\",\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 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 properties: t.ObjectProperty[] = [];\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 properties.push(\n t.objectProperty(\n t.identifier(\"document\"),\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 );\n } else {\n properties.push(t.objectProperty(t.identifier(\"query\"), t.stringLiteral(stripIgnoredCharacters(query))));\n }\n\n if (expressions.length) {\n properties.push(\n t.objectProperty(\n t.identifier(\"variables\"),\n t.objectExpression(\n expressions.map((expression, i) => t.objectProperty(t.identifier(\"_\" + i), expression as t.Expression)),\n ),\n ),\n );\n }\n\n path.replaceWith(t.callExpression(t.identifier(name), [t.objectExpression(properties)]));\n },\n },\n };\n};\n"],"names":[],"mappings":";;;;;;AAuBA,cAAe,CAAC,EAAE,KAAK,EAAE,CAAC,EAAgB,EAAE,OAAgB;IAC1D,IAAI,MAAqB,CAAC;IAE1B,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;QACtC,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;KACvE;SAAM,IAAI,OAAO,CAAC,MAAM,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE;QAC/D,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;KACzB;SAAM,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ,EAAE;QAC5C,MAAM,GAAG,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;KAClE;SAAM,IAAI,OAAO,CAAC,KAAK,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ,EAA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C,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC,EAAE,UAA0B,CAAC,CAAC,CACxG,CACF,CACF,CAAC;iBACH;gBAED,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;aAC1F;SACF;KACF,CAAC;AACJ,CAAC;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mo36924/babel-plugin-graphql-tagged-template",
3
- "version": "1.6.1",
3
+ "version": "1.6.5",
4
4
  "description": "babel-plugin-graphql-tagged-template",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/mo36924/monorepo#readme",
@@ -37,14 +37,15 @@
37
37
  "dist"
38
38
  ],
39
39
  "dependencies": {
40
- "@babel/core": "^7.16.12",
41
- "@mo36924/base52": "^1.6.1",
42
- "@mo36924/graphql-schema": "^1.6.1",
43
- "@types/babel__core": "^7.1.18",
40
+ "@babel/core": "^7.17.8",
41
+ "@mo36924/graphql-build": "^1.6.5",
42
+ "@mo36924/graphql-config": "^1.6.5",
43
+ "@mo36924/graphql-model": "^1.6.5",
44
+ "@types/babel__core": "^7.1.19",
44
45
  "graphql": "^16.3.0"
45
46
  },
46
47
  "publishConfig": {
47
48
  "access": "public"
48
49
  },
49
- "gitHead": "6db82b81fcee91132bd8d1fae25b0fd429e31d41"
50
+ "gitHead": "d9f671459b3a8cacc9e9220d2c90ca4ba070ca63"
50
51
  }