@constructive-io/graphql-codegen 3.2.1 → 3.3.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 (215) hide show
  1. package/cli/index.js +36 -43
  2. package/cli/shared.d.ts +15 -19
  3. package/cli/shared.js +104 -25
  4. package/client/error.js +31 -9
  5. package/client/execute.js +2 -2
  6. package/client/index.d.ts +3 -3
  7. package/client/index.js +6 -6
  8. package/core/ast.d.ts +1 -1
  9. package/core/ast.js +1 -1
  10. package/core/codegen/babel-ast.d.ts +1 -1
  11. package/core/codegen/babel-ast.js +2 -2
  12. package/core/codegen/barrel.d.ts +0 -6
  13. package/core/codegen/barrel.js +22 -19
  14. package/core/codegen/client.d.ts +2 -12
  15. package/core/codegen/client.js +7 -21
  16. package/core/codegen/custom-mutations.d.ts +0 -14
  17. package/core/codegen/custom-mutations.js +139 -88
  18. package/core/codegen/custom-queries.d.ts +0 -14
  19. package/core/codegen/custom-queries.js +483 -193
  20. package/core/codegen/hooks-ast.d.ts +75 -0
  21. package/core/codegen/hooks-ast.js +522 -0
  22. package/core/codegen/index.d.ts +16 -18
  23. package/core/codegen/index.js +42 -88
  24. package/core/codegen/invalidation.d.ts +1 -7
  25. package/core/codegen/invalidation.js +50 -16
  26. package/core/codegen/mutation-keys.d.ts +1 -10
  27. package/core/codegen/mutation-keys.js +22 -8
  28. package/core/codegen/mutations.d.ts +0 -13
  29. package/core/codegen/mutations.js +301 -366
  30. package/core/codegen/orm/barrel.d.ts +0 -5
  31. package/core/codegen/orm/barrel.js +5 -0
  32. package/core/codegen/orm/client-generator.d.ts +0 -5
  33. package/core/codegen/orm/client-generator.js +7 -2
  34. package/core/codegen/orm/client.js +3 -1
  35. package/core/codegen/orm/custom-ops-generator.d.ts +0 -6
  36. package/core/codegen/orm/custom-ops-generator.js +104 -51
  37. package/core/codegen/orm/index.d.ts +4 -4
  38. package/core/codegen/orm/index.js +28 -15
  39. package/core/codegen/orm/input-types-generator.d.ts +1 -13
  40. package/core/codegen/orm/input-types-generator.js +85 -23
  41. package/core/codegen/orm/model-generator.d.ts +0 -5
  42. package/core/codegen/orm/model-generator.js +309 -131
  43. package/core/codegen/orm/select-types.d.ts +19 -14
  44. package/core/codegen/queries.d.ts +0 -8
  45. package/core/codegen/queries.js +360 -559
  46. package/core/codegen/query-keys.d.ts +1 -1
  47. package/core/codegen/query-keys.js +37 -23
  48. package/core/codegen/scalars.js +3 -1
  49. package/core/codegen/schema-types-generator.d.ts +1 -1
  50. package/core/codegen/schema-types-generator.js +17 -2
  51. package/core/codegen/select-helpers.d.ts +19 -0
  52. package/core/codegen/select-helpers.js +40 -0
  53. package/core/codegen/selection.d.ts +4 -0
  54. package/core/codegen/selection.js +65 -0
  55. package/core/codegen/shared/index.d.ts +2 -15
  56. package/core/codegen/shared/index.js +17 -4
  57. package/core/codegen/templates/hooks-client.ts +49 -0
  58. package/core/codegen/templates/hooks-selection.ts +58 -0
  59. package/core/codegen/templates/orm-client.ts +8 -6
  60. package/core/codegen/templates/query-builder.ts +250 -46
  61. package/core/codegen/templates/select-types.ts +31 -14
  62. package/core/codegen/type-resolver.d.ts +1 -5
  63. package/core/codegen/type-resolver.js +0 -22
  64. package/core/codegen/types.d.ts +0 -3
  65. package/core/codegen/types.js +71 -14
  66. package/core/codegen/utils.d.ts +1 -4
  67. package/core/codegen/utils.js +4 -1
  68. package/core/config/index.d.ts +1 -1
  69. package/core/config/resolver.js +1 -3
  70. package/core/generate.js +38 -50
  71. package/core/index.d.ts +3 -3
  72. package/core/index.js +3 -4
  73. package/core/introspect/index.d.ts +6 -6
  74. package/core/introspect/index.js +5 -8
  75. package/core/introspect/infer-tables.d.ts +0 -14
  76. package/core/introspect/infer-tables.js +15 -1
  77. package/core/introspect/source/database.js +1 -1
  78. package/core/introspect/source/endpoint.d.ts +0 -6
  79. package/core/introspect/source/endpoint.js +7 -1
  80. package/core/introspect/source/index.d.ts +4 -4
  81. package/core/introspect/source/index.js +5 -9
  82. package/core/introspect/source/pgpm-module.js +3 -3
  83. package/core/introspect/transform-schema.d.ts +2 -2
  84. package/core/introspect/transform-schema.js +2 -2
  85. package/core/output/index.d.ts +1 -1
  86. package/core/output/index.js +2 -2
  87. package/core/output/writer.d.ts +3 -0
  88. package/core/output/writer.js +20 -1
  89. package/core/pipeline/index.d.ts +2 -2
  90. package/core/query-builder.d.ts +2 -2
  91. package/core/query-builder.js +1 -1
  92. package/core/watch/index.d.ts +4 -4
  93. package/core/watch/index.js +9 -9
  94. package/core/watch/orchestrator.js +5 -3
  95. package/esm/cli/index.js +37 -44
  96. package/esm/cli/shared.d.ts +15 -19
  97. package/esm/cli/shared.js +94 -23
  98. package/esm/client/error.js +31 -9
  99. package/esm/client/execute.js +2 -2
  100. package/esm/client/index.d.ts +3 -3
  101. package/esm/client/index.js +3 -3
  102. package/esm/core/ast.d.ts +1 -1
  103. package/esm/core/ast.js +1 -1
  104. package/esm/core/codegen/babel-ast.d.ts +1 -1
  105. package/esm/core/codegen/babel-ast.js +2 -2
  106. package/esm/core/codegen/barrel.d.ts +0 -6
  107. package/esm/core/codegen/barrel.js +23 -20
  108. package/esm/core/codegen/client.d.ts +2 -12
  109. package/esm/core/codegen/client.js +7 -21
  110. package/esm/core/codegen/custom-mutations.d.ts +0 -14
  111. package/esm/core/codegen/custom-mutations.js +141 -90
  112. package/esm/core/codegen/custom-queries.d.ts +0 -14
  113. package/esm/core/codegen/custom-queries.js +486 -196
  114. package/esm/core/codegen/hooks-ast.d.ts +75 -0
  115. package/esm/core/codegen/hooks-ast.js +424 -0
  116. package/esm/core/codegen/index.d.ts +16 -18
  117. package/esm/core/codegen/index.js +26 -71
  118. package/esm/core/codegen/invalidation.d.ts +1 -7
  119. package/esm/core/codegen/invalidation.js +51 -17
  120. package/esm/core/codegen/mutation-keys.d.ts +1 -10
  121. package/esm/core/codegen/mutation-keys.js +23 -9
  122. package/esm/core/codegen/mutations.d.ts +0 -13
  123. package/esm/core/codegen/mutations.js +302 -367
  124. package/esm/core/codegen/orm/barrel.d.ts +0 -5
  125. package/esm/core/codegen/orm/barrel.js +6 -1
  126. package/esm/core/codegen/orm/client-generator.d.ts +0 -5
  127. package/esm/core/codegen/orm/client-generator.js +7 -2
  128. package/esm/core/codegen/orm/client.js +3 -1
  129. package/esm/core/codegen/orm/custom-ops-generator.d.ts +0 -6
  130. package/esm/core/codegen/orm/custom-ops-generator.js +103 -50
  131. package/esm/core/codegen/orm/index.d.ts +4 -4
  132. package/esm/core/codegen/orm/index.js +25 -12
  133. package/esm/core/codegen/orm/input-types-generator.d.ts +1 -13
  134. package/esm/core/codegen/orm/input-types-generator.js +85 -23
  135. package/esm/core/codegen/orm/model-generator.d.ts +0 -5
  136. package/esm/core/codegen/orm/model-generator.js +310 -132
  137. package/esm/core/codegen/orm/select-types.d.ts +19 -14
  138. package/esm/core/codegen/queries.d.ts +0 -8
  139. package/esm/core/codegen/queries.js +362 -561
  140. package/esm/core/codegen/query-keys.d.ts +1 -1
  141. package/esm/core/codegen/query-keys.js +38 -24
  142. package/esm/core/codegen/scalars.js +3 -1
  143. package/esm/core/codegen/schema-types-generator.d.ts +1 -1
  144. package/esm/core/codegen/schema-types-generator.js +17 -2
  145. package/esm/core/codegen/select-helpers.d.ts +19 -0
  146. package/esm/core/codegen/select-helpers.js +35 -0
  147. package/esm/core/codegen/selection.d.ts +4 -0
  148. package/esm/core/codegen/selection.js +29 -0
  149. package/esm/core/codegen/shared/index.d.ts +2 -15
  150. package/esm/core/codegen/shared/index.js +16 -3
  151. package/esm/core/codegen/type-resolver.d.ts +1 -5
  152. package/esm/core/codegen/type-resolver.js +1 -22
  153. package/esm/core/codegen/types.d.ts +0 -3
  154. package/esm/core/codegen/types.js +72 -15
  155. package/esm/core/codegen/utils.d.ts +1 -4
  156. package/esm/core/codegen/utils.js +4 -1
  157. package/esm/core/config/index.d.ts +1 -1
  158. package/esm/core/config/resolver.js +2 -4
  159. package/esm/core/generate.js +38 -50
  160. package/esm/core/index.d.ts +3 -3
  161. package/esm/core/index.js +2 -3
  162. package/esm/core/introspect/index.d.ts +6 -6
  163. package/esm/core/introspect/index.js +3 -6
  164. package/esm/core/introspect/infer-tables.d.ts +0 -14
  165. package/esm/core/introspect/infer-tables.js +16 -2
  166. package/esm/core/introspect/source/database.js +2 -2
  167. package/esm/core/introspect/source/endpoint.d.ts +0 -6
  168. package/esm/core/introspect/source/endpoint.js +7 -1
  169. package/esm/core/introspect/source/index.d.ts +4 -4
  170. package/esm/core/introspect/source/index.js +6 -10
  171. package/esm/core/introspect/source/pgpm-module.js +3 -3
  172. package/esm/core/introspect/transform-schema.d.ts +2 -2
  173. package/esm/core/introspect/transform-schema.js +2 -2
  174. package/esm/core/output/index.d.ts +1 -1
  175. package/esm/core/output/index.js +1 -1
  176. package/esm/core/output/writer.d.ts +3 -0
  177. package/esm/core/output/writer.js +20 -1
  178. package/esm/core/pipeline/index.d.ts +2 -2
  179. package/esm/core/pipeline/index.js +2 -2
  180. package/esm/core/query-builder.d.ts +2 -2
  181. package/esm/core/query-builder.js +2 -2
  182. package/esm/core/watch/index.d.ts +4 -4
  183. package/esm/core/watch/index.js +3 -3
  184. package/esm/core/watch/orchestrator.js +5 -3
  185. package/esm/generators/index.d.ts +3 -3
  186. package/esm/generators/index.js +3 -3
  187. package/esm/generators/mutations.d.ts +1 -1
  188. package/esm/generators/select.d.ts +1 -1
  189. package/esm/index.d.ts +3 -3
  190. package/esm/index.js +1 -4
  191. package/esm/types/config.d.ts +0 -10
  192. package/esm/types/config.js +0 -2
  193. package/esm/types/index.d.ts +6 -6
  194. package/esm/types/index.js +1 -1
  195. package/generators/index.d.ts +3 -3
  196. package/generators/index.js +8 -8
  197. package/generators/mutations.d.ts +1 -1
  198. package/generators/select.d.ts +1 -1
  199. package/index.d.ts +3 -3
  200. package/index.js +11 -6
  201. package/package.json +10 -10
  202. package/types/config.d.ts +0 -10
  203. package/types/config.js +0 -2
  204. package/types/index.d.ts +6 -6
  205. package/types/index.js +2 -2
  206. package/core/codegen/gql-ast.d.ts +0 -41
  207. package/core/codegen/gql-ast.js +0 -353
  208. package/core/codegen/schema-gql-ast.d.ts +0 -51
  209. package/core/codegen/schema-gql-ast.js +0 -385
  210. package/core/codegen/templates/client.browser.ts +0 -271
  211. package/core/codegen/templates/client.node.ts +0 -337
  212. package/esm/core/codegen/gql-ast.d.ts +0 -41
  213. package/esm/core/codegen/gql-ast.js +0 -312
  214. package/esm/core/codegen/schema-gql-ast.d.ts +0 -51
  215. package/esm/core/codegen/schema-gql-ast.js +0 -343
