@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.
- package/cli/index.js +36 -43
- package/cli/shared.d.ts +15 -19
- package/cli/shared.js +104 -25
- package/client/error.js +31 -9
- package/client/execute.js +2 -2
- package/client/index.d.ts +3 -3
- package/client/index.js +6 -6
- package/core/ast.d.ts +1 -1
- package/core/ast.js +1 -1
- package/core/codegen/babel-ast.d.ts +1 -1
- package/core/codegen/babel-ast.js +2 -2
- package/core/codegen/barrel.d.ts +0 -6
- package/core/codegen/barrel.js +22 -19
- package/core/codegen/client.d.ts +2 -12
- package/core/codegen/client.js +7 -21
- package/core/codegen/custom-mutations.d.ts +0 -14
- package/core/codegen/custom-mutations.js +139 -88
- package/core/codegen/custom-queries.d.ts +0 -14
- package/core/codegen/custom-queries.js +483 -193
- package/core/codegen/hooks-ast.d.ts +75 -0
- package/core/codegen/hooks-ast.js +522 -0
- package/core/codegen/index.d.ts +16 -18
- package/core/codegen/index.js +42 -88
- package/core/codegen/invalidation.d.ts +1 -7
- package/core/codegen/invalidation.js +50 -16
- package/core/codegen/mutation-keys.d.ts +1 -10
- package/core/codegen/mutation-keys.js +22 -8
- package/core/codegen/mutations.d.ts +0 -13
- package/core/codegen/mutations.js +301 -366
- package/core/codegen/orm/barrel.d.ts +0 -5
- package/core/codegen/orm/barrel.js +5 -0
- package/core/codegen/orm/client-generator.d.ts +0 -5
- package/core/codegen/orm/client-generator.js +7 -2
- package/core/codegen/orm/client.js +3 -1
- package/core/codegen/orm/custom-ops-generator.d.ts +0 -6
- package/core/codegen/orm/custom-ops-generator.js +104 -51
- package/core/codegen/orm/index.d.ts +4 -4
- package/core/codegen/orm/index.js +28 -15
- package/core/codegen/orm/input-types-generator.d.ts +1 -13
- package/core/codegen/orm/input-types-generator.js +85 -23
- package/core/codegen/orm/model-generator.d.ts +0 -5
- package/core/codegen/orm/model-generator.js +309 -131
- package/core/codegen/orm/select-types.d.ts +19 -14
- package/core/codegen/queries.d.ts +0 -8
- package/core/codegen/queries.js +360 -559
- package/core/codegen/query-keys.d.ts +1 -1
- package/core/codegen/query-keys.js +37 -23
- package/core/codegen/scalars.js +3 -1
- package/core/codegen/schema-types-generator.d.ts +1 -1
- package/core/codegen/schema-types-generator.js +17 -2
- package/core/codegen/select-helpers.d.ts +19 -0
- package/core/codegen/select-helpers.js +40 -0
- package/core/codegen/selection.d.ts +4 -0
- package/core/codegen/selection.js +65 -0
- package/core/codegen/shared/index.d.ts +2 -15
- package/core/codegen/shared/index.js +17 -4
- package/core/codegen/templates/hooks-client.ts +49 -0
- package/core/codegen/templates/hooks-selection.ts +58 -0
- package/core/codegen/templates/orm-client.ts +8 -6
- package/core/codegen/templates/query-builder.ts +250 -46
- package/core/codegen/templates/select-types.ts +31 -14
- package/core/codegen/type-resolver.d.ts +1 -5
- package/core/codegen/type-resolver.js +0 -22
- package/core/codegen/types.d.ts +0 -3
- package/core/codegen/types.js +71 -14
- package/core/codegen/utils.d.ts +1 -4
- package/core/codegen/utils.js +4 -1
- package/core/config/index.d.ts +1 -1
- package/core/config/resolver.js +1 -3
- package/core/generate.js +38 -50
- package/core/index.d.ts +3 -3
- package/core/index.js +3 -4
- package/core/introspect/index.d.ts +6 -6
- package/core/introspect/index.js +5 -8
- package/core/introspect/infer-tables.d.ts +0 -14
- package/core/introspect/infer-tables.js +15 -1
- package/core/introspect/source/database.js +1 -1
- package/core/introspect/source/endpoint.d.ts +0 -6
- package/core/introspect/source/endpoint.js +7 -1
- package/core/introspect/source/index.d.ts +4 -4
- package/core/introspect/source/index.js +5 -9
- package/core/introspect/source/pgpm-module.js +3 -3
- package/core/introspect/transform-schema.d.ts +2 -2
- package/core/introspect/transform-schema.js +2 -2
- package/core/output/index.d.ts +1 -1
- package/core/output/index.js +2 -2
- package/core/output/writer.d.ts +3 -0
- package/core/output/writer.js +20 -1
- package/core/pipeline/index.d.ts +2 -2
- package/core/query-builder.d.ts +2 -2
- package/core/query-builder.js +1 -1
- package/core/watch/index.d.ts +4 -4
- package/core/watch/index.js +9 -9
- package/core/watch/orchestrator.js +5 -3
- package/esm/cli/index.js +37 -44
- package/esm/cli/shared.d.ts +15 -19
- package/esm/cli/shared.js +94 -23
- package/esm/client/error.js +31 -9
- package/esm/client/execute.js +2 -2
- package/esm/client/index.d.ts +3 -3
- package/esm/client/index.js +3 -3
- package/esm/core/ast.d.ts +1 -1
- package/esm/core/ast.js +1 -1
- package/esm/core/codegen/babel-ast.d.ts +1 -1
- package/esm/core/codegen/babel-ast.js +2 -2
- package/esm/core/codegen/barrel.d.ts +0 -6
- package/esm/core/codegen/barrel.js +23 -20
- package/esm/core/codegen/client.d.ts +2 -12
- package/esm/core/codegen/client.js +7 -21
- package/esm/core/codegen/custom-mutations.d.ts +0 -14
- package/esm/core/codegen/custom-mutations.js +141 -90
- package/esm/core/codegen/custom-queries.d.ts +0 -14
- package/esm/core/codegen/custom-queries.js +486 -196
- package/esm/core/codegen/hooks-ast.d.ts +75 -0
- package/esm/core/codegen/hooks-ast.js +424 -0
- package/esm/core/codegen/index.d.ts +16 -18
- package/esm/core/codegen/index.js +26 -71
- package/esm/core/codegen/invalidation.d.ts +1 -7
- package/esm/core/codegen/invalidation.js +51 -17
- package/esm/core/codegen/mutation-keys.d.ts +1 -10
- package/esm/core/codegen/mutation-keys.js +23 -9
- package/esm/core/codegen/mutations.d.ts +0 -13
- package/esm/core/codegen/mutations.js +302 -367
- package/esm/core/codegen/orm/barrel.d.ts +0 -5
- package/esm/core/codegen/orm/barrel.js +6 -1
- package/esm/core/codegen/orm/client-generator.d.ts +0 -5
- package/esm/core/codegen/orm/client-generator.js +7 -2
- package/esm/core/codegen/orm/client.js +3 -1
- package/esm/core/codegen/orm/custom-ops-generator.d.ts +0 -6
- package/esm/core/codegen/orm/custom-ops-generator.js +103 -50
- package/esm/core/codegen/orm/index.d.ts +4 -4
- package/esm/core/codegen/orm/index.js +25 -12
- package/esm/core/codegen/orm/input-types-generator.d.ts +1 -13
- package/esm/core/codegen/orm/input-types-generator.js +85 -23
- package/esm/core/codegen/orm/model-generator.d.ts +0 -5
- package/esm/core/codegen/orm/model-generator.js +310 -132
- package/esm/core/codegen/orm/select-types.d.ts +19 -14
- package/esm/core/codegen/queries.d.ts +0 -8
- package/esm/core/codegen/queries.js +362 -561
- package/esm/core/codegen/query-keys.d.ts +1 -1
- package/esm/core/codegen/query-keys.js +38 -24
- package/esm/core/codegen/scalars.js +3 -1
- package/esm/core/codegen/schema-types-generator.d.ts +1 -1
- package/esm/core/codegen/schema-types-generator.js +17 -2
- package/esm/core/codegen/select-helpers.d.ts +19 -0
- package/esm/core/codegen/select-helpers.js +35 -0
- package/esm/core/codegen/selection.d.ts +4 -0
- package/esm/core/codegen/selection.js +29 -0
- package/esm/core/codegen/shared/index.d.ts +2 -15
- package/esm/core/codegen/shared/index.js +16 -3
- package/esm/core/codegen/type-resolver.d.ts +1 -5
- package/esm/core/codegen/type-resolver.js +1 -22
- package/esm/core/codegen/types.d.ts +0 -3
- package/esm/core/codegen/types.js +72 -15
- package/esm/core/codegen/utils.d.ts +1 -4
- package/esm/core/codegen/utils.js +4 -1
- package/esm/core/config/index.d.ts +1 -1
- package/esm/core/config/resolver.js +2 -4
- package/esm/core/generate.js +38 -50
- package/esm/core/index.d.ts +3 -3
- package/esm/core/index.js +2 -3
- package/esm/core/introspect/index.d.ts +6 -6
- package/esm/core/introspect/index.js +3 -6
- package/esm/core/introspect/infer-tables.d.ts +0 -14
- package/esm/core/introspect/infer-tables.js +16 -2
- package/esm/core/introspect/source/database.js +2 -2
- package/esm/core/introspect/source/endpoint.d.ts +0 -6
- package/esm/core/introspect/source/endpoint.js +7 -1
- package/esm/core/introspect/source/index.d.ts +4 -4
- package/esm/core/introspect/source/index.js +6 -10
- package/esm/core/introspect/source/pgpm-module.js +3 -3
- package/esm/core/introspect/transform-schema.d.ts +2 -2
- package/esm/core/introspect/transform-schema.js +2 -2
- package/esm/core/output/index.d.ts +1 -1
- package/esm/core/output/index.js +1 -1
- package/esm/core/output/writer.d.ts +3 -0
- package/esm/core/output/writer.js +20 -1
- package/esm/core/pipeline/index.d.ts +2 -2
- package/esm/core/pipeline/index.js +2 -2
- package/esm/core/query-builder.d.ts +2 -2
- package/esm/core/query-builder.js +2 -2
- package/esm/core/watch/index.d.ts +4 -4
- package/esm/core/watch/index.js +3 -3
- package/esm/core/watch/orchestrator.js +5 -3
- package/esm/generators/index.d.ts +3 -3
- package/esm/generators/index.js +3 -3
- package/esm/generators/mutations.d.ts +1 -1
- package/esm/generators/select.d.ts +1 -1
- package/esm/index.d.ts +3 -3
- package/esm/index.js +1 -4
- package/esm/types/config.d.ts +0 -10
- package/esm/types/config.js +0 -2
- package/esm/types/index.d.ts +6 -6
- package/esm/types/index.js +1 -1
- package/generators/index.d.ts +3 -3
- package/generators/index.js +8 -8
- package/generators/mutations.d.ts +1 -1
- package/generators/select.d.ts +1 -1
- package/index.d.ts +3 -3
- package/index.js +11 -6
- package/package.json +10 -10
- package/types/config.d.ts +0 -10
- package/types/config.js +0 -2
- package/types/index.d.ts +6 -6
- package/types/index.js +2 -2
- package/core/codegen/gql-ast.d.ts +0 -41
- package/core/codegen/gql-ast.js +0 -353
- package/core/codegen/schema-gql-ast.d.ts +0 -51
- package/core/codegen/schema-gql-ast.js +0 -385
- package/core/codegen/templates/client.browser.ts +0 -271
- package/core/codegen/templates/client.node.ts +0 -337
- package/esm/core/codegen/gql-ast.d.ts +0 -41
- package/esm/core/codegen/gql-ast.js +0 -312
- package/esm/core/codegen/schema-gql-ast.d.ts +0 -51
- 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
|
|
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,
|
|
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
|
|
61
|
-
|
|
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
|
|
72
|
-
|
|
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
|
-
|
|
75
|
-
statements.push(
|
|
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
|
-
|
|
84
|
-
statements.push(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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 (
|
|
94
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
(
|
|
107
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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 (
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
t.
|
|
134
|
-
]);
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
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
|
|
141
|
-
const
|
|
142
|
-
t.variableDeclarator(t.identifier(queryKeyName),
|
|
143
|
-
]);
|
|
144
|
-
|
|
145
|
-
|
|
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
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
165
|
-
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
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
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
'```
|
|
188
|
-
`const
|
|
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(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
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
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
(
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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,
|
|
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,
|
|
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,
|