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

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ 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;
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const fs = require('fs');
4
+ const base52 = require('@mo36924/base52');
4
5
  const graphqlSchema = require('@mo36924/graphql-schema');
5
6
  const graphql = require('graphql');
6
7
 
@@ -9,9 +10,12 @@ const index = (({
9
10
  }, options) => {
10
11
  let schema;
11
12
 
12
- if (typeof options.schema === "string") {
13
- const graphql = fs.readFileSync(options.schema || "index.graphql", "utf8");
14
- schema = graphqlSchema.buildSchemaModel(graphql);
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);
15
19
  } else if (options.schema) {
16
20
  schema = options.schema;
17
21
  }
@@ -33,54 +37,44 @@ const index = (({
33
37
 
34
38
  const name = tag.name;
35
39
 
36
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "useQuery" && name !== "useMutation") {
40
+ if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
37
41
  return;
38
42
  }
39
43
 
40
- if (name === "gql" && expressions.length) {
41
- throw path.buildCodeFrameError("gql invalid expressions.");
42
- }
43
-
44
44
  let query = quasis[0].value.cooked ?? quasis[0].value.raw;
45
- let variables = "";
46
45
 
47
46
  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`;
47
+ query += `$${base52.encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
50
48
  }
51
49
 
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
- }
50
+ if (name === "mutation" || name === "subscription") {
51
+ query = name + query;
64
52
  }
65
53
 
66
54
  let documentNode;
67
55
 
68
56
  try {
69
- query = graphql.stripIgnoredCharacters(query);
70
57
  documentNode = graphql.parse(query);
71
58
  } catch (err) {
72
59
  throw path.buildCodeFrameError(String(err));
73
60
  }
74
61
 
75
- let errors = graphql.validate(schema, documentNode);
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
+ }));
76
70
 
77
- for (const error of errors) {
78
- const match = error.message.match(/^Variable ".*?" of type "Unknown" used in position expecting type "(.*?)"\.$/);
71
+ if (values.length) {
72
+ const variables = `(${values.map((value, i) => `$${base52.encode(i)}:${value}`).join()})`;
79
73
 
80
- if (match) {
81
- query = query.replace("Unknown", match[1]);
82
- } else {
83
- throw path.buildCodeFrameError(error.message);
74
+ if (name === "query") {
75
+ query = name + variables + query;
76
+ } else if (name === "mutation" || name === "subscription") {
77
+ query = name + variables + query.slice(name.length);
84
78
  }
85
79
  }
86
80
 
@@ -90,16 +84,16 @@ const index = (({
90
84
  throw path.buildCodeFrameError(String(err));
91
85
  }
92
86
 
93
- errors = graphql.validate(schema, documentNode);
87
+ const errors = graphql.validate(schema, documentNode);
94
88
 
95
- for (const error of errors) {
96
- throw path.buildCodeFrameError(error.message);
89
+ if (errors.length) {
90
+ throw path.buildCodeFrameError(errors[0].message);
97
91
  }
98
92
 
99
- const args = [t.stringLiteral(query)];
93
+ const args = [t.stringLiteral(graphql.stripIgnoredCharacters(query))];
100
94
 
101
- if (variables) {
102
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(`_${i}`), expression))));
95
+ if (expressions.length) {
96
+ args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(base52.encode(i)), expression))));
103
97
  }
104
98
 
105
99
  path.replaceWith(t.callExpression(t.identifier(name), args));
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,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;AAAV;AAFH,YAGFJ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I,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":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,GAAGF,eAAY,CAACH,OAAO,CAACC,MAAR,IAAkB,eAAnB,EAAoC,MAApC,CAAlC;AACAA,IAAAA,MAAM,GAAGK,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,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;;;;"}
package/dist/index.mjs CHANGED
@@ -1,15 +1,19 @@
1
1
  import { readFileSync } from 'fs';
2
- import { buildSchemaModel } from '@mo36924/graphql-schema';
3
- import { stripIgnoredCharacters, parse, validate } from 'graphql';
2
+ import { encode } from '@mo36924/base52';
3
+ import { buildSchemaModel, buildSchema } from '@mo36924/graphql-schema';
4
+ import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
4
5
 