@@ -35,243 +35,534 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.generateCustomQueryHook = generateCustomQueryHook;
37
37
  exports.generateAllCustomQueryHooks = generateAllCustomQueryHooks;
38
+ /**
39
+ * Custom query hook generators for non-table operations (Babel AST-based)
40
+ *
41
+ * Generates hooks for operations discovered via schema introspection
42
+ * that are NOT table CRUD operations (e.g., currentUser, nodeById, etc.)
43
+ *
44
+ * Delegates to ORM custom query operations:
45
+ * getClient().query.operationName(args, { select }).unwrap()
46
+ *
47
+ * Output structure:
48
+ * queries/
49
+ * useCurrentUserQuery.ts
50
+ * useNodeQuery.ts
51
+ * ...
52
+ */
38
53
  const t = __importStar(require("@babel/types"));
39
54
  const babel_ast_1 = require("./babel-ast");
40
- const schema_gql_ast_1 = require("./schema-gql-ast");
55
+ const hooks_ast_1 = require("./hooks-ast");
56
+ const select_helpers_1 = require("./select-helpers");
41
57
  const type_resolver_1 = require("./type-resolver");
42
58
  const utils_1 = require("./utils");
43
- function generateVariablesProperties(args, tracker) {
44
- return args.map((arg) => ({
45
- name: arg.name,
46
- type: (0, type_resolver_1.typeRefToTsType)(arg.type, tracker),
47
- optional: !(0, type_resolver_1.isTypeRequired)(arg.type),
48
- docs: arg.description ? [arg.description] : undefined,
49
- }));
50
- }
51
59
  function generateCustomQueryHook(options) {
52
- const { operation, typeRegistry, maxDepth = 2, skipQueryField = true, reactQueryEnabled = true, tableTypeNames, useCentralizedKeys = true, } = options;
60
+ const { operation, typeRegistry, reactQueryEnabled = true, tableTypeNames, useCentralizedKeys = true, } = options;
53
61
  const hookName = (0, type_resolver_1.getOperationHookName)(operation.name, 'query');
54
62
  const fileName = (0, type_resolver_1.getOperationFileName)(operation.name, 'query');
55
- const variablesTypeName = (0, type_resolver_1.getOperationVariablesTypeName)(operation.name, 'query');
56
- const resultTypeName = (0, type_resolver_1.getOperationResultTypeName)(operation.name, 'query');
57
- const documentConstName = (0, type_resolver_1.getDocumentConstName)(operation.name, 'query');
58
63
  const queryKeyName = (0, type_resolver_1.getQueryKeyName)(operation.name);
64
+ const varTypeName = `${(0, utils_1.ucFirst)(operation.name)}Variables`;
59
65
  const tracker = (0, type_resolver_1.createTypeTracker)({ tableTypeNames });
60
- const queryDocument = (0, schema_gql_ast_1.buildCustomQueryString)({
61
- operation,
62
- typeRegistry,
63
- maxDepth,
64
- skipQueryField,
65
- });
66
- const statements = [];
67
- const variablesProps = operation.args.length > 0
68
- ? generateVariablesProperties(operation.args, tracker)
69
- : [];
66
+ const hasArgs = operation.args.length > 0;
67
+ const hasRequiredArgs = operation.args.some((arg) => (0, type_resolver_1.isTypeRequired)(arg.type));
70
68
  const resultType = (0, type_resolver_1.typeRefToTsType)(operation.returnType, tracker);
71
- const schemaTypes = tracker.getImportableTypes();
72
- const tableTypes = tracker.getTableTypes();
69
+ for (const arg of operation.args) {
70
+ (0, type_resolver_1.typeRefToTsType)(arg.type, tracker);
71
+ }
72
+ const selectTypeName = (0, select_helpers_1.getSelectTypeName)(operation.returnType);
73
+ const payloadTypeName = (0, type_resolver_1.getTypeBaseName)(operation.returnType);
74
+ const hasSelect = !!selectTypeName && !!payloadTypeName;
75
+ const statements = [];
76
+ // Imports
73
77
  if (reactQueryEnabled) {
74
- const reactQueryImport = t.importDeclaration([t.importSpecifier(t.identifier('useQuery'), t.identifier('useQuery'))], t.stringLiteral('@tanstack/react-query'));
75
- statements.push(reactQueryImport);
76
- const reactQueryTypeImport = t.importDeclaration([
77
- t.importSpecifier(t.identifier('UseQueryOptions'), t.identifier('UseQueryOptions')),
78
- t.importSpecifier(t.identifier('QueryClient'), t.identifier('QueryClient')),
79
- ], t.stringLiteral('@tanstack/react-query'));
80
- reactQueryTypeImport.importKind = 'type';
81
- statements.push(reactQueryTypeImport);
78
+ statements.push((0, hooks_ast_1.createImportDeclaration)('@tanstack/react-query', ['useQuery']));
79
+ statements.push((0, hooks_ast_1.createImportDeclaration)('@tanstack/react-query', ['UseQueryOptions', 'UseQueryResult', 'QueryClient'], true));
82
80
  }
83
- const clientImport = t.importDeclaration([t.importSpecifier(t.identifier('execute'), t.identifier('execute'))], t.stringLiteral('../client'));
84
- statements.push(clientImport);
85
- const clientTypeImport = t.importDeclaration([t.importSpecifier(t.identifier('ExecuteOptions'), t.identifier('ExecuteOptions'))], t.stringLiteral('../client'));
86
- clientTypeImport.importKind = 'type';
87
- statements.push(clientTypeImport);
88
- if (tableTypes.length > 0) {
89
- const typesImport = t.importDeclaration(tableTypes.map((tt) => t.importSpecifier(t.identifier(tt), t.identifier(tt))), t.stringLiteral('../types'));
90
- typesImport.importKind = 'type';
91
- statements.push(typesImport);
81
+ statements.push((0, hooks_ast_1.createImportDeclaration)('../client', ['getClient']));
82
+ statements.push((0, hooks_ast_1.createImportDeclaration)('../selection', ['buildSelectionArgs']));
83
+ statements.push((0, hooks_ast_1.createImportDeclaration)('../selection', ['SelectionConfig'], true));
84
+ if (useCentralizedKeys) {
85
+ statements.push((0, hooks_ast_1.createImportDeclaration)('../query-keys', ['customQueryKeys']));
92
86
  }
93
- if (schemaTypes.length > 0) {
94
- const schemaTypesImport = t.importDeclaration(schemaTypes.map((st) => t.importSpecifier(t.identifier(st), t.identifier(st))), t.stringLiteral('../schema-types'));
95
- schemaTypesImport.importKind = 'type';
96
- statements.push(schemaTypesImport);
87
+ if (hasArgs) {
88
+ statements.push((0, hooks_ast_1.createImportDeclaration)('../../orm/query', [varTypeName], true));
97
89
  }
98
- if (useCentralizedKeys) {
99
- const queryKeyImport = t.importDeclaration([t.importSpecifier(t.identifier('customQueryKeys'), t.identifier('customQueryKeys'))], t.stringLiteral('../query-keys'));
100
- statements.push(queryKeyImport);
90
+ const inputTypeImports = [];
91
+ if (hasSelect) {
92
+ inputTypeImports.push(selectTypeName);
93
+ inputTypeImports.push(payloadTypeName);
94
+ }
95
+ else {
96
+ const baseName = (0, type_resolver_1.getTypeBaseName)(operation.returnType);
97
+ if (baseName && !tracker.referencedTypes.has('__skip__')) {
98
+ for (const refType of tracker.referencedTypes) {
99
+ if (!inputTypeImports.includes(refType)) {
100
+ inputTypeImports.push(refType);
101
+ }
102
+ }
103
+ }
104
+ }
105
+ if (inputTypeImports.length > 0) {
106
+ statements.push((0, hooks_ast_1.createImportDeclaration)('../../orm/input-types', inputTypeImports, true));
107
+ }
108
+ if (hasSelect) {
109
+ statements.push((0, hooks_ast_1.createImportDeclaration)('../../orm/select-types', ['InferSelectResult', 'StrictSelect'], true));
101
110
  }
102
- const queryDocConst = t.variableDeclaration('const', [
103
- t.variableDeclarator(t.identifier(documentConstName), t.templateLiteral([t.templateElement({ raw: '\n' + queryDocument, cooked: '\n' + queryDocument }, true)], [])),
104
- ]);
105
- const queryDocExport = t.exportNamedDeclaration(queryDocConst);
106
- (0, babel_ast_1.addJSDocComment)(queryDocExport, ['GraphQL query document']);
107
- statements.push(queryDocExport);
108
- if (operation.args.length > 0) {
109
- const variablesInterfaceProps = variablesProps.map((vp) => {
110
- const prop = t.tsPropertySignature(t.identifier(vp.name), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(vp.type))));
111
- prop.optional = vp.optional;
112
- return prop;
113
- });
114
- const variablesInterface = t.tsInterfaceDeclaration(t.identifier(variablesTypeName), null, null, t.tsInterfaceBody(variablesInterfaceProps));
115
- statements.push(t.exportNamedDeclaration(variablesInterface));
111
+ // Re-exports
112
+ if (hasArgs) {
113
+ statements.push((0, hooks_ast_1.createTypeReExport)([varTypeName], '../../orm/query'));
114
+ }
115
+ if (hasSelect) {
116
+ statements.push((0, hooks_ast_1.createTypeReExport)([selectTypeName], '../../orm/input-types'));
116
117
  }
117
- const resultInterfaceBody = t.tsInterfaceBody([
118
- t.tsPropertySignature(t.identifier(operation.name), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(resultType)))),
119
- ]);
120
- const resultInterface = t.tsInterfaceDeclaration(t.identifier(resultTypeName), null, null, resultInterfaceBody);
121
- statements.push(t.exportNamedDeclaration(resultInterface));
118
+ // Query key
122
119
  if (useCentralizedKeys) {
123
- const queryKeyConst = t.variableDeclaration('const', [
120
+ const keyDecl = t.exportNamedDeclaration(t.variableDeclaration('const', [
124
121
  t.variableDeclarator(t.identifier(queryKeyName), t.memberExpression(t.identifier('customQueryKeys'), t.identifier(operation.name))),
122
+ ]));
123
+ (0, hooks_ast_1.addJSDocComment)(keyDecl, [
124
+ 'Query key factory - re-exported from query-keys.ts',
125
125
  ]);
126
- const queryKeyExport = t.exportNamedDeclaration(queryKeyConst);
127
- (0, babel_ast_1.addJSDocComment)(queryKeyExport, ['Query key factory - re-exported from query-keys.ts']);
128
- statements.push(queryKeyExport);
126
+ statements.push(keyDecl);
129
127
  }
130
- else if (operation.args.length > 0) {
131
- const queryKeyArrow = t.arrowFunctionExpression([(0, babel_ast_1.typedParam)('variables', t.tsTypeReference(t.identifier(variablesTypeName)), true)], t.tsAsExpression(t.arrayExpression([t.stringLiteral(operation.name), t.identifier('variables')]), t.tsTypeReference(t.identifier('const'))));
132
- const queryKeyConst = t.variableDeclaration('const', [
133
- t.variableDeclarator(t.identifier(queryKeyName), queryKeyArrow),
134
- ]);
135
- const queryKeyExport = t.exportNamedDeclaration(queryKeyConst);
136
- (0, babel_ast_1.addJSDocComment)(queryKeyExport, ['Query key factory for caching']);
137
- statements.push(queryKeyExport);
128
+ else if (hasArgs) {
129
+ const keyFn = t.arrowFunctionExpression([(0, hooks_ast_1.createFunctionParam)('variables', (0, hooks_ast_1.typeRef)(varTypeName), true)], (0, babel_ast_1.asConst)(t.arrayExpression([
130
+ t.stringLiteral(operation.name),
131
+ t.identifier('variables'),
132
+ ])));
133
+ const keyDecl = t.exportNamedDeclaration(t.variableDeclaration('const', [
134
+ t.variableDeclarator(t.identifier(queryKeyName), keyFn),
135
+ ]));
136
+ (0, hooks_ast_1.addJSDocComment)(keyDecl, ['Query key factory for caching']);
137
+ statements.push(keyDecl);
138
138
  }
139
139
  else {
140
- const queryKeyArrow = t.arrowFunctionExpression([], t.tsAsExpression(t.arrayExpression([t.stringLiteral(operation.name)]), t.tsTypeReference(t.identifier('const'))));
141
- const queryKeyConst = t.variableDeclaration('const', [
142
- t.variableDeclarator(t.identifier(queryKeyName), queryKeyArrow),
143
- ]);
144
- const queryKeyExport = t.exportNamedDeclaration(queryKeyConst);
145
- (0, babel_ast_1.addJSDocComment)(queryKeyExport, ['Query key factory for caching']);
146
- statements.push(queryKeyExport);
140
+ const keyFn = t.arrowFunctionExpression([], (0, babel_ast_1.asConst)(t.arrayExpression([t.stringLiteral(operation.name)])));
141
+ const keyDecl = t.exportNamedDeclaration(t.variableDeclaration('const', [
142
+ t.variableDeclarator(t.identifier(queryKeyName), keyFn),
143
+ ]));
144
+ (0, hooks_ast_1.addJSDocComment)(keyDecl, ['Query key factory for caching']);
145
+ statements.push(keyDecl);
147
146
  }
147
+ // Helper to build query key call expression
148
+ const buildQueryKeyCall = (withVars) => {
149
+ if (useCentralizedKeys) {
150
+ return withVars
151
+ ? (0, hooks_ast_1.callExpr)(t.identifier(queryKeyName), [t.identifier('variables')])
152
+ : (0, hooks_ast_1.callExpr)(t.identifier(queryKeyName), []);
153
+ }
154
+ return withVars
155
+ ? (0, hooks_ast_1.callExpr)(t.identifier(queryKeyName), [t.identifier('variables')])
156
+ : (0, hooks_ast_1.callExpr)(t.identifier(queryKeyName), []);
157
+ };
158
+ // Helper to build the fields+StrictSelect intersection type
159
+ const fieldsSelectionType = (s) => t.tsParenthesizedType(t.tsIntersectionType([
160
+ t.tsTypeLiteral([
161
+ t.tsPropertySignature(t.identifier('fields'), t.tsTypeAnnotation(s)),
162
+ ]),
163
+ (0, hooks_ast_1.typeRef)('StrictSelect', [s, (0, hooks_ast_1.typeRef)(selectTypeName)]),
164
+ ]));
165
+ const selectedResultType = (sel) => (0, hooks_ast_1.customSelectResultTypeLiteral)(operation.name, operation.returnType, payloadTypeName, sel);
166
+ // Hook
148
167
  if (reactQueryEnabled) {
149
- const hasArgs = operation.args.length > 0;
150
- const hasRequiredArgs = operation.args.some((arg) => (0, type_resolver_1.isTypeRequired)(arg.type));
151
- const hookBodyStatements = [];
152
- const useQueryOptions = [];
153
- if (hasArgs) {
154
- useQueryOptions.push(t.objectProperty(t.identifier('queryKey'), t.callExpression(t.identifier(queryKeyName), [t.identifier('variables')])));
155
- useQueryOptions.push(t.objectProperty(t.identifier('queryFn'), t.arrowFunctionExpression([], (0, babel_ast_1.createTypedCallExpression)(t.identifier('execute'), [t.identifier(documentConstName), t.identifier('variables')], [
156
- t.tsTypeReference(t.identifier(resultTypeName)),
157
- t.tsTypeReference(t.identifier(variablesTypeName)),
158
- ]))));
159
- if (hasRequiredArgs) {
160
- useQueryOptions.push(t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('variables'))), t.binaryExpression('!==', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.booleanLiteral(false)))));
168
+ const description = operation.description || `Query hook for ${operation.name}`;
169
+ const argNames = operation.args.map((a) => a.name).join(', ');
170
+ const exampleCall = hasArgs
171
+ ? hasSelect
172
+ ? `${hookName}({ variables: { ${argNames} }, selection: { fields: { id: true } } })`
173
+ : `${hookName}({ variables: { ${argNames} } })`
174
+ : hasSelect
175
+ ? `${hookName}({ selection: { fields: { id: true } } })`
176
+ : `${hookName}()`;
177
+ if (hasSelect) {
178
+ // Overload 1: with selection.fields
179
+ const o1Props = [];
180
+ if (hasArgs) {
181
+ const varType = hasRequiredArgs
182
+ ? (0, hooks_ast_1.typeRef)(varTypeName)
183
+ : t.tsUnionType([(0, hooks_ast_1.typeRef)(varTypeName), t.tsUndefinedKeyword()]);
184
+ o1Props.push(t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation(varType)));
161
185
  }
186
+ o1Props.push(t.tsPropertySignature(t.identifier('selection'), t.tsTypeAnnotation(fieldsSelectionType((0, hooks_ast_1.sRef)()))));
187
+ const o1ParamType = t.tsIntersectionType([
188
+ t.tsTypeLiteral(o1Props),
189
+ (0, hooks_ast_1.omitType)((0, hooks_ast_1.typeRef)('UseQueryOptions', [
190
+ selectedResultType((0, hooks_ast_1.sRef)()),
191
+ (0, hooks_ast_1.typeRef)('Error'),
192
+ (0, hooks_ast_1.typeRef)('TData'),
193
+ ]), ['queryKey', 'queryFn']),
194
+ ]);
195
+ const o1 = (0, hooks_ast_1.exportDeclareFunction)(hookName, (0, hooks_ast_1.createSAndTDataTypeParams)(selectTypeName, selectedResultType((0, hooks_ast_1.sRef)())), [(0, hooks_ast_1.createFunctionParam)('params', o1ParamType)], (0, hooks_ast_1.typeRef)('UseQueryResult', [(0, hooks_ast_1.typeRef)('TData')]));
196
+ (0, hooks_ast_1.addJSDocComment)(o1, [
197
+ description,
198
+ '',
199
+ '@example',
200
+ '```tsx',
201
+ `const { data, isLoading } = ${exampleCall};`,
202
+ '',
203
+ `if (data?.${operation.name}) {`,
204
+ ` console.log(data.${operation.name});`,
205
+ '}',
206
+ '```',
207
+ ]);
208
+ statements.push(o1);
209
+ // Implementation
210
+ const implProps = [];
211
+ if (hasArgs) {
212
+ const varProp = t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation((0, hooks_ast_1.typeRef)(varTypeName)));
213
+ if (!hasRequiredArgs)
214
+ varProp.optional = true;
215
+ implProps.push(varProp);
216
+ }
217
+ const implSelProp = t.tsPropertySignature(t.identifier('selection'), t.tsTypeAnnotation((0, hooks_ast_1.selectionConfigType)((0, hooks_ast_1.typeRef)(selectTypeName))));
218
+ implProps.push(implSelProp);
219
+ const implParamType = t.tsIntersectionType([
220
+ t.tsTypeLiteral(implProps),
221
+ (0, hooks_ast_1.useQueryOptionsImplType)(),
222
+ ]);
223
+ const implParam = (0, hooks_ast_1.createFunctionParam)('params', implParamType, false);
224
+ const body = [];
225
+ if (hasArgs) {
226
+ body.push((0, hooks_ast_1.constDecl)('variables', hasRequiredArgs
227
+ ? t.memberExpression(t.identifier('params'), t.identifier('variables'))
228
+ : t.logicalExpression('??', t.memberExpression(t.identifier('params'), t.identifier('variables')), t.objectExpression([]))));
229
+ }
230
+ body.push((0, hooks_ast_1.buildSelectionArgsCall)(selectTypeName));
231
+ if (hasArgs) {
232
+ const destructPattern = t.objectPattern([
233
+ t.objectProperty(t.identifier('variables'), t.identifier('_variables'), false, false),
234
+ t.objectProperty(t.identifier('selection'), t.identifier('_selection'), false, false),
235
+ t.restElement(t.identifier('queryOptions')),
236
+ ]);
237
+ body.push(t.variableDeclaration('const', [
238
+ t.variableDeclarator(destructPattern, t.logicalExpression('??', t.identifier('params'), t.objectExpression([]))),
239
+ ]));
240
+ body.push((0, hooks_ast_1.voidStatement)('_variables'));
241
+ body.push((0, hooks_ast_1.voidStatement)('_selection'));
242
+ }
243
+ else {
244
+ body.push((0, hooks_ast_1.destructureParamsWithSelection)('queryOptions'));
245
+ body.push((0, hooks_ast_1.voidStatement)('_selection'));
246
+ }
247
+ const selectArgExpr = t.objectExpression([
248
+ (0, hooks_ast_1.objectProp)('select', t.memberExpression(t.identifier('args'), t.identifier('select'))),
249
+ ]);
250
+ const queryFnArgs = hasArgs
251
+ ? [
252
+ hasRequiredArgs
253
+ ? t.tsNonNullExpression(t.identifier('variables'))
254
+ : t.identifier('variables'),
255
+ selectArgExpr,
256
+ ]
257
+ : [selectArgExpr];
258
+ const queryFnExpr = t.arrowFunctionExpression([], (0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, queryFnArgs));
259
+ const extraProps = [];
260
+ const enabledExpr = hasRequiredArgs
261
+ ? t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('variables'))), t.binaryExpression('!==', t.optionalMemberExpression(t.identifier('params'), t.identifier('enabled'), false, true), t.booleanLiteral(false)))
262
+ : undefined;
263
+ extraProps.push((0, hooks_ast_1.spreadObj)(t.identifier('queryOptions')));
264
+ body.push((0, hooks_ast_1.returnUseQuery)(buildQueryKeyCall(hasArgs), queryFnExpr, extraProps, enabledExpr));
265
+ statements.push((0, hooks_ast_1.exportFunction)(hookName, null, [implParam], body));
162
266
  }
