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

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
@@ -7,5 +7,4 @@ declare type Options = {
7
7
  };
8
8
  declare const _default: ({ types: t }: typeof babel, options: Options) => PluginObj;
9
9
 
10
- export default _default;
11
- export { Options };
10
+ export { Options, _default as default };
package/dist/index.js CHANGED
@@ -1,107 +1,81 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const base52 = require('@mo36924/base52');
5
- const graphqlSchema = require('@mo36924/graphql-schema');
6
- const graphql = require('graphql');
7
-
8
- const index = (({
9
- types: t
10
- }, options) => {
11
- let schema;
12
-
13
- if (options.model) {
14
- const model = fs.readFileSync(options.model || "index.graphql", "utf8");
15
- schema = graphqlSchema.buildSchemaModel(model);
16
- } else if (typeof options.schema === "string") {
17
- const graphqlSchema$1 = fs.readFileSync(options.schema || "index.graphql", "utf8");
18
- schema = graphqlSchema.buildSchema(graphqlSchema$1);
19
- } else if (options.schema) {
20
- schema = options.schema;
21
- }
22
-
23
- return {
24
- visitor: {
25
- TaggedTemplateExpression(path) {
26
- const {
27
- tag,
28
- quasi: {
29
- quasis,
30
- expressions
31
- }
32
- } = path.node;
33
-
34
- if (!t.isIdentifier(tag)) {
35
- return;
36
- }
37
-
38
- const name = tag.name;
39
-
40
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
41
- return;
42
- }
43
-
44
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
45
-
46
- for (let i = 0; i < expressions.length; i++) {
47
- query += `$${base52.encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
48
- }
49
-
50
- if (name === "mutation" || name === "subscription") {
51
- query = name + query;
52
- }
53
-
54
- let documentNode;
55
-
56
- try {
57
- documentNode = graphql.parse(query);
58
- } catch (err) {
59
- throw path.buildCodeFrameError(String(err));
60
- }
61
-
62
- const values = [];
63
- const typeInfo = new graphql.TypeInfo(schema);
64
- graphql.visit(documentNode, graphql.visitWithTypeInfo(typeInfo, {
65
- Variable() {
66
- values.push(typeInfo.getInputType());
67
- }
68
-
69
- }));
70
-
71
- if (values.length) {
72
- const variables = `(${values.map((value, i) => `$${base52.encode(i)}:${value}`).join()})`;
73
-
74
- if (name === "query") {
75
- query = name + variables + query;
76
- } else if (name === "mutation" || name === "subscription") {
77
- query = name + variables + query.slice(name.length);
78
- }
79
- }
80
-
81
- try {
82
- documentNode = graphql.parse(query);
83
- } catch (err) {
84
- throw path.buildCodeFrameError(String(err));
85
- }
86
-
87
- const errors = graphql.validate(schema, documentNode);
88
-
89
- if (errors.length) {
90
- throw path.buildCodeFrameError(errors[0].message);
91
- }
92
-
93
- const args = [t.stringLiteral(graphql.stripIgnoredCharacters(query))];
94
-
95
- if (expressions.length) {
96
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(base52.encode(i)), expression))));
97
- }
98
-
99
- path.replaceWith(t.callExpression(t.identifier(name), args));
100
- }
101
-
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);
102
11
  }
103
- };
104
- });
105
-
106
- 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 };
107
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","model","readFileSync","buildSchemaModel","graphqlSchema","buildSchema","visitor","TaggedTemplateExpression","path","tag","quasi","quasis","expressions","node","isIdentifier","name","query","value","cooked","raw","i","length","encode","documentNode","parse","err","buildCodeFrameError","String","values","typeInfo","TypeInfo","visit","visitWithTypeInfo","Variable","push","getInputType","variables","map","join","slice","errors","validate","message","args","stringLiteral","stripIgnoredCharacters","objectExpression","expression","objectProperty","identifier","replaceWith","callExpression"],"mappings":";;;;;;;AAqBA,eAAe,CAAC;AAAEA,EAAAA,KAAK,EAAEC;AAAT,CAAD,EAA6BC,OAA7B;AACb,MAAIC,MAAJ;;AAEA,MAAID,OAAO,CAACE,KAAZ,EAAmB;AACjB,UAAMA,KAAK,GAAGC,eAAY,CAACH,OAAO,CAACE,KAAR,IAAiB,eAAlB,EAAmC,MAAnC,CAA1B;AACAD,IAAAA,MAAM,GAAGG,8BAAgB,CAACF,KAAD,CAAzB;AACD,GAHD,MAGO,IAAI,OAAOF,OAAO,CAACC,MAAf,KAA0B,QAA9B,EAAwC;AAC7C,UAAMI,eAAa,GA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,YAAIiB,MAAM,CAACnB,MAAX,EAAmB;AACjB,gBAAMb,IAAI,CAACkB,mBAAL,CAAyBc,MAAM,CAAC,CAAD,CAAN,CAAUE,OAAnC,CAAN;AACD;;AAED,cAAMC,IAAI,GAAmB,CAAC7C,CAAC,CAAC8C,aAAF,CAAgBC,8BAAsB,CAAC7B,KAAD,CAAtC,CAAD,CAA7B;;AAEA,YAAIJ,WAAW,CAACS,MAAhB,EAAwB;AACtBsB,UAAAA,IAAI,CAACT,IAAL,CACEpC,CAAC,CAACgD,gBAAF,CACElC,WAAW,CAACyB,GAAZ,CAAgB,CAACU,UAAD,EAAa3B,CAAb,KAAmBtB,CAAC,CAACkD,cAAF,CAAiBlD,CAAC,CAACmD,UAAF,CAAa3B,aAAM,CAACF,CAAD,CAAnB,CAAjB,EAA0C2B,UAA1C,CAAnC,CADF,CADF;AAKD;;AAEDvC,QAAAA,IAAI,CAAC0C,WAAL,CAAiBpD,CAAC,CAACqD,cAAF,CAAiBrD,CAAC,CAACmD,UAAF,CAAalC,IAAb,CAAjB,EAAqC4B,IAArC,CAAjB;AACD;;AAhFM;AADJ,GAAP;AAoFD,CAjGD;;;;"}
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;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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;;;;"}
package/dist/index.mjs CHANGED
@@ -3,103 +3,79 @@ import { encode } from '@mo36924/base52';
3
3
  import { buildSchemaModel, buildSchema } from '@mo36924/graphql-schema';
4
4
  import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
5
5
 
6
- const index = (({
7
- types: t
8
- }, options) => {
9
- let schema;
10
-
11
- if (options.model) {
12
- const model = readFileSync(options.model || "index.graphql", "utf8");
13
- schema = buildSchemaModel(model);
14
- } else if (typeof options.schema === "string") {
15
- const graphqlSchema = readFileSync(options.schema || "index.graphql", "utf8");
16
- schema = buildSchema(graphqlSchema);
17
- } else if (options.schema) {
18
- schema = options.schema;
19
- }
20
-
21
- return {
22
- visitor: {
23
- TaggedTemplateExpression(path) {
24
- const {
25
- tag,
26
- quasi: {
27
- quasis,
28
- expressions
29
- }
30
- } = path.node;
31
-
32
- if (!t.isIdentifier(tag)) {
33
- return;
34
- }
35
-
36
- const name = tag.name;
37
-
38
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
39
- return;
40
- }
41
-
42
- let query = quasis[0].value.cooked ?? quasis[0].value.raw;
43
-
44
- for (let i = 0; i < expressions.length; i++) {
45
- query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
46
- }
47
-
48
- if (name === "mutation" || name === "subscription") {
49
- query = name + query;
50
- }
51
-
52
- let documentNode;
53
-
54
- try {
55
- documentNode = parse(query);
56
- } catch (err) {
57
- throw path.buildCodeFrameError(String(err));
58
- }
59
-
60
- const values = [];
61
- const typeInfo = new TypeInfo(schema);
62
- visit(documentNode, visitWithTypeInfo(typeInfo, {
63
- Variable() {
64
- values.push(typeInfo.getInputType());
65
- }
66
-
67
- }));
68
-
69
- if (values.length) {
70
- const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
71
-
72
- if (name === "query") {
73
- query = name + variables + query;
74
- } else if (name === "mutation" || name === "subscription") {
75
- query = name + variables + query.slice(name.length);
76
- }
77
- }
78
-
79
- try {
80
- documentNode = parse(query);
81
- } catch (err) {
82
- throw path.buildCodeFrameError(String(err));
83
- }
84
-
85
- const errors = validate(schema, documentNode);
86
-
87
- if (errors.length) {
88
- throw path.buildCodeFrameError(errors[0].message);
89
- }
90
-
91
- const args = [t.stringLiteral(stripIgnoredCharacters(query))];
92
-
93
- if (expressions.length) {
94
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
95
- }
96
-
97
- path.replaceWith(t.callExpression(t.identifier(name), args));
98
- }
99
-
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);
100
11
  }
101
- };
102
- });
103
-
104
- 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 };
105
81
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":null,"names":["types","t","options","schema","model","readFileSync","buildSchemaModel","graphqlSchema","buildSchema","visitor","TaggedTemplateExpression","path","tag","quasi","quasis","expressions","node","isIdentifier","name","query","value","cooked","raw","i","length","encode","documentNode","parse","err","buildCodeFrameError","String","values","typeInfo","TypeInfo","visit","visitWithTypeInfo","Variable","push","getInputType","variables","map","join","slice","errors","validate","message","args","stringLiteral","stripIgnoredCharacters","objectExpression","expression","objectProperty","identifier","replaceWith","callExpression"],"mappings":";;;;;AAqBA,eAAe,CAAC;AAAEA,EAAAA,KAAK,EAAEC;AAAT,CAAD,EAA6BC,OAA7B;AACb,MAAIC,MAAJ;;AAEA,MAAID,OAAO,CAACE,KAAZ,EAAmB;AACjB,UAAMA,KAAK,GAAGC,YAAY,CAACH,OAAO,CAACE,KAAR,IAAiB,eAAlB,EAAmC,MAAnC,CAA1B;AACAD,IAAAA,MAAM,GAAGG,gBAAgB,CAACF,KAAD,CAAzB;AACD,GAHD,MAGO,IAAI,OAAOF,OAAO,CAACC,MAAf,KAA0B,QAA9B,EAAwC;AAC7C,UAAMI,aAAa,GAAGF,YAAY,CAACH,OAAO,CAACC,MAAR,IAAkB,eAAnB,EAAoC,MAApC,CAAlC;AACAA,IAAAA,MAAM,GAAGK,WAAW,CAACD,aAAD,CAApB;AACD,GAHM,MAGA,IAAIL,OAAO,CAACC,MAAZ,EAAoB;AACzBA,IAAAA,MAAM,GAAGD,OAAO,CAACC,MAAjB;AACD;;AAED,SAAO;AACLM,IAAAA,OAAO,EAAE;AACPC,MAAAA,wBAAwB,CAACC,IAAD;AACtB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ACT,IAAL,CACEpC,CAAC,CAACgD,gBAAF,CACElC,WAAW,CAACyB,GAAZ,CAAgB,CAACU,UAAD,EAAa3B,CAAb,KAAmBtB,CAAC,CAACkD,cAAF,CAAiBlD,CAAC,CAACmD,UAAF,CAAa3B,MAAM,CAACF,CAAD,CAAnB,CAAjB,EAA0C2B,UAA1C,CAAnC,CADF,CADF;AAKD;;AAEDvC,QAAAA,IAAI,CAAC0C,WAAL,CAAiBpD,CAAC,CAACqD,cAAF,CAAiBrD,CAAC,CAACmD,UAAF,CAAalC,IAAb,CAAjB,EAAqC4B,IAArC,CAAjB;AACD;;AAhFM;AADJ,GAAP;AAoFD,CAjGD;;;;"}
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,IAAI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AC;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.47",
3
+ "version": "1.5.0",
4
4
  "description": "babel-plugin-graphql-tagged-template",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/mo36924/monorepo#readme",
@@ -16,20 +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"
24
+ }
25
+ },
26
+ "main": "./index.cjs",
27
+ "module": "./index.mjs",
28
+ "typesVersions": {
29
+ "*": {
30
+ "*": [
31
+ "dist/*.d.ts"
32
+ ]
21
33
  }
22
34
  },
23
- "main": "./dist/index.js",
24
- "module": "./dist/index.mjs",
25
35
  "dependencies": {
26
36
  "@babel/core": "^7.15.0",
27
- "@mo36924/base52": "^1.4.0",
28
- "@mo36924/graphql-schema": "^1.4.42",
37
+ "@mo36924/base52": "^1.5.0",
38
+ "@mo36924/graphql-schema": "^1.5.0",
29
39
  "graphql": "^15.5.0"
30
40
  },
31
41
  "publishConfig": {
32
42
  "access": "public"
33
43
  },
34
- "gitHead": "ad5effba723836f25b2ca74e3e4d199b9c4b43ff"
44
+ "gitHead": "3998143a19e84538b9226e4617155e8f49ac5588"
35
45
  }