@mo36924/babel-plugin-graphql-tagged-template 5.1.14 → 5.4.3

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs CHANGED
@@ -5,35 +5,51 @@ var node_path = require('node:path');
5
5
  var node_url = require('node:url');
6
6
  var core = require('@babel/core');
7
7
  var helperPluginUtils = require('@babel/helper-plugin-utils');
8
- var basex = require('base-x');
8
+ var utils = require('@graphql-tools/utils');
9
9
  var graphql = require('graphql');
10
+ var packages_babelPluginGraphqlTaggedTemplate_dist_queries = require('./queries.cjs');
10
11
 
11
12
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
12
- const queriesCache = [];
13
13
  const queriesDir = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('packages/babel-plugin-graphql-tagged-template/dist/index.cjs', document.baseURI).href))));
14
14
  const queriesPaths = [
15
15
  "js",
16
16
  "cjs",
17
17
  "ts"
18
18
  ].map((extname)=>node_path.join(queriesDir, `queries.${extname}`));
19
- const base52 = basex("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
20
- const hash = (data)=>base52.encode(node_crypto.createHash("sha256").update(data).digest());
21
- var index = helperPluginUtils.declare((_, { schema, development, queries = queriesCache })=>{
19
+ const schemaPaths = [
20
+ "js",
21
+ "cjs",
22
+ "ts"
23
+ ].map((extname)=>node_path.join(queriesDir, `schema.${extname}`));
24
+ const hash = (data)=>node_crypto.createHash("sha256").update(data).digest("base64url");
25
+ var index = helperPluginUtils.declare((_api, { schema, development, queries = packages_babelPluginGraphqlTaggedTemplate_dist_queries.queries })=>{
22
26
  return {
23
27
  name: "babel-plugin-graphql-tagged-template",
24
28
  visitor: {
25
29
  ...development ? {} : {
26
30
  VariableDeclarator (path, { filename = "" }) {
27
- if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
31
+ if (queriesPaths.includes(filename) && path.get("id").isIdentifier({
28
32
  name: "queries"
29
- }) && path.get("init").isObjectExpression())) {
30
- return;
33
+ })) {
34
+ path.get("init").replaceWith(core.types.callExpression(core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("assign")), [
35
+ core.types.callExpression(core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("create")), [
36
+ core.types.nullLiteral()
37
+ ]),
38
+ core.types.objectExpression(Object.entries(queries).sort(([a], [b])=>a.localeCompare(b)).map(([key, documentNode])=>core.types.objectProperty(core.types.stringLiteral(key), core.types.callExpression(core.types.memberExpression(core.types.identifier("JSON"), core.types.identifier("parse")), [
39
+ core.types.stringLiteral(JSON.stringify(documentNode))
40
+ ]))))
41
+ ]));
42
+ } else if (schemaPaths.includes(filename) && path.get("id").isIdentifier({
43
+ name: "schema"
44
+ })) {
45
+ const source = utils.printSchemaWithDirectives(schema);
46
+ const documentNode = graphql.parse(source, {
47
+ noLocation: true
48
+ });
49
+ path.get("init").replaceWith(core.types.callExpression(core.types.memberExpression(core.types.identifier("JSON"), core.types.identifier("parse")), [
50
+ core.types.stringLiteral(JSON.stringify(documentNode))
51
+ ]));
31
52
  }
32
- path.get("init").replaceWith(core.types.objectExpression(queries.sort().map((query)=>core.types.objectProperty(core.types.identifier(hash(query)), core.types.callExpression(core.types.memberExpression(core.types.identifier("JSON"), core.types.identifier("parse")), [
33
- core.types.stringLiteral(JSON.stringify(graphql.parse(query, {
34
- noLocation: true
35
- })))
36
- ])))));
37
53
  }
38
54
  },
39
55
  TaggedTemplateExpression (path) {
@@ -70,13 +86,14 @@ var index = helperPluginUtils.declare((_, { schema, development, queries = queri
70
86
  if (field.kind !== "Field") {
71
87
  throw path.buildCodeFrameError("Only a field is allowed");
72
88
  }
73
- const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
74
- const operation = isMutation ? "mutation" : "query";
75
- // @ts-expect-error ignore readonly
76
- definition.operation = operation;
77
89
  const values = [];
78
90
  const typeInfo = new graphql.TypeInfo(schema);
79
- graphql.visit(definition, graphql.visitWithTypeInfo(typeInfo, {
91
+ const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
92
+ const operation = isMutation ? graphql.OperationTypeNode.MUTATION : graphql.OperationTypeNode.QUERY;
93
+ graphql.visit({
94
+ ...definition,
95
+ operation
96
+ }, graphql.visitWithTypeInfo(typeInfo, {
80
97
  Variable () {
81
98
  values.push(typeInfo.getInputType());
82
99
  }
@@ -99,12 +116,10 @@ var index = helperPluginUtils.declare((_, { schema, development, queries = queri
99
116
  throw path.buildCodeFrameError(errors[0].message);
100
117
  }
101
118
  query = graphql.stripIgnoredCharacters(query);
102
- if (!development && !queries.includes(query)) {
103
- queries.push(query);
104
- }
105
- const id = development ? query : hash(query);
119
+ const key = hash(query);
120
+ queries[key] = documentNode;
106
121
  const properties = [
107
- core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(id))
122
+ core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(key))
108
123
  ];
109
124
  if (expressions.length) {
110
125
  properties.push(core.types.objectProperty(core.types.identifier("variables"), core.types.objectExpression(expressions.map((expression, i)=>core.types.objectProperty(core.types.identifier(`_${i}`), expression)))));
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import * as _babel_core from '@babel/core';
2
2
  import { PluginObj } from '@babel/core';
3
3
  import { GraphQLSchema } from 'graphql';
4
+ import { Queries } from './queries.js';
4
5
 
5
6
  type Options = {
6
7
  schema: GraphQLSchema;
7
8
  development?: boolean;
8
- queries?: string[];
9
+ queries?: Queries;
9
10
  };
10
11
  declare const _default: (api: object, options: Options | null | undefined, dirname: string) => PluginObj<_babel_core.PluginPass>;
11
12
 
package/dist/index.js CHANGED
@@ -3,34 +3,50 @@ import { dirname, join } from 'node:path';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { types } from '@babel/core';
5
5
  import { declare } from '@babel/helper-plugin-utils';
6
- import basex from 'base-x';
7
- import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
6
+ import { printSchemaWithDirectives } from '@graphql-tools/utils';
7
+ import { parse, TypeInfo, OperationTypeNode, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
8
+ import { queries } from './queries.js';
8
9
 
9
- const queriesCache = [];
10
10
  const queriesDir = dirname(fileURLToPath(import.meta.url));
11
11
  const queriesPaths = [
12
12
  "js",
13
13
  "cjs",
14
14
  "ts"
15
15
  ].map((extname)=>join(queriesDir, `queries.${extname}`));
16
- const base52 = basex("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
17
- const hash = (data)=>base52.encode(createHash("sha256").update(data).digest());
18
- var index = declare((_, { schema, development, queries = queriesCache })=>{
16
+ const schemaPaths = [
17
+ "js",
18
+ "cjs",
19
+ "ts"
20
+ ].map((extname)=>join(queriesDir, `schema.${extname}`));
21
+ const hash = (data)=>createHash("sha256").update(data).digest("base64url");
22
+ var index = declare((_api, { schema, development, queries: queries$1 = queries })=>{
19
23
  return {
20
24
  name: "babel-plugin-graphql-tagged-template",
21
25
  visitor: {
22
26
  ...development ? {} : {
23
27
  VariableDeclarator (path, { filename = "" }) {
24
- if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
28
+ if (queriesPaths.includes(filename) && path.get("id").isIdentifier({
25
29
  name: "queries"
26
- }) && path.get("init").isObjectExpression())) {
27
- return;
30
+ })) {
31
+ path.get("init").replaceWith(types.callExpression(types.memberExpression(types.identifier("Object"), types.identifier("assign")), [
32
+ types.callExpression(types.memberExpression(types.identifier("Object"), types.identifier("create")), [
33
+ types.nullLiteral()
34
+ ]),
35
+ types.objectExpression(Object.entries(queries$1).sort(([a], [b])=>a.localeCompare(b)).map(([key, documentNode])=>types.objectProperty(types.stringLiteral(key), types.callExpression(types.memberExpression(types.identifier("JSON"), types.identifier("parse")), [
36
+ types.stringLiteral(JSON.stringify(documentNode))
37
+ ]))))
38
+ ]));
39
+ } else if (schemaPaths.includes(filename) && path.get("id").isIdentifier({
40
+ name: "schema"
41
+ })) {
42
+ const source = printSchemaWithDirectives(schema);
43
+ const documentNode = parse(source, {
44
+ noLocation: true
45
+ });
46
+ path.get("init").replaceWith(types.callExpression(types.memberExpression(types.identifier("JSON"), types.identifier("parse")), [
47
+ types.stringLiteral(JSON.stringify(documentNode))
48
+ ]));
28
49
  }
29
- path.get("init").replaceWith(types.objectExpression(queries.sort().map((query)=>types.objectProperty(types.identifier(hash(query)), types.callExpression(types.memberExpression(types.identifier("JSON"), types.identifier("parse")), [
30
- types.stringLiteral(JSON.stringify(parse(query, {
31
- noLocation: true
32
- })))
33
- ])))));
34
50
  }
35
51
  },
36
52
  TaggedTemplateExpression (path) {
@@ -67,13 +83,14 @@ var index = declare((_, { schema, development, queries = queriesCache })=>{
67
83
  if (field.kind !== "Field") {
68
84
  throw path.buildCodeFrameError("Only a field is allowed");
69
85
  }
70
- const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
71
- const operation = isMutation ? "mutation" : "query";
72
- // @ts-expect-error ignore readonly
73
- definition.operation = operation;
74
86
  const values = [];
75
87
  const typeInfo = new TypeInfo(schema);
76
- visit(definition, visitWithTypeInfo(typeInfo, {
88
+ const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
89
+ const operation = isMutation ? OperationTypeNode.MUTATION : OperationTypeNode.QUERY;
90
+ visit({
91
+ ...definition,
92
+ operation
93
+ }, visitWithTypeInfo(typeInfo, {
77
94
  Variable () {
78
95
  values.push(typeInfo.getInputType());
79
96
  }
@@ -96,12 +113,10 @@ var index = declare((_, { schema, development, queries = queriesCache })=>{
96
113
  throw path.buildCodeFrameError(errors[0].message);
97
114
  }
98
115
  query = stripIgnoredCharacters(query);
99
- if (!development && !queries.includes(query)) {
100
- queries.push(query);
101
- }
102
- const id = development ? query : hash(query);
116
+ const key = hash(query);
117
+ queries$1[key] = documentNode;
103
118
  const properties = [
104
- types.objectProperty(types.identifier("query"), types.stringLiteral(id))
119
+ types.objectProperty(types.identifier("query"), types.stringLiteral(key))
105
120
  ];
106
121
  if (expressions.length) {
107
122
  properties.push(types.objectProperty(types.identifier("variables"), types.objectExpression(expressions.map((expression, i)=>types.objectProperty(types.identifier(`_${i}`), expression)))));
package/dist/queries.cjs CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const queries = {};
3
+ var _globalThis;
4
+ // @ts-expect-error queries is set to a value by Babel
5
+ const queries = (_globalThis = globalThis).__GRAPHQL_QUERIES__ ?? (_globalThis.__GRAPHQL_QUERIES__ = Object.create(null));
4
6
 
5
7
  exports.queries = queries;
package/dist/queries.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { DocumentNode } from 'graphql';
2
2
 
3
- declare const queries: {
3
+ type Queries = {
4
4
  [key: string]: DocumentNode;
5
5
  };
6
+ declare const queries: Queries;
6
7
 
7
- export { queries };
8
+ export { type Queries, queries };
package/dist/queries.js CHANGED
@@ -1,3 +1,5 @@
1
- const queries = {};
1
+ var _globalThis;
2
+ // @ts-expect-error queries is set to a value by Babel
3
+ const queries = (_globalThis = globalThis).__GRAPHQL_QUERIES__ ?? (_globalThis.__GRAPHQL_QUERIES__ = Object.create(null));
2
4
 
3
5
  export { queries };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var config = require('@mo36924/graphql/config');
4
+
5
+ const schema = config.getSchema().schema;
6
+
7
+ exports.schema = schema;
@@ -0,0 +1,5 @@
1
+ import * as graphql from 'graphql';
2
+
3
+ declare const schema: graphql.GraphQLSchema | undefined;
4
+
5
+ export { schema };
package/dist/schema.js ADDED
@@ -0,0 +1,5 @@
1
+ import { getSchema } from '@mo36924/graphql/config';
2
+
3
+ const schema = getSchema().schema;
4
+
5
+ export { schema };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mo36924/babel-plugin-graphql-tagged-template",
3
3
  "type": "module",
4
- "version": "5.1.14",
4
+ "version": "5.4.3",
5
5
  "description": "babel-plugin-graphql-tagged-template",
6
6
  "author": "mo36924",
7
7
  "license": "MIT",
@@ -27,6 +27,12 @@
27
27
  "import": "./dist/queries.js",
28
28
  "require": "./dist/queries.cjs",
29
29
  "default": "./dist/queries.js"
30
+ },
31
+ "./schema": {
32
+ "types": "./dist/schema.d.ts",
33
+ "import": "./dist/schema.js",
34
+ "require": "./dist/schema.cjs",
35
+ "default": "./dist/schema.js"
30
36
  }
31
37
  },
32
38
  "main": "./dist/index.js",
@@ -49,10 +55,11 @@
49
55
  "dependencies": {
50
56
  "@babel/core": "^7.26.0",
51
57
  "@babel/helper-plugin-utils": "^7.25.9",
58
+ "@graphql-tools/utils": "^10.5.6",
59
+ "@mo36924/graphql": "^5.4.2",
52
60
  "@types/babel__core": "^7.20.5",
53
61
  "@types/babel__helper-plugin-utils": "^7.10.3",
54
- "base-x": "^5.0.0",
55
62
  "graphql": "^16.9.0"
56
63
  },
57
- "gitHead": "39dbb35229f5ea671f0c356000d63b81eba59f92"
64
+ "gitHead": "e313716c27a4a0550a2e2b06506fbef0558bd0af"
58
65
  }