163
267
  else {
164
- useQueryOptions.push(t.objectProperty(t.identifier('queryKey'), t.callExpression(t.identifier(queryKeyName), [])));
165
- useQueryOptions.push(t.objectProperty(t.identifier('queryFn'), t.arrowFunctionExpression([], (0, babel_ast_1.createTypedCallExpression)(t.identifier('execute'), [t.identifier(documentConstName)], [t.tsTypeReference(t.identifier(resultTypeName))]))));
268
+ // Without select: simple hook (scalar return type)
269
+ const resultTypeLiteral = t.tsTypeLiteral([
270
+ t.tsPropertySignature(t.identifier(operation.name), t.tsTypeAnnotation((0, hooks_ast_1.typeRef)(resultType))),
271
+ ]);
272
+ const optType = (0, hooks_ast_1.omitType)((0, hooks_ast_1.typeRef)('UseQueryOptions', [
273
+ resultTypeLiteral,
274
+ (0, hooks_ast_1.typeRef)('Error'),
275
+ (0, hooks_ast_1.typeRef)('TData'),
276
+ ]), ['queryKey', 'queryFn']);
277
+ let paramType;
278
+ if (hasArgs) {
279
+ const varProp = t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation((0, hooks_ast_1.typeRef)(varTypeName)));
280
+ if (!hasRequiredArgs)
281
+ varProp.optional = true;
282
+ paramType = t.tsIntersectionType([t.tsTypeLiteral([varProp]), optType]);
283
+ }
284
+ else {
285
+ paramType = optType;
286
+ }
287
+ const implParam = (0, hooks_ast_1.createFunctionParam)('params', paramType, !hasRequiredArgs);
288
+ const hookDecl = (0, hooks_ast_1.exportDeclareFunction)(hookName, (0, hooks_ast_1.createTDataTypeParam)(resultTypeLiteral), [implParam], (0, hooks_ast_1.typeRef)('UseQueryResult', [(0, hooks_ast_1.typeRef)('TData')]));
289
+ (0, hooks_ast_1.addJSDocComment)(hookDecl, [
290
+ description,
291
+ '',
292
+ '@example',
293
+ '```tsx',
294
+ `const { data, isLoading } = ${exampleCall};`,
295
+ '',
296
+ `if (data?.${operation.name}) {`,
297
+ ` console.log(data.${operation.name});`,
298
+ '}',
299
+ '```',
300
+ ]);
301
+ const body = [];
302
+ if (hasArgs) {
303
+ body.push((0, hooks_ast_1.constDecl)('variables', hasRequiredArgs
304
+ ? t.optionalMemberExpression(t.identifier('params'), t.identifier('variables'), false, true)
305
+ : t.logicalExpression('??', t.optionalMemberExpression(t.identifier('params'), t.identifier('variables'), false, true), t.objectExpression([]))));
306
+ const destructPattern = t.objectPattern([
307
+ t.objectProperty(t.identifier('variables'), t.identifier('_variables'), false, false),
308
+ t.restElement(t.identifier('queryOptions')),
309
+ ]);
310
+ body.push(t.variableDeclaration('const', [
311
+ t.variableDeclarator(destructPattern, t.logicalExpression('??', t.identifier('params'), t.objectExpression([]))),
312
+ ]));
313
+ body.push((0, hooks_ast_1.voidStatement)('_variables'));
314
+ }
315
+ else {
316
+ body.push((0, hooks_ast_1.constDecl)('queryOptions', t.logicalExpression('??', t.identifier('params'), t.objectExpression([]))));
317
+ }
318
+ const queryFnArgs = hasArgs
319
+ ? [
320
+ hasRequiredArgs
321
+ ? t.tsNonNullExpression(t.identifier('variables'))
322
+ : t.identifier('variables'),
323
+ ]
324
+ : [];
325
+ const queryFnExpr = t.arrowFunctionExpression([], (0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, queryFnArgs));
326
+ const enabledExpr = hasRequiredArgs
327
+ ? t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('variables'))), t.binaryExpression('!==', t.optionalMemberExpression(t.identifier('params'), t.identifier('enabled'), false, true), t.booleanLiteral(false)))
328
+ : undefined;
329
+ body.push((0, hooks_ast_1.returnUseQuery)(buildQueryKeyCall(hasArgs), queryFnExpr, [(0, hooks_ast_1.spreadObj)(t.identifier('queryOptions'))], enabledExpr));
330
+ // We need the implementation version (not declare), with return type
331
+ statements.push(hookDecl);
332
+ statements.push((0, hooks_ast_1.exportFunction)(hookName, (0, hooks_ast_1.createTDataTypeParam)(resultTypeLiteral), [implParam], body, (0, hooks_ast_1.typeRef)('UseQueryResult', [(0, hooks_ast_1.typeRef)('TData')])));
166
333
  }
