@constructive-io/graphql-codegen 2.19.0 → 2.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/README.md +1818 -113
  2. package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  3. package/__tests__/codegen/input-types-generator.test.js +635 -0
  4. package/cli/codegen/barrel.d.ts +27 -0
  5. package/cli/codegen/barrel.js +163 -0
  6. package/cli/codegen/client.d.ts +4 -0
  7. package/cli/codegen/client.js +170 -0
  8. package/cli/codegen/custom-mutations.d.ts +38 -0
  9. package/cli/codegen/custom-mutations.js +149 -0
  10. package/cli/codegen/custom-queries.d.ts +38 -0
  11. package/cli/codegen/custom-queries.js +358 -0
  12. package/cli/codegen/filters.d.ts +27 -0
  13. package/cli/codegen/filters.js +357 -0
  14. package/cli/codegen/gql-ast.d.ts +41 -0
  15. package/cli/codegen/gql-ast.js +329 -0
  16. package/cli/codegen/index.d.ts +71 -0
  17. package/cli/codegen/index.js +147 -0
  18. package/cli/codegen/mutations.d.ts +30 -0
  19. package/cli/codegen/mutations.js +410 -0
  20. package/cli/codegen/orm/barrel.d.ts +18 -0
  21. package/cli/codegen/orm/barrel.js +48 -0
  22. package/cli/codegen/orm/client-generator.d.ts +45 -0
  23. package/cli/codegen/orm/client-generator.js +646 -0
  24. package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  25. package/cli/codegen/orm/custom-ops-generator.js +350 -0
  26. package/cli/codegen/orm/index.d.ts +38 -0
  27. package/cli/codegen/orm/index.js +88 -0
  28. package/cli/codegen/orm/input-types-generator.d.ts +21 -0
  29. package/cli/codegen/orm/input-types-generator.js +705 -0
  30. package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  31. package/cli/codegen/orm/input-types-generator.test.js +75 -0
  32. package/cli/codegen/orm/model-generator.d.ts +32 -0
  33. package/cli/codegen/orm/model-generator.js +264 -0
  34. package/cli/codegen/orm/query-builder.d.ts +161 -0
  35. package/cli/codegen/orm/query-builder.js +366 -0
  36. package/cli/codegen/orm/select-types.d.ts +169 -0
  37. package/cli/codegen/orm/select-types.js +16 -0
  38. package/cli/codegen/orm/select-types.test.d.ts +11 -0
  39. package/cli/codegen/orm/select-types.test.js +22 -0
  40. package/cli/codegen/queries.d.ts +25 -0
  41. package/cli/codegen/queries.js +438 -0
  42. package/cli/codegen/scalars.d.ts +12 -0
  43. package/cli/codegen/scalars.js +71 -0
  44. package/cli/codegen/schema-gql-ast.d.ts +51 -0
  45. package/cli/codegen/schema-gql-ast.js +385 -0
  46. package/cli/codegen/ts-ast.d.ts +122 -0
  47. package/cli/codegen/ts-ast.js +280 -0
  48. package/cli/codegen/type-resolver.d.ts +96 -0
  49. package/cli/codegen/type-resolver.js +246 -0
  50. package/cli/codegen/types.d.ts +12 -0
  51. package/cli/codegen/types.js +69 -0
  52. package/cli/codegen/utils.d.ts +163 -0
  53. package/cli/codegen/utils.js +326 -0
  54. package/cli/commands/generate-orm.d.ts +37 -0
  55. package/cli/commands/generate-orm.js +195 -0
  56. package/cli/commands/generate.d.ts +39 -0
  57. package/cli/commands/generate.js +299 -0
  58. package/cli/commands/index.d.ts +7 -0
  59. package/cli/commands/index.js +12 -0
  60. package/cli/commands/init.d.ts +35 -0
  61. package/cli/commands/init.js +176 -0
  62. package/cli/index.d.ts +4 -0
  63. package/cli/index.js +291 -0
  64. package/cli/introspect/fetch-meta.d.ts +31 -0
  65. package/cli/introspect/fetch-meta.js +108 -0
  66. package/cli/introspect/fetch-schema.d.ts +21 -0
  67. package/cli/introspect/fetch-schema.js +86 -0
  68. package/cli/introspect/index.d.ts +8 -0
  69. package/cli/introspect/index.js +16 -0
  70. package/cli/introspect/meta-query.d.ts +111 -0
  71. package/cli/introspect/meta-query.js +191 -0
  72. package/cli/introspect/schema-query.d.ts +20 -0
  73. package/cli/introspect/schema-query.js +123 -0
  74. package/cli/introspect/transform-schema.d.ts +74 -0
  75. package/cli/introspect/transform-schema.js +269 -0
  76. package/cli/introspect/transform-schema.test.d.ts +1 -0
  77. package/cli/introspect/transform-schema.test.js +67 -0
  78. package/cli/introspect/transform.d.ts +21 -0
  79. package/cli/introspect/transform.js +216 -0
  80. package/cli/watch/cache.d.ts +45 -0
  81. package/cli/watch/cache.js +111 -0
  82. package/cli/watch/debounce.d.ts +19 -0
  83. package/cli/watch/debounce.js +89 -0
  84. package/cli/watch/hash.d.ts +17 -0
  85. package/cli/watch/hash.js +48 -0
  86. package/cli/watch/index.d.ts +10 -0
  87. package/cli/watch/index.js +22 -0
  88. package/cli/watch/orchestrator.d.ts +63 -0
  89. package/cli/watch/orchestrator.js +228 -0
  90. package/cli/watch/poller.d.ts +65 -0
  91. package/cli/watch/poller.js +203 -0
  92. package/cli/watch/types.d.ts +67 -0
  93. package/cli/watch/types.js +5 -0
  94. package/client/error.d.ts +95 -0
  95. package/client/error.js +255 -0
  96. package/client/execute.d.ts +57 -0
  97. package/client/execute.js +124 -0
  98. package/client/index.d.ts +6 -0
  99. package/client/index.js +18 -0
  100. package/client/typed-document.d.ts +31 -0
  101. package/client/typed-document.js +44 -0
  102. package/core/ast.d.ts +10 -0
  103. package/core/ast.js +593 -0
  104. package/core/custom-ast.d.ts +35 -0
  105. package/core/custom-ast.js +204 -0
  106. package/core/index.d.ts +8 -0
  107. package/core/index.js +33 -0
  108. package/core/meta-object/convert.d.ts +65 -0
  109. package/core/meta-object/convert.js +63 -0
  110. package/core/meta-object/format.json +93 -0
  111. package/core/meta-object/index.d.ts +2 -0
  112. package/core/meta-object/index.js +18 -0
  113. package/core/meta-object/validate.d.ts +9 -0
  114. package/core/meta-object/validate.js +34 -0
  115. package/core/query-builder.d.ts +46 -0
  116. package/core/query-builder.js +412 -0
  117. package/core/types.d.ts +139 -0
  118. package/core/types.js +28 -0
  119. package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  120. package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
  121. package/esm/cli/codegen/barrel.d.ts +27 -0
  122. package/esm/cli/codegen/barrel.js +156 -0
  123. package/esm/cli/codegen/client.d.ts +4 -0
  124. package/esm/cli/codegen/client.js +167 -0
  125. package/esm/cli/codegen/custom-mutations.d.ts +38 -0
  126. package/esm/cli/codegen/custom-mutations.js +145 -0
  127. package/esm/cli/codegen/custom-queries.d.ts +38 -0
  128. package/esm/cli/codegen/custom-queries.js +354 -0
  129. package/esm/cli/codegen/filters.d.ts +27 -0
  130. package/esm/cli/codegen/filters.js +351 -0
  131. package/esm/cli/codegen/gql-ast.d.ts +41 -0
  132. package/esm/cli/codegen/gql-ast.js +288 -0
  133. package/esm/cli/codegen/index.d.ts +71 -0
  134. package/esm/cli/codegen/index.js +124 -0
  135. package/esm/cli/codegen/mutations.d.ts +30 -0
  136. package/esm/cli/codegen/mutations.js +404 -0
  137. package/esm/cli/codegen/orm/barrel.d.ts +18 -0
  138. package/esm/cli/codegen/orm/barrel.js +44 -0
  139. package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
  140. package/esm/cli/codegen/orm/client-generator.js +640 -0
  141. package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  142. package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
  143. package/esm/cli/codegen/orm/index.d.ts +38 -0
  144. package/esm/cli/codegen/orm/index.js +75 -0
  145. package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
  146. package/esm/cli/codegen/orm/input-types-generator.js +700 -0
  147. package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  148. package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
  149. package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
  150. package/esm/cli/codegen/orm/model-generator.js +260 -0
  151. package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
  152. package/esm/cli/codegen/orm/query-builder.js +353 -0
  153. package/esm/cli/codegen/orm/select-types.d.ts +169 -0
  154. package/esm/cli/codegen/orm/select-types.js +15 -0
  155. package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
  156. package/esm/cli/codegen/orm/select-types.test.js +21 -0
  157. package/esm/cli/codegen/queries.d.ts +25 -0
  158. package/esm/cli/codegen/queries.js +433 -0
  159. package/esm/cli/codegen/scalars.d.ts +12 -0
  160. package/esm/cli/codegen/scalars.js +66 -0
  161. package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
  162. package/esm/cli/codegen/schema-gql-ast.js +343 -0
  163. package/esm/cli/codegen/ts-ast.d.ts +122 -0
  164. package/esm/cli/codegen/ts-ast.js +260 -0
  165. package/esm/cli/codegen/type-resolver.d.ts +96 -0
  166. package/esm/cli/codegen/type-resolver.js +224 -0
  167. package/esm/cli/codegen/types.d.ts +12 -0
  168. package/esm/cli/codegen/types.js +65 -0
  169. package/esm/cli/codegen/utils.d.ts +163 -0
  170. package/esm/cli/codegen/utils.js +288 -0
  171. package/esm/cli/commands/generate-orm.d.ts +37 -0
  172. package/esm/cli/commands/generate-orm.js +192 -0
  173. package/esm/cli/commands/generate.d.ts +39 -0
  174. package/esm/cli/commands/generate.js +262 -0
  175. package/esm/cli/commands/index.d.ts +7 -0
  176. package/esm/cli/commands/index.js +5 -0
  177. package/esm/cli/commands/init.d.ts +35 -0
  178. package/esm/cli/commands/init.js +138 -0
  179. package/esm/cli/index.d.ts +4 -0
  180. package/esm/cli/index.js +256 -0
  181. package/esm/cli/introspect/fetch-meta.d.ts +31 -0
  182. package/esm/cli/introspect/fetch-meta.js +104 -0
  183. package/esm/cli/introspect/fetch-schema.d.ts +21 -0
  184. package/esm/cli/introspect/fetch-schema.js +83 -0
  185. package/esm/cli/introspect/index.d.ts +8 -0
  186. package/esm/cli/introspect/index.js +6 -0
  187. package/esm/cli/introspect/meta-query.d.ts +111 -0
  188. package/esm/cli/introspect/meta-query.js +188 -0
  189. package/esm/cli/introspect/schema-query.d.ts +20 -0
  190. package/esm/cli/introspect/schema-query.js +120 -0
  191. package/esm/cli/introspect/transform-schema.d.ts +74 -0
  192. package/esm/cli/introspect/transform-schema.js +259 -0
  193. package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
  194. package/esm/cli/introspect/transform-schema.test.js +65 -0
  195. package/esm/cli/introspect/transform.d.ts +21 -0
  196. package/esm/cli/introspect/transform.js +210 -0
  197. package/esm/cli/watch/cache.d.ts +45 -0
  198. package/esm/cli/watch/cache.js +73 -0
  199. package/esm/cli/watch/debounce.d.ts +19 -0
  200. package/esm/cli/watch/debounce.js +85 -0
  201. package/esm/cli/watch/hash.d.ts +17 -0
  202. package/esm/cli/watch/hash.js +43 -0
  203. package/esm/cli/watch/index.d.ts +10 -0
  204. package/esm/cli/watch/index.js +8 -0
  205. package/esm/cli/watch/orchestrator.d.ts +63 -0
  206. package/esm/cli/watch/orchestrator.js +223 -0
  207. package/esm/cli/watch/poller.d.ts +65 -0
  208. package/esm/cli/watch/poller.js +198 -0
  209. package/esm/cli/watch/types.d.ts +67 -0
  210. package/esm/cli/watch/types.js +4 -0
  211. package/esm/client/error.d.ts +95 -0
  212. package/esm/client/error.js +249 -0
  213. package/esm/client/execute.d.ts +57 -0
  214. package/esm/client/execute.js +120 -0
  215. package/esm/client/index.d.ts +6 -0
  216. package/esm/client/index.js +6 -0
  217. package/esm/client/typed-document.d.ts +31 -0
  218. package/esm/client/typed-document.js +40 -0
  219. package/esm/core/ast.d.ts +10 -0
  220. package/esm/core/ast.js +549 -0
  221. package/esm/core/custom-ast.d.ts +35 -0
  222. package/esm/core/custom-ast.js +161 -0
  223. package/esm/core/index.d.ts +8 -0
  224. package/esm/core/index.js +12 -0
  225. package/esm/core/meta-object/convert.d.ts +65 -0
  226. package/esm/core/meta-object/convert.js +60 -0
  227. package/esm/core/meta-object/format.json +93 -0
  228. package/esm/core/meta-object/index.d.ts +2 -0
  229. package/esm/core/meta-object/index.js +2 -0
  230. package/esm/core/meta-object/validate.d.ts +9 -0
  231. package/esm/core/meta-object/validate.js +28 -0
  232. package/esm/core/query-builder.d.ts +46 -0
  233. package/esm/core/query-builder.js +375 -0
  234. package/esm/core/types.d.ts +139 -0
  235. package/esm/core/types.js +24 -0
  236. package/esm/generators/field-selector.d.ts +30 -0
  237. package/esm/generators/field-selector.js +355 -0
  238. package/esm/generators/index.d.ts +6 -0
  239. package/esm/generators/index.js +9 -0
  240. package/esm/generators/mutations.d.ts +31 -0
  241. package/esm/generators/mutations.js +197 -0
  242. package/esm/generators/select.d.ts +50 -0
  243. package/esm/generators/select.js +636 -0
  244. package/esm/index.d.ts +12 -0
  245. package/esm/index.js +17 -3
  246. package/esm/react/index.d.ts +5 -0
  247. package/esm/react/index.js +6 -0
  248. package/esm/types/config.d.ts +199 -0
  249. package/esm/types/config.js +106 -0
  250. package/esm/types/index.d.ts +9 -0
  251. package/esm/types/index.js +4 -0
  252. package/esm/types/introspection.d.ts +121 -0
  253. package/esm/types/introspection.js +54 -0
  254. package/esm/types/mutation.d.ts +45 -0
  255. package/esm/types/mutation.js +4 -0
  256. package/esm/types/query.d.ts +82 -0
  257. package/esm/types/query.js +4 -0
  258. package/esm/types/schema.d.ts +253 -0
  259. package/esm/types/schema.js +5 -0
  260. package/esm/types/selection.d.ts +43 -0
  261. package/esm/types/selection.js +4 -0
  262. package/esm/utils/index.d.ts +4 -0
  263. package/esm/utils/index.js +4 -0
  264. package/generators/field-selector.d.ts +30 -0
  265. package/generators/field-selector.js +361 -0
  266. package/generators/index.d.ts +6 -0
  267. package/generators/index.js +27 -0
  268. package/generators/mutations.d.ts +31 -0
  269. package/generators/mutations.js +235 -0
  270. package/generators/select.d.ts +50 -0
  271. package/generators/select.js +679 -0
  272. package/index.d.ts +12 -3
  273. package/index.js +19 -3
  274. package/package.json +59 -38
  275. package/react/index.d.ts +5 -0
  276. package/react/index.js +9 -0
  277. package/types/config.d.ts +199 -0
  278. package/types/config.js +111 -0
  279. package/types/index.d.ts +9 -0
  280. package/types/index.js +10 -0
  281. package/types/introspection.d.ts +121 -0
  282. package/types/introspection.js +62 -0
  283. package/types/mutation.d.ts +45 -0
  284. package/types/mutation.js +5 -0
  285. package/types/query.d.ts +82 -0
  286. package/types/query.js +5 -0
  287. package/types/schema.d.ts +253 -0
  288. package/types/schema.js +6 -0
  289. package/types/selection.d.ts +43 -0
  290. package/types/selection.js +5 -0
  291. package/utils/index.d.ts +4 -0
  292. package/utils/index.js +7 -0
  293. package/codegen.d.ts +0 -13
  294. package/codegen.js +0 -293
  295. package/esm/codegen.js +0 -253
  296. package/esm/gql.js +0 -939
  297. package/esm/options.js +0 -27
  298. package/gql.d.ts +0 -188
  299. package/gql.js +0 -992
  300. package/options.d.ts +0 -45
  301. package/options.js +0 -31
