@constructive-io/graphql-codegen 2.24.0 → 2.26.0

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 (66) hide show
  1. package/README.md +403 -279
  2. package/cli/codegen/babel-ast.d.ts +7 -0
  3. package/cli/codegen/babel-ast.js +15 -0
  4. package/cli/codegen/barrel.js +43 -14
  5. package/cli/codegen/custom-mutations.js +4 -4
  6. package/cli/codegen/custom-queries.js +12 -22
  7. package/cli/codegen/gql-ast.js +22 -1
  8. package/cli/codegen/index.js +1 -0
  9. package/cli/codegen/mutations.d.ts +2 -0
  10. package/cli/codegen/mutations.js +26 -13
  11. package/cli/codegen/orm/client-generator.js +475 -136
  12. package/cli/codegen/orm/custom-ops-generator.js +8 -3
  13. package/cli/codegen/orm/input-types-generator.js +22 -0
  14. package/cli/codegen/orm/model-generator.js +18 -5
  15. package/cli/codegen/orm/select-types.d.ts +33 -0
  16. package/cli/codegen/queries.d.ts +1 -1
  17. package/cli/codegen/queries.js +112 -35
  18. package/cli/codegen/utils.d.ts +6 -0
  19. package/cli/codegen/utils.js +19 -0
  20. package/cli/commands/generate-orm.d.ts +14 -0
  21. package/cli/commands/generate-orm.js +160 -44
  22. package/cli/commands/generate.d.ts +22 -0
  23. package/cli/commands/generate.js +195 -55
  24. package/cli/commands/init.js +29 -9
  25. package/cli/index.js +133 -28
  26. package/cli/watch/orchestrator.d.ts +4 -0
  27. package/cli/watch/orchestrator.js +4 -0
  28. package/esm/cli/codegen/babel-ast.d.ts +7 -0
  29. package/esm/cli/codegen/babel-ast.js +14 -0
  30. package/esm/cli/codegen/barrel.js +44 -15
  31. package/esm/cli/codegen/custom-mutations.js +5 -5
  32. package/esm/cli/codegen/custom-queries.js +13 -23
  33. package/esm/cli/codegen/gql-ast.js +23 -2
  34. package/esm/cli/codegen/index.js +1 -0
  35. package/esm/cli/codegen/mutations.d.ts +2 -0
  36. package/esm/cli/codegen/mutations.js +27 -14
  37. package/esm/cli/codegen/orm/client-generator.js +475 -136
  38. package/esm/cli/codegen/orm/custom-ops-generator.js +8 -3
  39. package/esm/cli/codegen/orm/input-types-generator.js +22 -0
  40. package/esm/cli/codegen/orm/model-generator.js +18 -5
  41. package/esm/cli/codegen/orm/select-types.d.ts +33 -0
  42. package/esm/cli/codegen/queries.d.ts +1 -1
  43. package/esm/cli/codegen/queries.js +114 -37
  44. package/esm/cli/codegen/utils.d.ts +6 -0
  45. package/esm/cli/codegen/utils.js +18 -0
  46. package/esm/cli/commands/generate-orm.d.ts +14 -0
  47. package/esm/cli/commands/generate-orm.js +161 -45
  48. package/esm/cli/commands/generate.d.ts +22 -0
  49. package/esm/cli/commands/generate.js +195 -56
  50. package/esm/cli/commands/init.js +29 -9
  51. package/esm/cli/index.js +134 -29
  52. package/esm/cli/watch/orchestrator.d.ts +4 -0
  53. package/esm/cli/watch/orchestrator.js +5 -1
  54. package/esm/types/config.d.ts +39 -2
  55. package/esm/types/config.js +88 -4
  56. package/esm/types/index.d.ts +2 -2
  57. package/esm/types/index.js +1 -1
  58. package/package.json +10 -7
  59. package/types/config.d.ts +39 -2
  60. package/types/config.js +91 -4
  61. package/types/index.d.ts +2 -2
  62. package/types/index.js +2 -1
  63. package/cli/codegen/orm/query-builder.d.ts +0 -161
  64. package/cli/codegen/orm/query-builder.js +0 -366
  65. package/esm/cli/codegen/orm/query-builder.d.ts +0 -161
  66. package/esm/cli/codegen/orm/query-builder.js +0 -353
@@ -17,6 +17,8 @@ export interface MutationGeneratorOptions {
17
17
  enumsFromSchemaTypes?: string[];
18
18
  useCentralizedKeys?: boolean;
19
19
  hasRelationships?: boolean;
20
+ /** All table type names for determining which types to import from types.ts vs schema-types.ts */
21
+ tableTypeNames?: Set<string>;
20
22
  }
21
23
  export declare function generateCreateMutationHook(table: CleanTable, options?: MutationGeneratorOptions): GeneratedMutationFile | null;
22
24
  export declare function generateUpdateMutationHook(table: CleanTable, options?: MutationGeneratorOptions): GeneratedMutationFile | null;