167
- useQueryOptions.push(t.spreadElement(t.identifier('options')));
168
- hookBodyStatements.push(t.returnStatement(t.callExpression(t.identifier('useQuery'), [t.objectExpression(useQueryOptions)])));
169
- const hookParams = [];
334
+ }
335
+ // Fetch function (non-hook)
336
+ const fetchFnName = `fetch${(0, utils_1.ucFirst)(operation.name)}Query`;
337
+ const fetchArgNames = operation.args.map((a) => a.name).join(', ');
338
+ const fetchExampleCall = hasArgs
339
+ ? hasSelect
340
+ ? `${fetchFnName}({ variables: { ${fetchArgNames} }, selection: { fields: { id: true } } })`
341
+ : `${fetchFnName}({ variables: { ${fetchArgNames} } })`
342
+ : hasSelect
343
+ ? `${fetchFnName}({ selection: { fields: { id: true } } })`
344
+ : `${fetchFnName}()`;
345
+ if (hasSelect) {
346
+ // Overload 1: with fields
347
+ const f1Props = [];
170
348
  if (hasArgs) {
171
- hookParams.push((0, babel_ast_1.typedParam)('variables', t.tsTypeReference(t.identifier(variablesTypeName)), !hasRequiredArgs));
349
+ const varType = hasRequiredArgs
350
+ ? (0, hooks_ast_1.typeRef)(varTypeName)
351
+ : t.tsUnionType([(0, hooks_ast_1.typeRef)(varTypeName), t.tsUndefinedKeyword()]);
352
+ f1Props.push(t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation(varType)));
172
353
  }
