@mo36924/babel-plugin-graphql-tagged-template 1.4.42 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs ADDED
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+ var base52 = require('@mo36924/base52');
5
+ var graphqlSchema = require('@mo36924/graphql-schema');
6
+ var graphql = require('graphql');
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
+ }
21
+ return {
22
+ visitor: {
23
+ TaggedTemplateExpression(path) {
24
+ const { tag, quasi: { quasis, expressions }, } = path.node;
25
+ if (!t.isIdentifier(tag)) {
26
+ return;
27
+ }
28
+ const name = tag.name;
29
+ if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
30
+ return;
31
+ }
32
+ let query = quasis[0].value.cooked ?? quasis[0].value.raw;
33
+ 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;
38
+ }
39
+ let documentNode;
40
+ try {
41
+ documentNode = graphql.parse(query);
42
+ }
43
+ catch (err) {
44
+ throw path.buildCodeFrameError(String(err));
45
+ }
46
+ const values = [];
47
+ const typeInfo = new graphql.TypeInfo(schema);
48
+ graphql.visit(documentNode, graphql.visitWithTypeInfo(typeInfo, {
49
+ Variable() {
50
+ values.push(typeInfo.getInputType());
51
+ },
52
+ }));
53
+ if (values.length) {
54
+ const variables = `(${values.map((value, i) => `$${base52.encode(i)}:${value}`).join()})`;
55
+ if (name === "query") {
56
+ query = name + variables + query;
57
+ }
58
+ else if (name === "mutation" || name === "subscription") {
59
+ query = name + variables + query.slice(name.length);
60
+ }
61
+ }
62
+ try {
63
+ documentNode = graphql.parse(query);
64
+ }
65
+ catch (err) {
66
+ throw path.buildCodeFrameError(String(err));
67
+ }
68
+ const errors = graphql.validate(schema, documentNode);
69
+ if (errors.length) {
70
+ throw path.buildCodeFrameError(errors[0].message);
71
+ }
72
+ const args = [t.stringLiteral(graphql.stripIgnoredCharacters(query))];
73
+ if (expressions.length) {
74
+ args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(base52.encode(i)), expression))));
75
+ }
76
+ path.replaceWith(t.callExpression(t.identifier(name), args));
77
+ },
78
+ },
79
+ };
80
+ };
81
+
82
+ module.exports = index;
83
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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,eAA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AC,CAAC,CAAC;iBAC7C;gBAED,MAAM,MAAM,GAAGI,gBAAQ,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,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;;;;"}
package/dist/index.d.ts CHANGED
@@ -2,9 +2,9 @@ import babel, { PluginObj } from '@babel/core';
2
2
  import { GraphQLSchema } from 'graphql';
3
3
 
4
4
  declare type Options = {
5
+ model: string;
5
6
  schema: string | GraphQLSchema;
6
7
  };
7
8
  declare const _default: ({ types: t }: typeof babel, options: Options) => PluginObj;
8
9
 
