@mo36924/babel-plugin-graphql-tagged-template 1.6.11 → 5.0.47
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +1 -1
- package/dist/index.cjs +53 -76
- package/dist/index.d.ts +6 -8
- package/dist/index.js +52 -75
- package/package.json +23 -24
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -108
- package/dist/index.mjs.map +0 -1
package/LICENSE
CHANGED
package/dist/index.cjs
CHANGED
@@ -1,110 +1,87 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
const graphqlModel = require('@mo36924/graphql-model');
|
7
|
-
const graphql = require('graphql');
|
3
|
+
var core = require('@babel/core');
|
4
|
+
var helperPluginUtils = require('@babel/helper-plugin-utils');
|
5
|
+
var graphql = require('graphql');
|
8
6
|
|
9
|
-
|
10
|
-
let schema;
|
11
|
-
if (typeof options.schema === "string") {
|
12
|
-
schema = graphqlBuild.buildASTSchema(graphql.parse(fs.readFileSync(options.schema, "utf-8")));
|
13
|
-
}
|
14
|
-
else if (options.schema && typeof options.schema === "object") {
|
15
|
-
schema = options.schema;
|
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
|
-
}
|
7
|
+
var index = helperPluginUtils.declare((_, { schema })=>{
|
26
8
|
return {
|
27
|
-
name: "graphql-tagged-template",
|
9
|
+
name: "babel-plugin-graphql-tagged-template",
|
28
10
|
visitor: {
|
29
|
-
TaggedTemplateExpression(path) {
|
30
|
-
const { tag, quasi: { quasis, expressions }
|
31
|
-
if (!
|
11
|
+
TaggedTemplateExpression (path) {
|
12
|
+
const { tag, quasi: { quasis, expressions } } = path.node;
|
13
|
+
if (!core.types.isIdentifier(tag)) {
|
32
14
|
return;
|
33
15
|
}
|
34
16
|
const name = tag.name;
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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;
|
17
|
+
if (name !== "gql") {
|
18
|
+
return;
|
19
|
+
}
|
20
|
+
let query = quasis[0].value.cooked ?? quasis[0].value.raw;
|
21
|
+
if (query.trimStart()[0] !== "{") {
|
22
|
+
throw path.buildCodeFrameError("Named operation are not allowed");
|
51
23
|
}
|
52
|
-
let
|
53
|
-
for (let i = 0; i < expressions.length; i++) {
|
24
|
+
for(let i = 0; i < expressions.length; i++){
|
54
25
|
query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
|
55
26
|
}
|
56
27
|
let documentNode;
|
57
28
|
try {
|
58
29
|
documentNode = graphql.parse(query);
|
59
|
-
}
|
60
|
-
catch (err) {
|
30
|
+
} catch (err) {
|
61
31
|
throw path.buildCodeFrameError(String(err));
|
62
32
|
}
|
33
|
+
const definitions = documentNode.definitions;
|
34
|
+
const definition = definitions[0];
|
35
|
+
if (definitions.length !== 1 || definition.kind !== "OperationDefinition") {
|
36
|
+
throw path.buildCodeFrameError("Only a single operation is allowed");
|
37
|
+
}
|
38
|
+
if (definition.variableDefinitions?.length) {
|
39
|
+
throw path.buildCodeFrameError("Variables are not allowed");
|
40
|
+
}
|
41
|
+
const field = definition.selectionSet.selections[0];
|
42
|
+
if (field.kind !== "Field") {
|
43
|
+
throw path.buildCodeFrameError("Only a field is allowed");
|
44
|
+
}
|
45
|
+
const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
|
46
|
+
const operation = isMutation ? "mutation" : "query";
|
47
|
+
// @ts-expect-error ignore readonly
|
48
|
+
definition.operation = operation;
|
63
49
|
const values = [];
|
64
50
|
const typeInfo = new graphql.TypeInfo(schema);
|
65
|
-
graphql.visit(
|
66
|
-
Variable() {
|
51
|
+
graphql.visit(definition, graphql.visitWithTypeInfo(typeInfo, {
|
52
|
+
Variable () {
|
67
53
|
values.push(typeInfo.getInputType());
|
68
|
-
}
|
54
|
+
}
|
69
55
|
}));
|
70
56
|
if (values.length) {
|
71
|
-
const variables = `(${values.map((value, i)
|
72
|
-
|
73
|
-
|
74
|
-
}
|
75
|
-
else if (name !== "gql") {
|
76
|
-
query = "query" + variables + query;
|
77
|
-
}
|
57
|
+
const variables = `(${values.map((value, i)=>`$_${i}:${value}`).join()})`;
|
58
|
+
query = `${operation}${variables}${query}`;
|
59
|
+
} else if (operation === "mutation") {
|
60
|
+
query = `${operation}${query}`;
|
78
61
|
}
|
79
62
|
try {
|
80
|
-
documentNode = graphql.parse(query, {
|
81
|
-
|
82
|
-
|
63
|
+
documentNode = graphql.parse(query, {
|
64
|
+
noLocation: true
|
65
|
+
});
|
66
|
+
} catch (err) {
|
83
67
|
throw path.buildCodeFrameError(String(err));
|
84
68
|
}
|
85
69
|
const errors = graphql.validate(schema, documentNode);
|
86
70
|
if (errors.length) {
|
87
71
|
throw path.buildCodeFrameError(errors[0].message);
|
88
72
|
}
|
89
|
-
const properties = [
|
90
|
-
|
91
|
-
|
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
|
+
const properties = [
|
74
|
+
core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(graphql.stripIgnoredCharacters(query)))
|
75
|
+
];
|
100
76
|
if (expressions.length) {
|
101
|
-
properties.push(
|
77
|
+
properties.push(core.types.objectProperty(core.types.identifier("variables"), core.types.objectExpression(expressions.map((expression, i)=>core.types.objectProperty(core.types.identifier(`_${i}`), expression)))));
|
102
78
|
}
|
103
|
-
path.replaceWith(
|
104
|
-
|
105
|
-
|
79
|
+
path.replaceWith(core.types.callExpression(core.types.identifier(name), [
|
80
|
+
core.types.objectExpression(properties)
|
81
|
+
]));
|
82
|
+
}
|
83
|
+
}
|
106
84
|
};
|
107
|
-
};
|
85
|
+
});
|
108
86
|
|
109
87
|
module.exports = index;
|
110
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.d.ts
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
import
|
2
|
-
import {
|
1
|
+
import * as _babel_core from '@babel/core';
|
2
|
+
import { GraphQLSchema } from 'graphql';
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
model?: string | DocumentNode;
|
7
|
-
schema?: string | GraphQLSchema;
|
4
|
+
type Options = {
|
5
|
+
schema: GraphQLSchema;
|
8
6
|
};
|
9
|
-
declare const _default: (
|
7
|
+
declare const _default: (api: object, options: Options | null | undefined, dirname: string) => _babel_core.PluginObj<_babel_core.PluginPass>;
|
10
8
|
|
11
|
-
export { Options, _default as default };
|
9
|
+
export { type Options, _default as default };
|
package/dist/index.js
CHANGED
@@ -1,108 +1,85 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import { config } from '@mo36924/graphql-config';
|
4
|
-
import { buildModel } from '@mo36924/graphql-model';
|
1
|
+
import { types } from '@babel/core';
|
2
|
+
import { declare } from '@babel/helper-plugin-utils';
|
5
3
|
import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
|
6
4
|
|
7
|
-
|
8
|
-
let schema;
|
9
|
-
if (typeof options.schema === "string") {
|
10
|
-
schema = buildASTSchema(parse(readFileSync(options.schema, "utf-8")));
|
11
|
-
}
|
12
|
-
else if (options.schema && typeof options.schema === "object") {
|
13
|
-
schema = options.schema;
|
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
|
-
}
|
5
|
+
var index = declare((_, { schema })=>{
|
24
6
|
return {
|
25
|
-
name: "graphql-tagged-template",
|
7
|
+
name: "babel-plugin-graphql-tagged-template",
|
26
8
|
visitor: {
|
27
|
-
TaggedTemplateExpression(path) {
|
28
|
-
const { tag, quasi: { quasis, expressions }
|
29
|
-
if (!
|
9
|
+
TaggedTemplateExpression (path) {
|
10
|
+
const { tag, quasi: { quasis, expressions } } = path.node;
|
11
|
+
if (!types.isIdentifier(tag)) {
|
30
12
|
return;
|
31
13
|
}
|
32
14
|
const name = tag.name;
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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;
|
15
|
+
if (name !== "gql") {
|
16
|
+
return;
|
17
|
+
}
|
18
|
+
let query = quasis[0].value.cooked ?? quasis[0].value.raw;
|
19
|
+
if (query.trimStart()[0] !== "{") {
|
20
|
+
throw path.buildCodeFrameError("Named operation are not allowed");
|
49
21
|
}
|
50
|
-
let
|
51
|
-
for (let i = 0; i < expressions.length; i++) {
|
22
|
+
for(let i = 0; i < expressions.length; i++){
|
52
23
|
query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
|
53
24
|
}
|
54
25
|
let documentNode;
|
55
26
|
try {
|
56
27
|
documentNode = parse(query);
|
57
|
-
}
|
58
|
-
catch (err) {
|
28
|
+
} catch (err) {
|
59
29
|
throw path.buildCodeFrameError(String(err));
|
60
30
|
}
|
31
|
+
const definitions = documentNode.definitions;
|
32
|
+
const definition = definitions[0];
|
33
|
+
if (definitions.length !== 1 || definition.kind !== "OperationDefinition") {
|
34
|
+
throw path.buildCodeFrameError("Only a single operation is allowed");
|
35
|
+
}
|
36
|
+
if (definition.variableDefinitions?.length) {
|
37
|
+
throw path.buildCodeFrameError("Variables are not allowed");
|
38
|
+
}
|
39
|
+
const field = definition.selectionSet.selections[0];
|
40
|
+
if (field.kind !== "Field") {
|
41
|
+
throw path.buildCodeFrameError("Only a field is allowed");
|
42
|
+
}
|
43
|
+
const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
|
44
|
+
const operation = isMutation ? "mutation" : "query";
|
45
|
+
// @ts-expect-error ignore readonly
|
46
|
+
definition.operation = operation;
|
61
47
|
const values = [];
|
62
48
|
const typeInfo = new TypeInfo(schema);
|
63
|
-
visit(
|
64
|
-
Variable() {
|
49
|
+
visit(definition, visitWithTypeInfo(typeInfo, {
|
50
|
+
Variable () {
|
65
51
|
values.push(typeInfo.getInputType());
|
66
|
-
}
|
52
|
+
}
|
67
53
|
}));
|
68
54
|
if (values.length) {
|
69
|
-
const variables = `(${values.map((value, i)
|
70
|
-
|
71
|
-
|
72
|
-
}
|
73
|
-
else if (name !== "gql") {
|
74
|
-
query = "query" + variables + query;
|
75
|
-
}
|
55
|
+
const variables = `(${values.map((value, i)=>`$_${i}:${value}`).join()})`;
|
56
|
+
query = `${operation}${variables}${query}`;
|
57
|
+
} else if (operation === "mutation") {
|
58
|
+
query = `${operation}${query}`;
|
76
59
|
}
|
77
60
|
try {
|
78
|
-
documentNode = parse(query, {
|
79
|
-
|
80
|
-
|
61
|
+
documentNode = parse(query, {
|
62
|
+
noLocation: true
|
63
|
+
});
|
64
|
+
} catch (err) {
|
81
65
|
throw path.buildCodeFrameError(String(err));
|
82
66
|
}
|
83
67
|
const errors = validate(schema, documentNode);
|
84
68
|
if (errors.length) {
|
85
69
|
throw path.buildCodeFrameError(errors[0].message);
|
86
70
|
}
|
87
|
-
const properties = [
|
88
|
-
|
89
|
-
|
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
|
+
const properties = [
|
72
|
+
types.objectProperty(types.identifier("query"), types.stringLiteral(stripIgnoredCharacters(query)))
|
73
|
+
];
|
98
74
|
if (expressions.length) {
|
99
|
-
properties.push(
|
75
|
+
properties.push(types.objectProperty(types.identifier("variables"), types.objectExpression(expressions.map((expression, i)=>types.objectProperty(types.identifier(`_${i}`), expression)))));
|
100
76
|
}
|
101
|
-
path.replaceWith(
|
102
|
-
|
103
|
-
|
77
|
+
path.replaceWith(types.callExpression(types.identifier(name), [
|
78
|
+
types.objectExpression(properties)
|
79
|
+
]));
|
80
|
+
}
|
81
|
+
}
|
104
82
|
};
|
105
|
-
};
|
83
|
+
});
|
106
84
|
|
107
85
|
export { index as default };
|
108
|
-
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
@@ -1,31 +1,34 @@
|
|
1
1
|
{
|
2
2
|
"name": "@mo36924/babel-plugin-graphql-tagged-template",
|
3
|
-
"
|
3
|
+
"type": "module",
|
4
|
+
"version": "5.0.47",
|
4
5
|
"description": "babel-plugin-graphql-tagged-template",
|
5
|
-
"
|
6
|
-
"
|
7
|
-
"
|
8
|
-
"url": "https://github.com/mo36924/monorepo/issues"
|
9
|
-
},
|
6
|
+
"author": "mo36924",
|
7
|
+
"license": "MIT",
|
8
|
+
"homepage": "https://github.com/mo36924/packages#readme",
|
10
9
|
"repository": {
|
11
10
|
"type": "git",
|
12
|
-
"url": "git+https://github.com/mo36924/
|
11
|
+
"url": "git+https://github.com/mo36924/packages.git",
|
13
12
|
"directory": "packages/babel-plugin-graphql-tagged-template"
|
14
13
|
},
|
15
|
-
"
|
16
|
-
|
14
|
+
"bugs": {
|
15
|
+
"url": "https://github.com/mo36924/packages/issues"
|
16
|
+
},
|
17
|
+
"keywords": [],
|
17
18
|
"exports": {
|
18
19
|
".": {
|
19
20
|
"types": "./dist/index.d.ts",
|
20
|
-
"
|
21
|
-
"import": "./dist/index.mjs",
|
21
|
+
"import": "./dist/index.js",
|
22
22
|
"require": "./dist/index.cjs",
|
23
|
-
"default": "./dist/index.
|
23
|
+
"default": "./dist/index.js"
|
24
24
|
}
|
25
25
|
},
|
26
|
-
"main": "./dist/index.
|
27
|
-
"module": "./dist/index.
|
26
|
+
"main": "./dist/index.js",
|
27
|
+
"module": "./dist/index.js",
|
28
28
|
"types": "./dist/index.d.ts",
|
29
|
+
"publishConfig": {
|
30
|
+
"access": "public"
|
31
|
+
},
|
29
32
|
"typesVersions": {
|
30
33
|
"*": {
|
31
34
|
"*": [
|
@@ -38,15 +41,11 @@
|
|
38
41
|
"dist"
|
39
42
|
],
|
40
43
|
"dependencies": {
|
41
|
-
"@babel/core": "^7.
|
42
|
-
"@
|
43
|
-
"@
|
44
|
-
"@
|
45
|
-
"
|
46
|
-
"graphql": "^16.3.0"
|
47
|
-
},
|
48
|
-
"publishConfig": {
|
49
|
-
"access": "public"
|
44
|
+
"@babel/core": "^7.25.2",
|
45
|
+
"@babel/helper-plugin-utils": "^7.24.8",
|
46
|
+
"@types/babel__core": "^7.20.5",
|
47
|
+
"@types/babel__helper-plugin-utils": "^7.10.3",
|
48
|
+
"graphql": "^16.9.0"
|
50
49
|
},
|
51
|
-
"gitHead": "
|
50
|
+
"gitHead": "e8a97dd132ef7998c0ef5d305d3b8d93c983ff32"
|
52
51
|
}
|
package/dist/index.cjs.map
DELETED
@@ -1 +0,0 @@
|
|
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,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AAE;wBAChF,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;qBAC9C,CAAC,CACH,CACF,CACF,CAAC;iBACH;qBAAM;oBACL,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,aAAa,CAACC,8BAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;iBAC1G;gBAED,IAAI,WAAW,CAAC,MAAM,EAAE;oBACtB,UAAU,CAAC,IAAI,CACb,CAAC,CAAC,cAAc,CACd,CAAC,CAAC,UAAU,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.js.map
DELETED
@@ -1 +0,0 @@
|
|
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,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;KAClE;SAAM,IAAI,OAAO,CAAC,KAAK,IAAI,OAAO,OAAO,CAAC,KAAK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C,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
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
import { readFileSync } from 'fs';
|
2
|
-
import { buildASTSchema } from '@mo36924/graphql-build';
|
3
|
-
import { config } from '@mo36924/graphql-config';
|
4
|
-
import { buildModel } from '@mo36924/graphql-model';
|
5
|
-
import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
|
6
|
-
|
7
|
-
const index = ({ types: t }, options) => {
|
8
|
-
let schema;
|
9
|
-
if (typeof options.schema === "string") {
|
10
|
-
schema = buildASTSchema(parse(readFileSync(options.schema, "utf-8")));
|
11
|
-
}
|
12
|
-
else if (options.schema && typeof options.schema === "object") {
|
13
|
-
schema = options.schema;
|
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
|
-
}
|
24
|
-
return {
|
25
|
-
name: "graphql-tagged-template",
|
26
|
-
visitor: {
|
27
|
-
TaggedTemplateExpression(path) {
|
28
|
-
const { tag, quasi: { quasis, expressions }, } = path.node;
|
29
|
-
if (!t.isIdentifier(tag)) {
|
30
|
-
return;
|
31
|
-
}
|
32
|
-
const name = tag.name;
|
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;
|
49
|
-
}
|
50
|
-
let query = operation + (quasis[0].value.cooked ?? quasis[0].value.raw);
|
51
|
-
for (let i = 0; i < expressions.length; i++) {
|
52
|
-
query += `$_${i}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
|
53
|
-
}
|
54
|
-
let documentNode;
|
55
|
-
try {
|
56
|
-
documentNode = parse(query);
|
57
|
-
}
|
58
|
-
catch (err) {
|
59
|
-
throw path.buildCodeFrameError(String(err));
|
60
|
-
}
|
61
|
-
const values = [];
|
62
|
-
const typeInfo = new TypeInfo(schema);
|
63
|
-
visit(documentNode, visitWithTypeInfo(typeInfo, {
|
64
|
-
Variable() {
|
65
|
-
values.push(typeInfo.getInputType());
|
66
|
-
},
|
67
|
-
}));
|
68
|
-
if (values.length) {
|
69
|
-
const variables = `(${values.map((value, i) => `$_${i}:${value}`).join()})`;
|
70
|
-
if (operation) {
|
71
|
-
query = operation + variables + query.slice(operation.length);
|
72
|
-
}
|
73
|
-
else if (name !== "gql") {
|
74
|
-
query = "query" + variables + query;
|
75
|
-
}
|
76
|
-
}
|
77
|
-
try {
|
78
|
-
documentNode = parse(query, { noLocation: true });
|
79
|
-
}
|
80
|
-
catch (err) {
|
81
|
-
throw path.buildCodeFrameError(String(err));
|
82
|
-
}
|
83
|
-
const errors = validate(schema, documentNode);
|
84
|
-
if (errors.length) {
|
85
|
-
throw path.buildCodeFrameError(errors[0].message);
|
86
|
-
}
|
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
|
-
}
|
98
|
-
if (expressions.length) {
|
99
|
-
properties.push(t.objectProperty(t.identifier("variables"), t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier("_" + i), expression)))));
|
100
|
-
}
|
101
|
-
path.replaceWith(t.callExpression(t.identifier(name), [t.objectExpression(properties)]));
|
102
|
-
},
|
103
|
-
},
|
104
|
-
};
|
105
|
-
};
|
106
|
-
|
107
|
-
export { index as default };
|
108
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
@@ -1 +0,0 @@
|
|
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,EAAE;QAC7D,MAAM,GAAG,cAAc,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;KACxC;SAAM;QACL,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC;KAC1B;IAED,OAAO;QACL,IAAI,EAAE,yBAAyB;QAC/B,OAAO,EAAE;YACP,wBAAwB,CAAC,IAAI;gBAC3B,MAAM,EACJ,GAAG,EACH,KAAK,EAAE,EAAE,M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