173
- const optionsTypeStr = `Omit<UseQueryOptions<${resultTypeName}, Error>, 'queryKey' | 'queryFn'>`;
174
- const optionsParam = t.identifier('options');
175
- optionsParam.optional = true;
176
- optionsParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(optionsTypeStr)));
177
- hookParams.push(optionsParam);
178
- const hookFunc = t.functionDeclaration(t.identifier(hookName), hookParams, t.blockStatement(hookBodyStatements));
179
- const hookExport = t.exportNamedDeclaration(hookFunc);
180
- const description = operation.description || `Query hook for ${operation.name}`;
181
- const argNames = operation.args.map((a) => a.name).join(', ');
182
- const exampleCall = hasArgs ? `${hookName}({ ${argNames} })` : `${hookName}()`;
183
- (0, babel_ast_1.addJSDocComment)(hookExport, [
184
- description,
354
+ f1Props.push(t.tsPropertySignature(t.identifier('selection'), t.tsTypeAnnotation(fieldsSelectionType((0, hooks_ast_1.sRef)()))));
355
+ const f1Decl = (0, hooks_ast_1.exportAsyncDeclareFunction)(fetchFnName, (0, hooks_ast_1.createSTypeParam)(selectTypeName), [(0, hooks_ast_1.createFunctionParam)('params', t.tsTypeLiteral(f1Props))], (0, hooks_ast_1.typeRef)('Promise', [selectedResultType((0, hooks_ast_1.sRef)())]));
356
+ (0, hooks_ast_1.addJSDocComment)(f1Decl, [
357
+ `Fetch ${operation.name} without React hooks`,
185
358
  '',
186
359
  '@example',
187
- '```tsx',
188
- `const { data, isLoading } = ${exampleCall};`,
189
- '',
190
- `if (data?.${operation.name}) {`,
191
- ` console.log(data.${operation.name});`,
192
- '}',
360
+ '```ts',
361
+ `const data = await ${fetchExampleCall};`,
193
362
  '```',
194
363
  ]);
195
- statements.push(hookExport);
196
- }
197
- const fetchFnName = `fetch${(0, utils_1.ucFirst)(operation.name)}Query`;
198
- const hasArgs = operation.args.length > 0;
199
- const hasRequiredArgs = operation.args.some((arg) => (0, type_resolver_1.isTypeRequired)(arg.type));
200
- const fetchBodyStatements = [];
201
- if (hasArgs) {
202
- fetchBodyStatements.push(t.returnStatement((0, babel_ast_1.createTypedCallExpression)(t.identifier('execute'), [t.identifier(documentConstName), t.identifier('variables'), t.identifier('options')], [
203
- t.tsTypeReference(t.identifier(resultTypeName)),
204
- t.tsTypeReference(t.identifier(variablesTypeName)),
205
- ])));
364
+ statements.push(f1Decl);
365
+ // Implementation
366
+ const fImplProps = [];
367
+ if (hasArgs) {
368
+ const varProp = t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation((0, hooks_ast_1.typeRef)(varTypeName)));
369
+ if (!hasRequiredArgs)
370
+ varProp.optional = true;
371
+ fImplProps.push(varProp);
372
+ }
373
+ const fImplSelProp = t.tsPropertySignature(t.identifier('selection'), t.tsTypeAnnotation((0, hooks_ast_1.selectionConfigType)((0, hooks_ast_1.typeRef)(selectTypeName))));
374
+ fImplProps.push(fImplSelProp);
375
+ const fBody = [];
376
+ if (hasArgs) {
377
+ fBody.push((0, hooks_ast_1.constDecl)('variables', hasRequiredArgs
378
+ ? t.memberExpression(t.identifier('params'), t.identifier('variables'))
379
+ : t.logicalExpression('??', t.memberExpression(t.identifier('params'), t.identifier('variables')), t.objectExpression([]))));
380
+ }
381
+ fBody.push((0, hooks_ast_1.buildSelectionArgsCall)(selectTypeName));
382
+ const selectArgExpr = t.objectExpression([
383
+ (0, hooks_ast_1.objectProp)('select', t.memberExpression(t.identifier('args'), t.identifier('select'))),
384
+ ]);
385
+ const fCallArgs = hasArgs
386
+ ? [
387
+ hasRequiredArgs
388
+ ? t.tsNonNullExpression(t.identifier('variables'))
389
+ : t.identifier('variables'),
390
+ selectArgExpr,
391
+ ]
392
+ : [selectArgExpr];
393
+ fBody.push(t.returnStatement((0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, fCallArgs)));
394
+ statements.push((0, hooks_ast_1.exportAsyncFunction)(fetchFnName, null, [
395
+ (0, hooks_ast_1.createFunctionParam)('params', t.tsTypeLiteral(fImplProps), false),
396
+ ], fBody));
206
397
  }