9
- export default _default;
10
- export { Options };
10
+ export { Options, _default as default };
package/dist/index.js CHANGED
@@ -1,113 +1,81 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const graphqlSchema = require('@mo36924/graphql-schema');
5
- const graphql = require('graphql');
6
-
7
- const index = (({
8
- types: t
9
- }, options) => {
10
- let schema;
11
-
12
- if (typeof options.schema === "string") {
13
- const graphql = fs.readFileSync(options.schema || "index.graphql", "utf8");
14
- schema = graphqlSchema.buildSchemaModel(graphql);
15
- } else if (options.schema) {
16
- schema = options.schema;
17
- }
18
-
19
- return {
20
- visitor: {
21
- TaggedTemplateExpression(path) {
22
- const {
23
- tag,
24
- quasi: {
25
- quasis,
26
- expressions
27
- }
28
- } = path.node;
29
-
30
- if (!t.isIdentifier(tag)) {
31
- return;
32
- }
33
-
34
- const name = tag.name;
35
-
36
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "useQuery" && name !== "useMutation") {
37
- return;
38
- }
39
-
40
- if (name === "gql" && expressions.length) {
41
- throw path.buildCodeFrameError("gql invalid expressions.");
42
- }
43
-
44
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
45
- let variables = "";
46
-
47
- for (let i = 0; i < expressions.length; i++) {
48
- query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
49
- variables += `$_${i}:Unknown`;
50
- }
51
-
52
- if (name === "query" || name === "useQuery") {
53
- if (variables) {
54
- query = `query(${variables}){${query}}`;
55
- } else {
56
- query = `{${query}}`;
57
- }
58
- } else if (name === "mutation" || name === "useMutation") {
59
- if (variables) {
60
- query = `mutation(${variables}){${query}}`;
61
- } else {
62
- query = `mutation{${query}}`;
63
- }
64
- }
65
-
66
- let documentNode;
67
-
68
- try {
69
- query = graphql.stripIgnoredCharacters(query);
70
- documentNode = graphql.parse(query);
71
- } catch (err) {
72
- throw path.buildCodeFrameError(String(err));
73
- }
74
-
75
- let errors = graphql.validate(schema, documentNode);
76
-
77
- for (const error of errors) {
78
- const match = error.message.match(/^Variable ".*?" of type "Unknown" used in position expecting type "(.*?)"\.$/);
79
-
80
- if (match) {
81
- query = query.replace("Unknown", match[1]);
82
- } else {
83
- throw path.buildCodeFrameError(error.message);
84
- }
85
- }
86
-
87
- try {
88
- documentNode = graphql.parse(query);
89
- } catch (err) {
90
- throw path.buildCodeFrameError(String(err));
91
- }
92
-
93
- errors = graphql.validate(schema, documentNode);
94
-
95
- for (const error of errors) {
96
- throw path.buildCodeFrameError(error.message);
97
- }
98
-
99
- const args = [t.stringLiteral(query)];
100
-
101
- if (variables) {
102
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(`_${i}`), expression))));
103
- }
104
-
105
- path.replaceWith(t.callExpression(t.identifier(name), args));
106
- }
107
-
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';
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);
108
11
  }
109
- };
110
- });
111
-
112
- module.exports = index;
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
+ }
19
+ return {
20
+ visitor: {
21
+ TaggedTemplateExpression(path) {
22
+ const { tag, quasi: { quasis, expressions }, } = path.node;
23
+ if (!t.isIdentifier(tag)) {
24
+ return;
25
+ }
26
+ const name = tag.name;
27
+ if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
28
+ return;
29
+ }
30
+ let query = quasis[0].value.cooked ?? quasis[0].value.raw;
31
+ 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;
36
+ }
37
+ let documentNode;
38
+ try {
39
+ documentNode = parse(query);
40
+ }
41
+ catch (err) {
42
+ throw path.buildCodeFrameError(String(err));
43
+ }
44
+ const values = [];
45
+ const typeInfo = new TypeInfo(schema);
46
+ visit(documentNode, visitWithTypeInfo(typeInfo, {
47
+ Variable() {
48
+ values.push(typeInfo.getInputType());
49
+ },
50
+ }));
51
+ if (values.length) {
52
+ const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
53
+ if (name === "query") {
54
+ query = name + variables + query;
55
+ }
56
+ else if (name === "mutation" || name === "subscription") {
57
+ query = name + variables + query.slice(name.length);
58
+ }
59
+ }
60
+ try {
61
+ documentNode = parse(query);
62
+ }
63
+ catch (err) {
64
+ throw path.buildCodeFrameError(String(err));
65
+ }
66
+ const errors = validate(schema, documentNode);
67
+ if (errors.length) {
68
+ throw path.buildCodeFrameError(errors[0].message);
69
+ }
70
+ const args = [t.stringLiteral(stripIgnoredCharacters(query))];
71
+ if (expressions.length) {
72
+ args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
73
+ }
74
+ path.replaceWith(t.callExpression(t.identifier(name), args));
75
+ },
76
+ },
77
+ };
78
+ };
79
+
80
+ export { index as default };
113
81
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":null,"names":["types","t","options","schema","graphql","readFileSync","buildSchemaModel","visitor","TaggedTemplateExpression","path","tag","quasi","quasis","expressions","node","isIdentifier","name","length","buildCodeFrameError","query","value","cooked","raw","variables","i","documentNode","stripIgnoredCharacters","parse","err","String","errors","validate","error","match","message","replace","args","stringLiteral","push","objectExpression","map","expression","objectProperty","identifier","replaceWith","callExpression"],"mappings":";;;;;;AASA,eAAe,CAAC;AAAEA,EAAAA,KAAK,EAAEC;AAAT,CAAD,EAA6BC,OAA7B;AACb,MAAIC,MAAJ;;AAEA,MAAI,OAAOD,OAAO,CAACC,MAAf,KAA0B,QAA9B,EAAwC;AACtC,UAAMC,OAAO,GAAGC,eAAY,CAACH,OAAO,CAACC,MAAR,IAAkB,eAAnB,EAAoC,MAApC,CAA5B;AACAA,IAAAA,MAAM,GAAGG,8BAAgB,CAACF,OAAD,CAAzB;AACD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AAC5B,MAAD,EAASsB,YAAT,CAAjB;;AAEA,aAAK,MAAMO,KAAX,IAAoBF,MAApB,EAA4B;AAC1B,gBAAMrB,IAAI,CAACS,mBAAL,CAAyBc,KAAK,CAACE,OAA/B,CAAN;AACD;;AAED,cAAME,IAAI,GAAmB,CAACnC,CAAC,CAACoC,aAAF,CAAgBlB,KAAhB,CAAD,CAA7B;;AAEA,YAAII,SAAJ,EAAe;AACba,UAAAA,IAAI,CAACE,IAAL,CACErC,CAAC,CAACsC,gBAAF,CACE1B,WAAW,CAAC2B,GAAZ,CAAgB,CAACC,UAAD,EAAajB,CAAb,KAAmBvB,CAAC,CAACyC,cAAF,CAAiBzC,CAAC,CAAC0C,UAAF,KAAiBnB,GAAjB,CAAjB,EAAwCiB,UAAxC,CAAnC,CADF,CADF;AAKD;;AAEDhC,QAAAA,IAAI,CAACmC,WAAL,CAAiB3C,CAAC,CAAC4C,cAAF,CAAiB5C,CAAC,CAAC0C,UAAF,CAAa3B,IAAb,CAAjB,EAAqCoB,IAArC,CAAjB;AACD;;AA/FM;AADJ,GAAP;AAmGD,CA7GD;;;;"}
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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,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;;;;"}
package/dist/index.mjs CHANGED
@@ -1,111 +1,81 @@
1
1
  import { readFileSync } from 'fs';