package/codegen.js DELETED
@@ -1,293 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.runCodegen = runCodegen;
40
- exports.runCodegenFromJSONConfig = runCodegenFromJSONConfig;
41
- const fs_1 = require("fs");
42
- const path_1 = require("path");
43
- const graphql_1 = require("graphql");
44
- const inflection = require('inflection');
45
- const gql_1 = require("./gql");
46
- const introspectron_1 = require("introspectron");
47
- const options_1 = require("./options");
48
- const core_1 = require("@graphql-codegen/core");
49
- const typescriptPlugin = __importStar(require("@graphql-codegen/typescript"));
50
- const typescriptOperationsPlugin = __importStar(require("@graphql-codegen/typescript-operations"));
51
- const typescriptGraphqlRequestPlugin = __importStar(require("@graphql-codegen/typescript-graphql-request"));
52
- const typescriptReactQueryPlugin = __importStar(require("@graphql-codegen/typescript-react-query"));
53
- const graphql_request_1 = require("graphql-request");
54
- const parser_1 = require("@babel/parser");
55
- const generator_1 = __importDefault(require("@babel/generator"));
56
- const t = __importStar(require("@babel/types"));
57
- function addDocumentNodeImport(code) {
58
- const ast = (0, parser_1.parse)(code, {
59
- sourceType: 'module',
60
- plugins: ['typescript']
61
- });
62
- const importDecl = t.importDeclaration([t.importSpecifier(t.identifier('DocumentNode'), t.identifier('DocumentNode'))], t.stringLiteral('graphql'));
63
- importDecl.importKind = 'type';
64
- ast.program.body.unshift(importDecl);
65
- const output = (0, generator_1.default)(ast, {}, code);
66
- return output.code;
67
- }
68
- function getFilename(key, convention) {
69
- if (convention === 'underscore')
70
- return inflection.underscore(key);
71
- if (convention === 'dashed')
72
- return inflection.underscore(key).replace(/_/g, '-');
73
- if (convention === 'camelUpper')
74
- return inflection.camelize(key, false);
75
- return key;
76
- }
77
- async function ensureDir(p) {
78
- await fs_1.promises.mkdir(p, { recursive: true });
79
- }
80
- async function readFileUTF8(p) {
81
- return fs_1.promises.readFile(p, 'utf8');
82
- }
83
- async function writeFileUTF8(p, content) {
84
- await ensureDir((0, path_1.dirname)(p));
85
- await fs_1.promises.writeFile(p, content, 'utf8');
86
- }
87
- async function getIntrospectionFromSDL(schemaPath) {
88
- const sdl = await readFileUTF8(schemaPath);
89
- const schema = (0, graphql_1.buildSchema)(sdl);
90
- const q = (0, graphql_1.getIntrospectionQuery)();
91
- const res = await (0, graphql_1.graphql)({ schema, source: q });
92
- return res.data;
93
- }
94
- async function getIntrospectionFromEndpoint(endpoint, headers) {
95
- const client = new graphql_request_1.GraphQLClient(endpoint, { headers });
96
- const q = (0, graphql_1.getIntrospectionQuery)();
97
- const res = await client.request(q);
98
- return res;
99
- }
100
- function generateKeyedObjFromGqlMap(gqlMap, selection, typeNameOverrides, typeIndex) {
101
- const gen = (0, gql_1.generate)(gqlMap, selection, typeNameOverrides, typeIndex);
102
- return Object.entries(gen).reduce((acc, [key, val]) => {
103
- if (val?.ast)
104
- acc[key] = (0, graphql_1.print)(val.ast);
105
- return acc;
106
- }, {});
107
- }
108
- function applyQueryFilters(map, docs) {
109
- const allow = (docs.allowQueries || []).filter(Boolean);
110
- const exclude = (docs.excludeQueries || []).filter(Boolean);
111
- const patterns = (docs.excludePatterns || []).filter(Boolean);
112
- let keys = Object.keys(map);
113
- if (allow.length > 0)
114
- keys = keys.filter((k) => allow.includes(k));
115
- if (exclude.length > 0)
116
- keys = keys.filter((k) => !exclude.includes(k));
117
- if (patterns.length > 0) {
118
- const regs = patterns.map((p) => {
119
- try {
120
- return new RegExp(p);
121
- }
122
- catch {
123
- return null;
124
- }
125
- }).filter((r) => !!r);
126
- keys = keys.filter((k) => !regs.some((r) => r.test(k)));
127
- }
128
- return keys.reduce((acc, k) => {
129
- acc[k] = map[k];
130
- return acc;
131
- }, {});
132
- }
133
- async function writeOperationsDocuments(docs, dir, format, convention) {
134
- await ensureDir(dir);
135
- const index = [];
136
- for (const key of Object.keys(docs)) {
137
- const base = getFilename(key, convention);
138
- const filename = base + (format === 'ts' ? '.ts' : '.gql');
139
- if (format === 'ts') {
140
- const code = `import gql from 'graphql-tag'\nexport const ${key} = gql\`\n${docs[key]}\n\``;
141
- await writeFileUTF8((0, path_1.join)(dir, filename), code);
142
- index.push(`export * from './${base}'`);
143
- }
144
- else {
145
- await writeFileUTF8((0, path_1.join)(dir, filename), docs[key]);
146
- }
147
- }
148
- if (format === 'ts')
149
- await writeFileUTF8((0, path_1.join)(dir, 'index.ts'), index.sort().join('\n'));
150
- }
151
- async function runCodegen(opts, cwd) {
152
- const options = {
153
- input: { ...(options_1.defaultGraphQLCodegenOptions.input), ...(opts.input || {}) },
154
- output: { ...(options_1.defaultGraphQLCodegenOptions.output), ...(opts.output || {}) },
155
- documents: { ...(options_1.defaultGraphQLCodegenOptions.documents), ...(opts.documents || {}) },
156
- features: { ...(options_1.defaultGraphQLCodegenOptions.features), ...(opts.features || {}) },
157
- selection: { ...(options_1.defaultGraphQLCodegenOptions.selection), ...(opts.selection || {}) },
158
- scalars: { ...(options_1.defaultGraphQLCodegenOptions.scalars || {}), ...(opts.scalars || {}) },
159
- typeNameOverrides: { ...(options_1.defaultGraphQLCodegenOptions.typeNameOverrides || {}), ...(opts.typeNameOverrides || {}) }
160
- };
161
- const root = (0, path_1.join)(cwd, options.output.root);
162
- const typesFile = (0, path_1.join)(root, options.output.typesFile);
163
- const operationsDir = (0, path_1.join)(root, options.output.operationsDir);
164
- const sdkFile = (0, path_1.join)(root, options.output.sdkFile);
165
- const reactQueryFile = (0, path_1.join)(root, options.output.reactQueryFile || 'react-query.ts');
166
- const hasSchemaPath = !!options.input.schema && options.input.schema.trim() !== '';
167
- const hasEndpoint = !!options.input.endpoint && options.input.endpoint.trim() !== '';
168
- const schemaPath = hasSchemaPath ? ((0, path_1.isAbsolute)(options.input.schema) ? options.input.schema : (0, path_1.resolve)(cwd, options.input.schema)) : '';
169
- const introspection = hasEndpoint
170
- ? await getIntrospectionFromEndpoint(options.input.endpoint, options.input.headers || {})
171
- : await getIntrospectionFromSDL(schemaPath);
172
- const { queries, mutations } = (0, introspectron_1.parseGraphQuery)(introspection);
173
- const gqlMap = applyQueryFilters({ ...queries, ...mutations }, options.documents);
174
- let docs = {};
175
- const schema = hasEndpoint
176
- ? (0, graphql_1.buildClientSchema)(introspection)
177
- : (0, graphql_1.buildSchema)(await readFileUTF8(schemaPath));
178
- const typeIndex = buildTypeIndex(introspection);
179
- if (options.features.emitOperations || options.features.emitSdk || options.features.emitReactQuery) {
180
- docs = generateKeyedObjFromGqlMap(gqlMap, options.selection, options.typeNameOverrides, typeIndex);
181
- }
182
- if (options.features.emitOperations) {
183
- await writeOperationsDocuments(docs, operationsDir, options.documents.format, options.documents.convention);
184
- }
185
- if (options.features.emitTypes) {
186
- const typesContent = await (0, core_1.codegen)({
187
- filename: typesFile,
188
- schema: schema,
189
- documents: [],
190
- config: { scalars: options.scalars || {} },
191
- plugins: [{ typescript: {} }],
192
- pluginMap: { typescript: typescriptPlugin }
193
- });
194
- await writeFileUTF8(typesFile, typesContent);
195
- }
196
- if (options.features.emitSdk) {
197
- const documents = [];
198
- for (const [name, content] of Object.entries(docs)) {
199
- try {
200
- const doc = require('graphql').parse(content);
201
- documents.push({ location: name, document: doc });
202
- }
203
- catch (e) { }
204
- }
205
- const sdkContent = await (0, core_1.codegen)({
206
- filename: sdkFile,
207
- schema: schema,
208
- documents,
209
- config: { scalars: options.scalars || {}, dedupeOperationSuffix: true },
210
- plugins: [
211
- { typescript: {} },
212
- { 'typescript-operations': {} },
213
- { 'typescript-graphql-request': { dedupeOperationSuffix: true } }
214
- ],
215
- pluginMap: {
216
- typescript: typescriptPlugin,
217
- 'typescript-operations': typescriptOperationsPlugin,
218
- 'typescript-graphql-request': typescriptGraphqlRequestPlugin
219
- }
220
- });
221
- // Fix TS2742: Add missing DocumentNode import using Babel AST
222
- const sdkContentWithImport = addDocumentNodeImport(sdkContent);
223
- await writeFileUTF8(sdkFile, sdkContentWithImport);
224
- }
225
- if (options.features.emitReactQuery) {
226
- const documents = [];
227
- for (const [name, content] of Object.entries(docs)) {
228
- try {
229
- const doc = require('graphql').parse(content);
230
- documents.push({ location: name, document: doc });
231
- }
232
- catch (e) { }
233
- }
234
- const rqConfig = {
235
- fetcher: options.reactQuery?.fetcher || 'graphql-request',
236
- legacyMode: options.reactQuery?.legacyMode || false,
237
- exposeDocument: options.reactQuery?.exposeDocument || false,
238
- addInfiniteQuery: options.reactQuery?.addInfiniteQuery || false,
239
- reactQueryVersion: options.reactQuery?.reactQueryVersion || 5,
240
- scalars: options.scalars || {}
241
- };
242
- const rqContent = await (0, core_1.codegen)({
243
- filename: reactQueryFile,
244
- schema: schema,
245
- documents,
246
- config: { ...rqConfig, dedupeOperationSuffix: true },
247
- plugins: [
248
- { typescript: {} },
249
- { 'typescript-operations': {} },
250
- { 'typescript-react-query': rqConfig }
251
- ],
252
- pluginMap: {
253
- typescript: typescriptPlugin,
254
- 'typescript-operations': typescriptOperationsPlugin,
255
- 'typescript-react-query': typescriptReactQueryPlugin
256
- }
257
- });
258
- await writeFileUTF8(reactQueryFile, rqContent);
259
- }
260
- return { root, typesFile, operationsDir, sdkFile };
261
- }
262
- async function runCodegenFromJSONConfig(configPath, cwd) {
263
- const path = (0, path_1.isAbsolute)(configPath) ? configPath : (0, path_1.resolve)(cwd, configPath);
264
- const content = await readFileUTF8(path);
265
- let overrides = {};
266
- try {
267
- overrides = JSON.parse(content);
268
- }
269
- catch (e) {
270
- throw new Error('Invalid JSON config: ' + e);
271
- }
272
- const merged = (0, options_1.mergeGraphQLCodegenOptions)(options_1.defaultGraphQLCodegenOptions, overrides);
273
- return runCodegen(merged, cwd);
274
- }
275
- function buildTypeIndex(introspection) {
276
- const byName = {};
277
- const types = (introspection && introspection.__schema && introspection.__schema.types) || [];
278
- for (const t of types) {
279
- if (t && typeof t.name === 'string' && t.name.length > 0)
280
- byName[t.name] = t;
281
- }
282
- return {
283
- byName,
284
- getInputFieldType(typeName, fieldName) {
285
- const typ = byName[typeName];
286
- if (!typ || typ.kind !== 'INPUT_OBJECT')
287
- return null;
288
- const fields = typ.inputFields || [];
289
- const f = fields.find((x) => x && x.name === fieldName);
290
- return f ? f.type : null;
291
- }
292
- };
293
- }
package/esm/codegen.js DELETED
@@ -1,253 +0,0 @@
1
- import { promises as fs } from 'fs';
2
- import { join, dirname, isAbsolute, resolve } from 'path';
3
- import { buildSchema, buildClientSchema, graphql, getIntrospectionQuery, print } from 'graphql';
4
- const inflection = require('inflection');
5
- import { generate as generateGql } from './gql';
6
- import { parseGraphQuery } from 'introspectron';
7
- import { defaultGraphQLCodegenOptions, mergeGraphQLCodegenOptions } from './options';
8
- import { codegen as runCoreCodegen } from '@graphql-codegen/core';
9
- import * as typescriptPlugin from '@graphql-codegen/typescript';
10
- import * as typescriptOperationsPlugin from '@graphql-codegen/typescript-operations';
11
- import * as typescriptGraphqlRequestPlugin from '@graphql-codegen/typescript-graphql-request';
12
- import * as typescriptReactQueryPlugin from '@graphql-codegen/typescript-react-query';
13
- import { GraphQLClient } from 'graphql-request';
14
- import { parse } from '@babel/parser';
15
- import generate from '@babel/generator';
16
- import * as t from '@babel/types';
17
- function addDocumentNodeImport(code) {
18
- const ast = parse(code, {
19
- sourceType: 'module',
20
- plugins: ['typescript']
21
- });
22
- const importDecl = t.importDeclaration([t.importSpecifier(t.identifier('DocumentNode'), t.identifier('DocumentNode'))], t.stringLiteral('graphql'));
23
- importDecl.importKind = 'type';
24
- ast.program.body.unshift(importDecl);
25
- const output = generate(ast, {}, code);
26
- return output.code;
27
- }
28
- function getFilename(key, convention) {
29
- if (convention === 'underscore')
30
- return inflection.underscore(key);
31
- if (convention === 'dashed')
32
- return inflection.underscore(key).replace(/_/g, '-');
33
- if (convention === 'camelUpper')
34
- return inflection.camelize(key, false);
35
- return key;
36
- }
37
- async function ensureDir(p) {
38
- await fs.mkdir(p, { recursive: true });
39
- }
40
- async function readFileUTF8(p) {
41
- return fs.readFile(p, 'utf8');
42
- }
43
- async function writeFileUTF8(p, content) {
44
- await ensureDir(dirname(p));
45
- await fs.writeFile(p, content, 'utf8');
46
- }
47
- async function getIntrospectionFromSDL(schemaPath) {
48
- const sdl = await readFileUTF8(schemaPath);
49
- const schema = buildSchema(sdl);
50
- const q = getIntrospectionQuery();
51
- const res = await graphql({ schema, source: q });
52
- return res.data;
53
- }
54
- async function getIntrospectionFromEndpoint(endpoint, headers) {
55
- const client = new GraphQLClient(endpoint, { headers });
56
- const q = getIntrospectionQuery();
57
- const res = await client.request(q);
58
- return res;
59
- }
60
- function generateKeyedObjFromGqlMap(gqlMap, selection, typeNameOverrides, typeIndex) {
61
- const gen = generateGql(gqlMap, selection, typeNameOverrides, typeIndex);
62
- return Object.entries(gen).reduce((acc, [key, val]) => {
63
- if (val?.ast)
64
- acc[key] = print(val.ast);
65
- return acc;
66
- }, {});
67
- }
68
- function applyQueryFilters(map, docs) {
69
- const allow = (docs.allowQueries || []).filter(Boolean);
70
- const exclude = (docs.excludeQueries || []).filter(Boolean);
71
- const patterns = (docs.excludePatterns || []).filter(Boolean);
72
- let keys = Object.keys(map);
73
- if (allow.length > 0)
74
- keys = keys.filter((k) => allow.includes(k));
75
- if (exclude.length > 0)
76
- keys = keys.filter((k) => !exclude.includes(k));
77
- if (patterns.length > 0) {
78
- const regs = patterns.map((p) => {
79
- try {
80
- return new RegExp(p);
81
- }
82
- catch {
83
- return null;
84
- }
85
- }).filter((r) => !!r);
86
- keys = keys.filter((k) => !regs.some((r) => r.test(k)));
87
- }
88
- return keys.reduce((acc, k) => {
89
- acc[k] = map[k];
90
- return acc;
91
- }, {});
92
- }
93
- async function writeOperationsDocuments(docs, dir, format, convention) {
94
- await ensureDir(dir);
95
- const index = [];
96
- for (const key of Object.keys(docs)) {
97
- const base = getFilename(key, convention);
98
- const filename = base + (format === 'ts' ? '.ts' : '.gql');
99
- if (format === 'ts') {
100
- const code = `import gql from 'graphql-tag'\nexport const ${key} = gql\`\n${docs[key]}\n\``;
101
- await writeFileUTF8(join(dir, filename), code);
102
- index.push(`export * from './${base}'`);
103
- }
104
- else {
105
- await writeFileUTF8(join(dir, filename), docs[key]);
106
- }
107
- }
108
- if (format === 'ts')
109
- await writeFileUTF8(join(dir, 'index.ts'), index.sort().join('\n'));
110
- }
111
- export async function runCodegen(opts, cwd) {
112
- const options = {
113
- input: { ...(defaultGraphQLCodegenOptions.input), ...(opts.input || {}) },
114
- output: { ...(defaultGraphQLCodegenOptions.output), ...(opts.output || {}) },
115
- documents: { ...(defaultGraphQLCodegenOptions.documents), ...(opts.documents || {}) },
116
- features: { ...(defaultGraphQLCodegenOptions.features), ...(opts.features || {}) },
117
- selection: { ...(defaultGraphQLCodegenOptions.selection), ...(opts.selection || {}) },
118
- scalars: { ...(defaultGraphQLCodegenOptions.scalars || {}), ...(opts.scalars || {}) },
119
- typeNameOverrides: { ...(defaultGraphQLCodegenOptions.typeNameOverrides || {}), ...(opts.typeNameOverrides || {}) }
120
- };
121
- const root = join(cwd, options.output.root);
122
- const typesFile = join(root, options.output.typesFile);
123
- const operationsDir = join(root, options.output.operationsDir);
124
- const sdkFile = join(root, options.output.sdkFile);
125
- const reactQueryFile = join(root, options.output.reactQueryFile || 'react-query.ts');
126
- const hasSchemaPath = !!options.input.schema && options.input.schema.trim() !== '';
127
- const hasEndpoint = !!options.input.endpoint && options.input.endpoint.trim() !== '';
128
- const schemaPath = hasSchemaPath ? (isAbsolute(options.input.schema) ? options.input.schema : resolve(cwd, options.input.schema)) : '';
129
- const introspection = hasEndpoint
130
- ? await getIntrospectionFromEndpoint(options.input.endpoint, options.input.headers || {})
131
- : await getIntrospectionFromSDL(schemaPath);
132
- const { queries, mutations } = parseGraphQuery(introspection);
133
- const gqlMap = applyQueryFilters({ ...queries, ...mutations }, options.documents);
134
- let docs = {};
135
- const schema = hasEndpoint
136
- ? buildClientSchema(introspection)
137
- : buildSchema(await readFileUTF8(schemaPath));
138
- const typeIndex = buildTypeIndex(introspection);
139
- if (options.features.emitOperations || options.features.emitSdk || options.features.emitReactQuery) {
140
- docs = generateKeyedObjFromGqlMap(gqlMap, options.selection, options.typeNameOverrides, typeIndex);
141
- }
142
- if (options.features.emitOperations) {
143
- await writeOperationsDocuments(docs, operationsDir, options.documents.format, options.documents.convention);
144
- }
145
- if (options.features.emitTypes) {
146
- const typesContent = await runCoreCodegen({
147
- filename: typesFile,
148
- schema: schema,
149
- documents: [],
150
- config: { scalars: options.scalars || {} },
151
- plugins: [{ typescript: {} }],
152
- pluginMap: { typescript: typescriptPlugin }
153
- });
154
- await writeFileUTF8(typesFile, typesContent);
155
- }
156
- if (options.features.emitSdk) {
157
- const documents = [];
158
- for (const [name, content] of Object.entries(docs)) {
159
- try {
160
- const doc = require('graphql').parse(content);
161
- documents.push({ location: name, document: doc });
162
- }
163
- catch (e) { }
164
- }
165
- const sdkContent = await runCoreCodegen({
166
- filename: sdkFile,
167
- schema: schema,
168
- documents,
169
- config: { scalars: options.scalars || {}, dedupeOperationSuffix: true },
170
- plugins: [
171
- { typescript: {} },
172
- { 'typescript-operations': {} },
173
- { 'typescript-graphql-request': { dedupeOperationSuffix: true } }
174
- ],
175
- pluginMap: {
176
- typescript: typescriptPlugin,
177
- 'typescript-operations': typescriptOperationsPlugin,
178
- 'typescript-graphql-request': typescriptGraphqlRequestPlugin
179
- }
180
- });
181
- // Fix TS2742: Add missing DocumentNode import using Babel AST
182
- const sdkContentWithImport = addDocumentNodeImport(sdkContent);
183
- await writeFileUTF8(sdkFile, sdkContentWithImport);
184
- }
185
- if (options.features.emitReactQuery) {
186
- const documents = [];
187
- for (const [name, content] of Object.entries(docs)) {
188
- try {
189
- const doc = require('graphql').parse(content);
190
- documents.push({ location: name, document: doc });
191
- }
192
- catch (e) { }
193
- }
194
- const rqConfig = {
195
- fetcher: options.reactQuery?.fetcher || 'graphql-request',
196
- legacyMode: options.reactQuery?.legacyMode || false,
197
- exposeDocument: options.reactQuery?.exposeDocument || false,
198
- addInfiniteQuery: options.reactQuery?.addInfiniteQuery || false,
199
- reactQueryVersion: options.reactQuery?.reactQueryVersion || 5,
200
- scalars: options.scalars || {}
201
- };
202
- const rqContent = await runCoreCodegen({
203
- filename: reactQueryFile,
204
- schema: schema,
205
- documents,
206
- config: { ...rqConfig, dedupeOperationSuffix: true },
207
- plugins: [
208
- { typescript: {} },
209
- { 'typescript-operations': {} },
210
- { 'typescript-react-query': rqConfig }
211
- ],
212
- pluginMap: {
213
- typescript: typescriptPlugin,
214
- 'typescript-operations': typescriptOperationsPlugin,
215
- 'typescript-react-query': typescriptReactQueryPlugin
216
- }
217
- });
218
- await writeFileUTF8(reactQueryFile, rqContent);
219
- }
220
- return { root, typesFile, operationsDir, sdkFile };
221
- }
222
- export async function runCodegenFromJSONConfig(configPath, cwd) {
223
- const path = isAbsolute(configPath) ? configPath : resolve(cwd, configPath);
224
- const content = await readFileUTF8(path);
225
- let overrides = {};
226
- try {
227
- overrides = JSON.parse(content);
228
- }
229
- catch (e) {
230
- throw new Error('Invalid JSON config: ' + e);
231
- }
232
- const merged = mergeGraphQLCodegenOptions(defaultGraphQLCodegenOptions, overrides);
233
- return runCodegen(merged, cwd);
234
- }
235
- function buildTypeIndex(introspection) {
236
- const byName = {};
237
- const types = (introspection && introspection.__schema && introspection.__schema.types) || [];
238
- for (const t of types) {
239
- if (t && typeof t.name === 'string' && t.name.length > 0)
240
- byName[t.name] = t;
241
- }
242
- return {
243
- byName,
244
- getInputFieldType(typeName, fieldName) {
245
- const typ = byName[typeName];
246
- if (!typ || typ.kind !== 'INPUT_OBJECT')
247
- return null;
248
- const fields = typ.inputFields || [];
249
- const f = fields.find((x) => x && x.name === fieldName);
250
- return f ? f.type : null;
251
- }
252
- };
253
- }