207
398
  else {
208
- fetchBodyStatements.push(t.returnStatement((0, babel_ast_1.createTypedCallExpression)(t.identifier('execute'), [t.identifier(documentConstName), t.identifier('undefined'), t.identifier('options')], [t.tsTypeReference(t.identifier(resultTypeName))])));
209
- }
210
- const fetchParams = [];
211
- if (hasArgs) {
212
- fetchParams.push((0, babel_ast_1.typedParam)('variables', t.tsTypeReference(t.identifier(variablesTypeName)), !hasRequiredArgs));
399
+ const fBody = [];
400
+ if (hasArgs) {
401
+ const fProps = [];
402
+ const varProp = t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation((0, hooks_ast_1.typeRef)(varTypeName)));
403
+ if (!hasRequiredArgs)
404
+ varProp.optional = true;
405
+ fProps.push(varProp);
406
+ fBody.push((0, hooks_ast_1.constDecl)('variables', hasRequiredArgs
407
+ ? t.optionalMemberExpression(t.identifier('params'), t.identifier('variables'), false, true)
408
+ : t.logicalExpression('??', t.optionalMemberExpression(t.identifier('params'), t.identifier('variables'), false, true), t.objectExpression([]))));
409
+ const fCallArgs = hasRequiredArgs
410
+ ? [t.tsNonNullExpression(t.identifier('variables'))]
411
+ : [t.identifier('variables')];
412
+ fBody.push(t.returnStatement((0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, fCallArgs)));
413
+ const fDecl = (0, hooks_ast_1.exportAsyncFunction)(fetchFnName, null, [
414
+ (0, hooks_ast_1.createFunctionParam)('params', t.tsTypeLiteral(fProps), !hasRequiredArgs),
415
+ ], fBody);
416
+ (0, hooks_ast_1.addJSDocComment)(fDecl, [
417
+ `Fetch ${operation.name} without React hooks`,
418
+ '',
419
+ '@example',
420
+ '```ts',
421
+ `const data = await ${fetchExampleCall};`,
422
+ '```',
423
+ ]);
424
+ statements.push(fDecl);
425
+ }
426
+ else {
427
+ fBody.push(t.returnStatement((0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, [])));
428
+ const fDecl = (0, hooks_ast_1.exportAsyncFunction)(fetchFnName, null, [], fBody);
429
+ (0, hooks_ast_1.addJSDocComment)(fDecl, [
430
+ `Fetch ${operation.name} without React hooks`,
431
+ '',
432
+ '@example',
433
+ '```ts',
434
+ `const data = await ${fetchExampleCall};`,
435
+ '```',
436
+ ]);
437
+ statements.push(fDecl);
438
+ }
213
439
  }