2
- import { buildSchemaModel } from '@mo36924/graphql-schema';
3
- import { stripIgnoredCharacters, parse, validate } from 'graphql';
4
-
5
- const index = (({
6
- types: t
7
- }, options) => {
8
- let schema;
9
-
10
- if (typeof options.schema === "string") {
11
- const graphql = readFileSync(options.schema || "index.graphql", "utf8");
12
- schema = buildSchemaModel(graphql);
13
- } else if (options.schema) {
14
- schema = options.schema;
15
- }
16
-
17
- return {
18
- visitor: {
19
- TaggedTemplateExpression(path) {
20
- const {
21
- tag,
22
- quasi: {
23
- quasis,
24
- expressions
25
- }
26
- } = path.node;
27
-
28
- if (!t.isIdentifier(tag)) {
29
- return;
30
- }
31
-
32
- const name = tag.name;
33
-
34
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "useQuery" && name !== "useMutation") {
35
- return;
36
- }
37
-
38
- if (name === "gql" && expressions.length) {
39
- throw path.buildCodeFrameError("gql invalid expressions.");
40
- }
41
-
42
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
43
- let variables = "";
44
-
45
- for (let i = 0; i < expressions.length; i++) {
46
- query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
47
- variables += `$_${i}:Unknown`;
48
- }
49
-
50
- if (name === "query" || name === "useQuery") {
51
- if (variables) {
52
- query = `query(${variables}){${query}}`;
53
- } else {
54
- query = `{${query}}`;
55
- }
56
- } else if (name === "mutation" || name === "useMutation") {
57
- if (variables) {
58
- query = `mutation(${variables}){${query}}`;
59
- } else {
60
- query = `mutation{${query}}`;
61
- }
62
- }
63
-
64
- let documentNode;
65
-
66
- try {
67
- query = stripIgnoredCharacters(query);
68
- documentNode = parse(query);
69
- } catch (err) {
70
- throw path.buildCodeFrameError(String(err));
71
- }
72
-
73
- let errors = validate(schema, documentNode);
74
-
75
- for (const error of errors) {
76
- const match = error.message.match(/^Variable ".*?" of type "Unknown" used in position expecting type "(.*?)"\.$/);
77
-
78
- if (match) {
79
- query = query.replace("Unknown", match[1]);
80
- } else {
81
- throw path.buildCodeFrameError(error.message);
82
- }
83
- }
84
-
85
- try {
86
- documentNode = parse(query);
87
- } catch (err) {
88
- throw path.buildCodeFrameError(String(err));
89
- }
90
-
91
- errors = validate(schema, documentNode);
92
-
93
- for (const error of errors) {
94
- throw path.buildCodeFrameError(error.message);
95
- }
96
-
97
- const args = [t.stringLiteral(query)];
98
-
99
- if (variables) {
100
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(`_${i}`), expression))));
101
- }
102
-
103
- path.replaceWith(t.callExpression(t.identifier(name), args));
104
- }
105
-
2
+ import { encode } from '@mo36924/base52';
3
+ import { buildSchemaModel, buildSchema } from '@mo36924/graphql-schema';
4
+ import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
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);
106
11
  }