@@ -1,5 +1,5 @@
1
1
  import * as t from '@babel/types';
2
- import { generateCode, addJSDocComment, typedParam } from './babel-ast';
2
+ import { generateCode, addJSDocComment, typedParam, createTypedCallExpression } from './babel-ast';
3
3
  import { buildCreateMutationAST, buildUpdateMutationAST, buildDeleteMutationAST, printGraphQL, } from './gql-ast';
4
4
  import { getTableNames, getCreateMutationHookName, getUpdateMutationHookName, getDeleteMutationHookName, getCreateMutationFileName, getUpdateMutationFileName, getDeleteMutationFileName, getCreateMutationName, getUpdateMutationName, getDeleteMutationName, getScalarFields, getPrimaryKeyInfo, fieldTypeToTs, ucFirst, lcFirst, getGeneratedFileHeader, } from './utils';
5
5
  function isAutoGeneratedField(fieldName, pkFieldNames) {
@@ -14,7 +14,7 @@ function isAutoGeneratedField(fieldName, pkFieldNames) {
14
14
  return timestampPatterns.includes(name);
15
15
  }
16
16
  export function generateCreateMutationHook(table, options = {}) {
17
- const { reactQueryEnabled = true, enumsFromSchemaTypes = [], useCentralizedKeys = true, hasRelationships = false, } = options;
17
+ const { reactQueryEnabled = true, enumsFromSchemaTypes = [], useCentralizedKeys = true, hasRelationships = false, tableTypeNames = new Set(), } = options;
18
18
  if (!reactQueryEnabled) {
19
19
  return null;
20
20
  }
@@ -28,11 +28,16 @@ export function generateCreateMutationHook(table, options = {}) {
28
28
  const scalarFields = getScalarFields(table);
29
29
  const pkFieldNames = new Set(getPrimaryKeyInfo(table).map((pk) => pk.name));
30
30
  const usedEnums = new Set();
31
+ const usedTableTypes = new Set();
31
32
  for (const field of scalarFields) {
32
33
  const cleanType = field.type.gqlType.replace(/!/g, '');
33
34
  if (enumSet.has(cleanType)) {
34
35
  usedEnums.add(cleanType);
35
36
  }
37
+ else if (tableTypeNames.has(cleanType) && cleanType !== typeName) {
38
+ // Track table types used in scalar fields (excluding the main type which is already imported)
39
+ usedTableTypes.add(cleanType);
40
+ }
36
41
  }
37
42
  const mutationAST = buildCreateMutationAST({ table });
38
43
  const mutationDocument = printGraphQL(mutationAST);
@@ -47,7 +52,9 @@ export function generateCreateMutationHook(table, options = {}) {
47
52
  statements.push(reactQueryTypeImport);
48
53
  const clientImport = t.importDeclaration([t.importSpecifier(t.identifier('execute'), t.identifier('execute'))], t.stringLiteral('../client'));
49
54
  statements.push(clientImport);
50
- const typesImport = t.importDeclaration([t.importSpecifier(t.identifier(typeName), t.identifier(typeName))], t.stringLiteral('../types'));
55
+ // Import the main type and any other table types used in scalar fields
56
+ const allTypesToImport = [typeName, ...Array.from(usedTableTypes)].sort();
57
+ const typesImport = t.importDeclaration(allTypesToImport.map((t_) => t.importSpecifier(t.identifier(t_), t.identifier(t_))), t.stringLiteral('../types'));
51
58
  typesImport.importKind = 'type';
52
59
  statements.push(typesImport);
53
60
  if (usedEnums.size > 0) {
@@ -108,9 +115,9 @@ export function generateCreateMutationHook(table, options = {}) {
108
115
  if (useCentralizedKeys) {
109
116
  mutationOptions.push(t.objectProperty(t.identifier('mutationKey'), t.callExpression(t.memberExpression(t.identifier(mutationKeysName), t.identifier('create')), [])));
110
117
  }
111
- mutationOptions.push(t.objectProperty(t.identifier('mutationFn'), t.arrowFunctionExpression([typedParam('variables', t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)))], t.callExpression(t.identifier('execute'), [
112
- t.identifier(`${mutationName}MutationDocument`),
113
- t.identifier('variables'),
118
+ mutationOptions.push(t.objectProperty(t.identifier('mutationFn'), t.arrowFunctionExpression([typedParam('variables', t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)))], createTypedCallExpression(t.identifier('execute'), [t.identifier(`${mutationName}MutationDocument`), t.identifier('variables')], [
119
+ t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationResult`)),
120
+ t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)),
114
121
  ]))));
115
122
  const invalidateQueryKey = useCentralizedKeys
116
123
  ? t.callExpression(t.memberExpression(t.identifier(keysName), t.identifier('lists')), [])
@@ -151,7 +158,7 @@ export function generateCreateMutationHook(table, options = {}) {
151
158
  };
152
159
  }
153
160
  export function generateUpdateMutationHook(table, options = {}) {
154
- const { reactQueryEnabled = true, enumsFromSchemaTypes = [], useCentralizedKeys = true, hasRelationships = false, } = options;
161
+ const { reactQueryEnabled = true, enumsFromSchemaTypes = [], useCentralizedKeys = true, hasRelationships = false, tableTypeNames = new Set(), } = options;
155
162
  if (!reactQueryEnabled) {
156
163
  return null;
157
164
  }
@@ -170,11 +177,15 @@ export function generateUpdateMutationHook(table, options = {}) {
170
177
  const pkField = pkFields[0];
171
178
  const pkFieldNames = new Set(pkFields.map((pk) => pk.name));
172
179
  const usedEnums = new Set();
180
+ const usedTableTypes = new Set();
173
181
  for (const field of scalarFields) {
174
182
  const cleanType = field.type.gqlType.replace(/!/g, '');
175
183
  if (enumSet.has(cleanType)) {
176
184
  usedEnums.add(cleanType);
177
185
  }
186
+ else if (tableTypeNames.has(cleanType) && cleanType !== typeName) {
187
+ usedTableTypes.add(cleanType);
188
+ }
178
189
  }
179
190
  const mutationAST = buildUpdateMutationAST({ table });
180
191
  const mutationDocument = printGraphQL(mutationAST);
@@ -189,7 +200,9 @@ export function generateUpdateMutationHook(table, options = {}) {
189
200
  statements.push(reactQueryTypeImport);
190
201
  const clientImport = t.importDeclaration([t.importSpecifier(t.identifier('execute'), t.identifier('execute'))], t.stringLiteral('../client'));
191
202
  statements.push(clientImport);
192
- const typesImport = t.importDeclaration([t.importSpecifier(t.identifier(typeName), t.identifier(typeName))], t.stringLiteral('../types'));
203
+ // Import the main type and any other table types used in scalar fields
204
+ const allTypesToImportUpdate = [typeName, ...Array.from(usedTableTypes)].sort();
205
+ const typesImport = t.importDeclaration(allTypesToImportUpdate.map((t_) => t.importSpecifier(t.identifier(t_), t.identifier(t_))), t.stringLiteral('../types'));
193
206
  typesImport.importKind = 'type';
194
207
  statements.push(typesImport);
195
208
  if (usedEnums.size > 0) {
@@ -256,9 +269,9 @@ export function generateUpdateMutationHook(table, options = {}) {
256
269
  if (useCentralizedKeys) {
257
270
  mutationOptions.push(t.objectProperty(t.identifier('mutationKey'), t.memberExpression(t.identifier(mutationKeysName), t.identifier('all'))));
258
271
  }
259
- mutationOptions.push(t.objectProperty(t.identifier('mutationFn'), t.arrowFunctionExpression([typedParam('variables', t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)))], t.callExpression(t.identifier('execute'), [
260
- t.identifier(`${mutationName}MutationDocument`),
261
- t.identifier('variables'),
272
+ mutationOptions.push(t.objectProperty(t.identifier('mutationFn'), t.arrowFunctionExpression([typedParam('variables', t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)))], createTypedCallExpression(t.identifier('execute'), [t.identifier(`${mutationName}MutationDocument`), t.identifier('variables')], [
273
+ t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationResult`)),
274
+ t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)),
262
275
  ]))));
263
276
  const detailQueryKey = useCentralizedKeys
264
277
  ? t.callExpression(t.memberExpression(t.identifier(keysName), t.identifier('detail')), [t.memberExpression(t.memberExpression(t.identifier('variables'), t.identifier('input')), t.identifier(pkField.name))])
@@ -378,9 +391,9 @@ export function generateDeleteMutationHook(table, options = {}) {
378
391
  if (useCentralizedKeys) {
379
392
  mutationOptions.push(t.objectProperty(t.identifier('mutationKey'), t.memberExpression(t.identifier(mutationKeysName), t.identifier('all'))));
380
393
  }
381
- mutationOptions.push(t.objectProperty(t.identifier('mutationFn'), t.arrowFunctionExpression([typedParam('variables', t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)))], t.callExpression(t.identifier('execute'), [
382
- t.identifier(`${mutationName}MutationDocument`),
383
- t.identifier('variables'),
394
+ mutationOptions.push(t.objectProperty(t.identifier('mutationFn'), t.arrowFunctionExpression([typedParam('variables', t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)))], createTypedCallExpression(t.identifier('execute'), [t.identifier(`${mutationName}MutationDocument`), t.identifier('variables')], [
395
+ t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationResult`)),
396
+ t.tsTypeReference(t.identifier(`${ucFirst(mutationName)}MutationVariables`)),
384
397
  ]))));
385
398
  const detailQueryKey = useCentralizedKeys
386
399
  ? t.callExpression(t.memberExpression(t.identifier(keysName), t.identifier('detail')), [t.memberExpression(t.memberExpression(t.identifier('variables'), t.identifier('input')), t.identifier(pkField.name))])