214
- fetchParams.push((0, babel_ast_1.typedParam)('options', t.tsTypeReference(t.identifier('ExecuteOptions')), true));
215
- const fetchFunc = t.functionDeclaration(t.identifier(fetchFnName), fetchParams, t.blockStatement(fetchBodyStatements));
216
- fetchFunc.async = true;
217
- fetchFunc.returnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(resultTypeName))])));
218
- const fetchExport = t.exportNamedDeclaration(fetchFunc);
219
- const argNames = operation.args.map((a) => a.name).join(', ');
220
- const fetchExampleCall = hasArgs ? `${fetchFnName}({ ${argNames} })` : `${fetchFnName}()`;
221
- (0, babel_ast_1.addJSDocComment)(fetchExport, [
222
- `Fetch ${operation.name} without React hooks`,
223
- '',
224
- '@example',
225
- '```ts',
226
- `const data = await ${fetchExampleCall};`,
227
- '```',
228
- ]);
229
- statements.push(fetchExport);
440
+ // Prefetch function
230
441
  if (reactQueryEnabled) {
231
442
  const prefetchFnName = `prefetch${(0, utils_1.ucFirst)(operation.name)}Query`;
232
- const prefetchBodyStatements = [];
233
- const prefetchQueryOptions = [];
234
- if (hasArgs) {
235
- prefetchQueryOptions.push(t.objectProperty(t.identifier('queryKey'), t.callExpression(t.identifier(queryKeyName), [t.identifier('variables')])));
236
- prefetchQueryOptions.push(t.objectProperty(t.identifier('queryFn'), t.arrowFunctionExpression([], (0, babel_ast_1.createTypedCallExpression)(t.identifier('execute'), [t.identifier(documentConstName), t.identifier('variables'), t.identifier('options')], [
237
- t.tsTypeReference(t.identifier(resultTypeName)),
238
- t.tsTypeReference(t.identifier(variablesTypeName)),
239
- ]))));
443
+ const prefetchArgNames = operation.args.map((a) => a.name).join(', ');
444
+ const prefetchExampleCall = hasArgs
445
+ ? hasSelect
446
+ ? `${prefetchFnName}(queryClient, { variables: { ${prefetchArgNames} }, selection: { fields: { id: true } } })`
447
+ : `${prefetchFnName}(queryClient, { variables: { ${prefetchArgNames} } })`
448
+ : hasSelect
449
+ ? `${prefetchFnName}(queryClient, { selection: { fields: { id: true } } })`
450
+ : `${prefetchFnName}(queryClient)`;
451
+ if (hasSelect) {
452
+ // Overload 1: with fields
453
+ const p1Props = [];
454
+ if (hasArgs) {
455
+ const varType = hasRequiredArgs
456
+ ? (0, hooks_ast_1.typeRef)(varTypeName)
457
+ : t.tsUnionType([(0, hooks_ast_1.typeRef)(varTypeName), t.tsUndefinedKeyword()]);
458
+ p1Props.push(t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation(varType)));
459
+ }
460
+ p1Props.push(t.tsPropertySignature(t.identifier('selection'), t.tsTypeAnnotation(fieldsSelectionType((0, hooks_ast_1.sRef)()))));
461
+ const p1Decl = (0, hooks_ast_1.exportAsyncDeclareFunction)(prefetchFnName, (0, hooks_ast_1.createSTypeParam)(selectTypeName), [
462
+ (0, hooks_ast_1.createFunctionParam)('queryClient', (0, hooks_ast_1.typeRef)('QueryClient')),
463
+ (0, hooks_ast_1.createFunctionParam)('params', t.tsTypeLiteral(p1Props)),
464
+ ], (0, hooks_ast_1.typeRef)('Promise', [t.tsVoidKeyword()]));
465
+ (0, hooks_ast_1.addJSDocComment)(p1Decl, [
466
+ `Prefetch ${operation.name} for SSR or cache warming`,
467
+ '',
468
+ '@example',
469
+ '```ts',
470
+ `await ${prefetchExampleCall};`,
471
+ '```',
472
+ ]);
473
+ statements.push(p1Decl);
474
+ // Implementation
475
+ const pImplProps = [];
476
+ if (hasArgs) {
477
+ const varProp = t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation((0, hooks_ast_1.typeRef)(varTypeName)));
478
+ if (!hasRequiredArgs)
479
+ varProp.optional = true;
480
+ pImplProps.push(varProp);
481
+ }
482
+ const pImplSelProp = t.tsPropertySignature(t.identifier('selection'), t.tsTypeAnnotation((0, hooks_ast_1.selectionConfigType)((0, hooks_ast_1.typeRef)(selectTypeName))));
483
+ pImplProps.push(pImplSelProp);
484
+ const pBody = [];
485
+ if (hasArgs) {
486
+ pBody.push((0, hooks_ast_1.constDecl)('variables', hasRequiredArgs
487
+ ? t.memberExpression(t.identifier('params'), t.identifier('variables'))
488
+ : t.logicalExpression('??', t.memberExpression(t.identifier('params'), t.identifier('variables')), t.objectExpression([]))));
489
+ }
490
+ pBody.push((0, hooks_ast_1.buildSelectionArgsCall)(selectTypeName));
491
+ const selectArgExpr = t.objectExpression([
492
+ (0, hooks_ast_1.objectProp)('select', t.memberExpression(t.identifier('args'), t.identifier('select'))),
493
+ ]);
494
+ const pCallArgs = hasArgs
495
+ ? [
496
+ hasRequiredArgs
497
+ ? t.tsNonNullExpression(t.identifier('variables'))
498
+ : t.identifier('variables'),
499
+ selectArgExpr,
500
+ ]
501
+ : [selectArgExpr];
502
+ const prefetchQueryCall = (0, hooks_ast_1.callExpr)(t.memberExpression(t.identifier('queryClient'), t.identifier('prefetchQuery')), [
503
+ t.objectExpression([
504
+ (0, hooks_ast_1.objectProp)('queryKey', buildQueryKeyCall(hasArgs)),
505
+ (0, hooks_ast_1.objectProp)('queryFn', t.arrowFunctionExpression([], (0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, pCallArgs))),
506
+ ]),
507
+ ]);
508
+ pBody.push(t.expressionStatement(t.awaitExpression(prefetchQueryCall)));
509
+ statements.push((0, hooks_ast_1.exportAsyncFunction)(prefetchFnName, null, [
510
+ (0, hooks_ast_1.createFunctionParam)('queryClient', (0, hooks_ast_1.typeRef)('QueryClient')),
511
+ (0, hooks_ast_1.createFunctionParam)('params', t.tsTypeLiteral(pImplProps), false),
512
+ ], pBody, (0, hooks_ast_1.typeRef)('Promise', [t.tsVoidKeyword()])));
240
513
  }