107
- };
108
- });
109
-
110
- export default index;
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
+ }
19
+ return {
20
+ visitor: {
21
+ TaggedTemplateExpression(path) {
22
+ const { tag, quasi: { quasis, expressions }, } = path.node;
23
+ if (!t.isIdentifier(tag)) {
24
+ return;
25
+ }
26
+ const name = tag.name;
27
+ if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
28
+ return;
29
+ }
30
+ let query = quasis[0].value.cooked ?? quasis[0].value.raw;
31
+ 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;
36
+ }
37
+ let documentNode;
38
+ try {
39
+ documentNode = parse(query);
40
+ }
41
+ catch (err) {
42
+ throw path.buildCodeFrameError(String(err));
43
+ }
44
+ const values = [];
45
+ const typeInfo = new TypeInfo(schema);
46
+ visit(documentNode, visitWithTypeInfo(typeInfo, {
47
+ Variable() {
48
+ values.push(typeInfo.getInputType());
49
+ },
50
+ }));
51
+ if (values.length) {
52
+ const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
53
+ if (name === "query") {
54
+ query = name + variables + query;
55
+ }
56
+ else if (name === "mutation" || name === "subscription") {
57
+ query = name + variables + query.slice(name.length);
58
+ }
59
+ }
60
+ try {
61
+ documentNode = parse(query);
62
+ }
63
+ catch (err) {
64
+ throw path.buildCodeFrameError(String(err));
65
+ }
66
+ const errors = validate(schema, documentNode);
67
+ if (errors.length) {
68
+ throw path.buildCodeFrameError(errors[0].message);
69
+ }
70
+ const args = [t.stringLiteral(stripIgnoredCharacters(query))];
71
+ if (expressions.length) {
72
+ args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
73
+ }
74
+ path.replaceWith(t.callExpression(t.identifier(name), args));
75
+ },
76
+ },
77
+ };
78
+ };
79
+
80
+ export { index as default };
111
81
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":null,"names":["types","t","options","schema","graphql","readFileSync","buildSchemaModel","visitor","TaggedTemplateExpression","path","tag","quasi","quasis","expressions","node","isIdentifier","name","length","buildCodeFrameError","query","value","cooked","raw","variables","i","documentNode","stripIgnoredCharacters","parse","err","String","errors","validate","error","match","message","replace","args","stringLiteral","push","objectExpression","map","expression","objectProperty","identifier","replaceWith","callExpression"],"mappings":";;;;AASA,eAAe,CAAC;AAAEA,EAAAA,KAAK,EAAEC;AAAT,CAAD,EAA6BC,OAA7B;AACb,MAAIC,MAAJ;;AAEA,MAAI,OAAOD,OAAO,CAACC,MAAf,KAA0B,QAA9B,EAAwC;AACtC,UAAMC,OAAO,GAAGC,YAAY,CAACH,OAAO,CAACC,MAAR,IAAkB,eAAnB,EAAoC,MAApC,CAA5B;AACAA,IAAAA,MAAM,GAAGG,gBAAgB,CAACF,OAAD,CAAzB;AACD,GAHD,MAGO,IAAIF,OAAO,CAACC,MAAZ,EAAoB;AACzBA,IAAAA,MAAM,GAAGD,OAAO,CAACC,MAAjB;AACD;;AAED,SAAO;AACLI,IAAAA,OAAO,EAAE;AACPC,MAAAA,wBAAwB,CAACC,IAAD;AACtB,cAAM;AACJC,UAAAA,GADI;AAEJC,UAAAA,KAAK,EAAE;AAAEC,YAAAA,MAAF;AAAUC,YAAAA;AA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ACba,UAAAA,IAAI,CAACE,IAAL,CACErC,CAAC,CAACsC,gBAAF,CACE1B,WAAW,CAAC2B,GAAZ,CAAgB,CAACC,UAAD,EAAajB,CAAb,KAAmBvB,CAAC,CAACyC,cAAF,CAAiBzC,CAAC,CAAC0C,UAAF,KAAiBnB,GAAjB,CAAjB,EAAwCiB,UAAxC,CAAnC,CADF,CADF;AAKD;;AAEDhC,QAAAA,IAAI,CAACmC,WAAL,CAAiB3C,CAAC,CAAC4C,cAAF,CAAiB5C,CAAC,CAAC0C,UAAF,CAAa3B,IAAb,CAAjB,EAAqCoB,IAArC,CAAjB;AACD;;AA/FM;AADJ,GAAP;AAmGD,CA7GD;;;;"}
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;QAC7C,MAAM,aAAa,GAAG,YAAY,CAAC,OAAO,CAAC,MAAM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AC,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;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mo36924/babel-plugin-graphql-tagged-template",
3
- "version": "1.4.42",
3
+ "version": "1.5.2",
4
4
  "description": "babel-plugin-graphql-tagged-template",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/mo36924/monorepo#readme",