5
6
  const index = (({
6
7
  types: t
7
8
  }, options) => {
8
9
  let schema;
9
10
 
10
- if (typeof options.schema === "string") {
11
- const graphql = readFileSync(options.schema || "index.graphql", "utf8");
12
- schema = buildSchemaModel(graphql);
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);
13
17
  } else if (options.schema) {
14
18
  schema = options.schema;
15
19
  }
@@ -31,54 +35,44 @@ const index = (({
31
35
 
32
36
  const name = tag.name;
33
37
 
34
- if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "useQuery" && name !== "useMutation") {
38
+ if (name !== "gql" && name !== "query" && name !== "mutation" && name !== "subscription") {
35
39
  return;
36
40
  }
37
41
 
38
- if (name === "gql" && expressions.length) {
39
- throw path.buildCodeFrameError("gql invalid expressions.");
40
- }
41
-
42
42
  let query = quasis[0].value.cooked ?? quasis[0].value.raw;
43
- let variables = "";
44
43
 
45
44
  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`;
45
+ query += `$${encode(i)}${quasis[i + 1].value.cooked ?? quasis[i + 1].value.raw}`;
48
46
  }
49
47
 
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
- }
48
+ if (name === "mutation" || name === "subscription") {
49
+ query = name + query;
62
50
  }
63
51
 
64
52
  let documentNode;
65
53
 
66
54
  try {
67
- query = stripIgnoredCharacters(query);
68
55
  documentNode = parse(query);
69
56
  } catch (err) {
70
57
  throw path.buildCodeFrameError(String(err));
71
58
  }
72
59
 
73
- let errors = validate(schema, documentNode);
60
+ const values = [];
61
+ const typeInfo = new TypeInfo(schema);
62
+ visit(documentNode, visitWithTypeInfo(typeInfo, {
63
+ Variable() {
64
+ values.push(typeInfo.getInputType());
65
+ }
66
+
67
+ }));
74
68
 
75
- for (const error of errors) {
76
- const match = error.message.match(/^Variable ".*?" of type "Unknown" used in position expecting type "(.*?)"\.$/);
69
+ if (values.length) {
70
+ const variables = `(${values.map((value, i) => `$${encode(i)}:${value}`).join()})`;
77
71
 
78
- if (match) {
79
- query = query.replace("Unknown", match[1]);
80
- } else {
81
- throw path.buildCodeFrameError(error.message);
72
+ if (name === "query") {
73
+ query = name + variables + query;
74
+ } else if (name === "mutation" || name === "subscription") {
75
+ query = name + variables + query.slice(name.length);
82
76
  }
83
77
  }
84
78
 
@@ -88,16 +82,16 @@ const index = (({
88
82
  throw path.buildCodeFrameError(String(err));
89
83
  }
90
84
 
91
- errors = validate(schema, documentNode);
85
+ const errors = validate(schema, documentNode);
92
86
 
93
- for (const error of errors) {
94
- throw path.buildCodeFrameError(error.message);
87
+ if (errors.length) {
88
+ throw path.buildCodeFrameError(errors[0].message);
95
89
  }
96
90
 
97
- const args = [t.stringLiteral(query)];
91
+ const args = [t.stringLiteral(stripIgnoredCharacters(query))];
98
92
 
99
- if (variables) {
100
- args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(`_${i}`), expression))));
93
+ if (expressions.length) {
94
+ args.push(t.objectExpression(expressions.map((expression, i) => t.objectProperty(t.identifier(encode(i)), expression))));
101
95
  }
102
96
 
103
97
  path.replaceWith(t.callExpression(t.identifier(name), args));
@@ -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,CAA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AD,CAA/B,CAAN;AACD;;AAEDE,QAAAA,MAAM,GAAGC,QAAQ,CAAC5B,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.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,EAA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ACFE,UAAAA,YAAY,GAAGC,KAAK,CAACR,KAAD,CAApB;AACD,SAFD,CAEE,OAAOS,GAAP,EAAY;AACZ,gBAAMjB,IAAI,CAACkB,mBAAL,CAAyBC,MAAM,CAACF,GAAD,CAA/B,CAAN;AACD;;AAED,cAAMe,MAAM,GAAGC,QAAQ,CAACzC,MAAD,EAASuB,YAAT,CAAvB;;AAEA,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,sBAAsB,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,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;;;;"}
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.4.47",
4
4
  "description": "babel-plugin-graphql-tagged-template",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/mo36924/monorepo#readme",
@@ -24,11 +24,12 @@
24
24
  "module": "./dist/index.mjs",
25
25
  "dependencies": {
26
26
  "@babel/core": "^7.15.0",
27
+ "@mo36924/base52": "^1.4.0",
27
28
  "@mo36924/graphql-schema": "^1.4.42",
28
29
  "graphql": "^15.5.0"
29
30
  },
30
31
  "publishConfig": {
31
32
  "access": "public"
32
33
  },
33
- "gitHead": "0097eee4454a2cdc97498eb90e01d9884c1aaa0c"
34
+ "gitHead": "ad5effba723836f25b2ca74e3e4d199b9c4b43ff"
34
35
  }
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
  }