241
514
  else {
242
- prefetchQueryOptions.push(t.objectProperty(t.identifier('queryKey'), t.callExpression(t.identifier(queryKeyName), [])));
243
- prefetchQueryOptions.push(t.objectProperty(t.identifier('queryFn'), t.arrowFunctionExpression([], (0, babel_ast_1.createTypedCallExpression)(t.identifier('execute'), [t.identifier(documentConstName), t.identifier('undefined'), t.identifier('options')], [t.tsTypeReference(t.identifier(resultTypeName))]))));
244
- }
245
- prefetchBodyStatements.push(t.expressionStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('queryClient'), t.identifier('prefetchQuery')), [t.objectExpression(prefetchQueryOptions)]))));
246
- const prefetchParams = [
247
- (0, babel_ast_1.typedParam)('queryClient', t.tsTypeReference(t.identifier('QueryClient'))),
248
- ];
249
- if (hasArgs) {
250
- prefetchParams.push((0, babel_ast_1.typedParam)('variables', t.tsTypeReference(t.identifier(variablesTypeName)), !hasRequiredArgs));
515
+ // Without select
516
+ const pBody = [];
517
+ const pParams = [
518
+ (0, hooks_ast_1.createFunctionParam)('queryClient', (0, hooks_ast_1.typeRef)('QueryClient')),
519
+ ];
520
+ if (hasArgs) {
521
+ const pProps = [];
522
+ const varProp = t.tsPropertySignature(t.identifier('variables'), t.tsTypeAnnotation((0, hooks_ast_1.typeRef)(varTypeName)));
523
+ if (!hasRequiredArgs)
524
+ varProp.optional = true;
525
+ pProps.push(varProp);
526
+ pParams.push((0, hooks_ast_1.createFunctionParam)('params', t.tsTypeLiteral(pProps), !hasRequiredArgs));
527
+ pBody.push((0, hooks_ast_1.constDecl)('variables', hasRequiredArgs
528
+ ? t.optionalMemberExpression(t.identifier('params'), t.identifier('variables'), false, true)
529
+ : t.logicalExpression('??', t.optionalMemberExpression(t.identifier('params'), t.identifier('variables'), false, true), t.objectExpression([]))));
530
+ const pCallArgs = hasRequiredArgs
531
+ ? [t.tsNonNullExpression(t.identifier('variables'))]
532
+ : [t.identifier('variables')];
533
+ const prefetchQueryCall = (0, hooks_ast_1.callExpr)(t.memberExpression(t.identifier('queryClient'), t.identifier('prefetchQuery')), [
534
+ t.objectExpression([
535
+ (0, hooks_ast_1.objectProp)('queryKey', buildQueryKeyCall(true)),
536
+ (0, hooks_ast_1.objectProp)('queryFn', t.arrowFunctionExpression([], (0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, pCallArgs))),
537
+ ]),
538
+ ]);
539
+ pBody.push(t.expressionStatement(t.awaitExpression(prefetchQueryCall)));
540
+ }
541
+ else {
542
+ const prefetchQueryCall = (0, hooks_ast_1.callExpr)(t.memberExpression(t.identifier('queryClient'), t.identifier('prefetchQuery')), [
543
+ t.objectExpression([
544
+ (0, hooks_ast_1.objectProp)('queryKey', buildQueryKeyCall(false)),
545
+ (0, hooks_ast_1.objectProp)('queryFn', t.arrowFunctionExpression([], (0, hooks_ast_1.getClientCustomCallUnwrap)('query', operation.name, []))),
546
+ ]),
547
+ ]);
548
+ pBody.push(t.expressionStatement(t.awaitExpression(prefetchQueryCall)));
549
+ }
550
+ const pDecl = (0, hooks_ast_1.exportAsyncFunction)(prefetchFnName, null, pParams, pBody, (0, hooks_ast_1.typeRef)('Promise', [t.tsVoidKeyword()]));
551
+ (0, hooks_ast_1.addJSDocComment)(pDecl, [
552
+ `Prefetch ${operation.name} for SSR or cache warming`,
553
+ '',
554
+ '@example',
555
+ '```ts',
556
+ `await ${prefetchExampleCall};`,
557
+ '```',
558
+ ]);
559
+ statements.push(pDecl);
251
560
  }
252
- prefetchParams.push((0, babel_ast_1.typedParam)('options', t.tsTypeReference(t.identifier('ExecuteOptions')), true));
253
- const prefetchFunc = t.functionDeclaration(t.identifier(prefetchFnName), prefetchParams, t.blockStatement(prefetchBodyStatements));
254
- prefetchFunc.async = true;
255
- prefetchFunc.returnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsVoidKeyword()])));
256
- const prefetchExport = t.exportNamedDeclaration(prefetchFunc);
257
- const prefetchExampleCall = hasArgs
258
- ? `${prefetchFnName}(queryClient, { ${argNames} })`
259
- : `${prefetchFnName}(queryClient)`;
260
- (0, babel_ast_1.addJSDocComment)(prefetchExport, [
261
- `Prefetch ${operation.name} for SSR or cache warming`,
262
- '',
263
- '@example',
264
- '```ts',
265
- `await ${prefetchExampleCall};`,
266
- '```',
267
- ]);
268
- statements.push(prefetchExport);
269
561
  }
270
- const code = (0, babel_ast_1.generateCode)(statements);
271
562
  const headerText = reactQueryEnabled
272
563
  ? `Custom query hook for ${operation.name}`
273
564
  : `Custom query functions for ${operation.name}`;
274
- const content = (0, utils_1.getGeneratedFileHeader)(headerText) + '\n\n' + code;
565
+ const content = (0, hooks_ast_1.generateHookFileCode)(headerText, statements);
275
566
  return {
276
567
  fileName,
277
568
  content,
@@ -279,13 +570,12 @@ function generateCustomQueryHook(options) {
279
570
  };
280
571
  }
281
572
  function generateAllCustomQueryHooks(options) {
282
- const { operations, typeRegistry, maxDepth = 2, skipQueryField = true, reactQueryEnabled = true, tableTypeNames, useCentralizedKeys = true, } = options;
573
+ const { operations, typeRegistry, skipQueryField = true, reactQueryEnabled = true, tableTypeNames, useCentralizedKeys = true, } = options;
283
574
  return operations
284
575
  .filter((op) => op.kind === 'query')
285
576
  .map((operation) => generateCustomQueryHook({
286
577
  operation,
287
578
  typeRegistry,
288
- maxDepth,
289
579
  skipQueryField,
290
580
  reactQueryEnabled,
291
581
  tableTypeNames,