@@ -16,19 +16,30 @@
16
16
  "author": "mo36924 <mo36924@users.noreply.github.com>",
17
17
  "exports": {
18
18
  ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "browser": "./dist/index.js",
19
21
  "import": "./dist/index.mjs",
20
- "require": "./dist/index.js"
22
+ "require": "./dist/index.cjs",
23
+ "default": "./dist/index.cjs"
21
24
  }
22
25
  },
23
- "main": "./dist/index.js",
26
+ "main": "./dist/index.cjs",
24
27
  "module": "./dist/index.mjs",
28
+ "typesVersions": {
29
+ "*": {
30
+ "*": [
31
+ "dist/*.d.ts"
32
+ ]
33
+ }
34
+ },
25
35
  "dependencies": {
26
- "@babel/core": "^7.15.0",
27
- "@mo36924/graphql-schema": "^1.4.42",
28
- "graphql": "^15.5.0"
36
+ "@babel/core": "^7.16.12",
37
+ "@mo36924/base52": "^1.5.2",
38
+ "@mo36924/graphql-schema": "^1.5.2",
39
+ "graphql": "^16.3.0"
29
40
  },
30
41
  "publishConfig": {
31
42
  "access": "public"
32
43
  },
33
- "gitHead": "0097eee4454a2cdc97498eb90e01d9884c1aaa0c"
44
+ "gitHead": "f0f9d902e7699b51567fac98cc7f39de8aae3643"
34
45
  }
package/src/index.test.ts CHANGED
@@ -52,35 +52,19 @@ describe("babel-plugin-graphql-tagged-template", () => {
52
52
  test("query", () => {
53
53
  const result = transform(`
54
54
  const offset = 2
55
- query\`
56
- user(offset: \${offset}) {
57
- name
58
- }
59
- \`
60
- `);
61
-
62
- expect(result).toMatchInlineSnapshot(`
63
- const offset = 2;
64
- query("query($_0:Int){user(offset:$_0){name}}", {
65
- _0: offset,
66
- });
67
- `);
68
- });
69
-
70
- test("useQuery", () => {
71
- const result = transform(`
72
- const offset = 2
73
- useQuery\`
74
- user(offset: \${offset}) {
75
- name
55
+ query\`
56
+ {
57
+ user(offset: \${offset}) {
58
+ name
59
+ }
76
60
  }
77
61
  \`
78
62
  `);
79
63
 
80
64
  expect(result).toMatchInlineSnapshot(`
81
65
  const offset = 2;
82
- useQuery("query($_0:Int){user(offset:$_0){name}}", {
83
- _0: offset,
66
+ query("query($a:Int){user(offset:$a){name}}", {
67
+ a: offset,
84
68
  });
85
69
  `);
86
70
  });
@@ -89,34 +73,18 @@ describe("babel-plugin-graphql-tagged-template", () => {
89
73
  const result = transform(`
90
74
  const name = "hoge";
91
75
  mutation\`
92
- create(name: \${name}) {
93
- name
94
- }
95
- \`
96
- `);
97
-
98
- expect(result).toMatchInlineSnapshot(`
99
- const name = "hoge";
100
- mutation("mutation($_0:String){create(name:$_0){name}}", {
101
- _0: name,
102
- });
103
- `);
104
- });
105
-
106
- test("useMutation", () => {
107
- const result = transform(`
108
- const name = "hoge";
109
- useMutation\`
110
- create(name: \${name}) {
111
- name
76
+ {
77
+ create(name: \${name}) {
78
+ name
79
+ }
112
80
  }
113
81
  \`
114
82
  `);
115
83
 
116
84
  expect(result).toMatchInlineSnapshot(`
117
85
  const name = "hoge";
118
- useMutation("mutation($_0:String){create(name:$_0){name}}", {
119
- _0: name,
86
+ mutation("mutation($a:String){create(name:$a){name}}", {
87
+ a: name,
120
88
  });
121
89
  `);
122
90
  });
package/src/index.ts CHANGED
@@ -1,18 +1,33 @@
1
1
  import { readFileSync } from "fs";
2
2
  import type { default as babel, PluginObj, types as t } from "@babel/core";
3
- import { buildSchemaModel } from "@mo36924/graphql-schema";
4
- import { DocumentNode, GraphQLSchema, parse, stripIgnoredCharacters, validate } from "graphql";
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";
5
16
 
6
17
  export type Options = {
18
+ model: string;
7
19
  schema: string | GraphQLSchema;
8
20
  };
9
21
 
10
22
  export default ({ types: t }: typeof babel, options: Options): PluginObj => {
11
23
  let schema: GraphQLSchema;
12
24
 
13
- if (typeof options.schema === "string") {
14
- const graphql = readFileSync(options.schema || "index.graphql", "utf8");
15
- schema = buildSchemaModel(graphql);
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);
16
31
  } else if (options.schema) {
17
32
  schema = options.schema;
18
33
  }
@@ -31,62 +46,47 @@ export default ({ types: t }: typeof babel, options: Options): PluginObj => {
31
46
 
32
47
  const name = tag.name;
33
48
 
34
- if (
35
- name !== "gql" &&
36
- name !== "query" &&
37
- name !== "mutation" &&
38
- name !== "useQuery" &&
39
- name !== "useMutation"
40
- ) {
49
+ if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
41
50
  return;
42
51
  }
43
52
 
44
- if (name === "gql" && expressions.length) {
45
- throw path.buildCodeFrameError("gql invalid expressions.");
46
- }
47
-
48
53
  let query = quasis[0].value.cooked ?? quasis[0].value.raw;
49
- let variables = "";
50
54
 
51
55
  for (let i = 0; i < expressions.length; i++) {
52
- query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
53
- variables += `$_${i}:Unknown`;
56
+ query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
54
57
  }
55
58
 
56
- if (name === "query" || name === "useQuery") {
57
- if (variables) {
58
- query = `query(${variables}){${query}}`;
59
- } else {
60
- query = `{${query}}`;
61
- }
62
- } else if (name === "mutation" || name === "useMutation") {
63
- if (variables) {
64
- query = `mutation(${variables}){${query}}`;
65
- } else {
66
- query = `mutation{${query}}`;
67
- }
59
+ if (name === "mutation" || name === "subscription") {
60
+ query = name + query;
68
61
  }
69
62
 
70
63
  let documentNode: DocumentNode;
71
64
 
72
65
  try {
73
- query = stripIgnoredCharacters(query);
74
66
  documentNode = parse(query);
75
67
  } catch (err) {
76
68
  throw path.buildCodeFrameError(String(err));
77
69
  }
78
70
 
79
- let errors = validate(schema, documentNode);
80
-
81
- for (const error of errors) {
82
- const match = error.message.match(
83
- /^Variable ".*?" of type "Unknown" used in position expecting type "(.*?)"\.$/,
84
- );
85
-
86
- if (match) {
87
- query = query.replace("Unknown", match[1]);
88
- } else {
89
- throw path.buildCodeFrameError(error.message);
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
90
  }
91
91
  }
92
92
 
@@ -96,18 +96,18 @@ export default ({ types: t }: typeof babel, options: Options): PluginObj => {
96
96
  throw path.buildCodeFrameError(String(err));
97
97
  }
98
98
 
99
- errors = validate(schema, documentNode);
99
+ const errors = validate(schema, documentNode);
100
100
 
101
- for (const error of errors) {
102
- throw path.buildCodeFrameError(error.message);
101
+ if (errors.length) {
102
+ throw path.buildCodeFrameError(errors[0].message);
103
103
  }
104
104
 
105
- const args: t.Expression[] = [t.stringLiteral(query)];
105
+ const args: t.Expression[] = [t.stringLiteral(stripIgnoredCharacters(query))];
106
106
 
107
- if (variables) {
107
+ if (expressions.length) {
108
108
  args.push(
109
109
  t.objectExpression(
110
- expressions.map((expression, i) => t.objectProperty(t.identifier(`_${i}`), expression as any)),
110
+ expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression as any)),
111
111
  ),
112
112
  );
113
113
  }