@constructive-io/graphql-codegen 2.19.0 → 2.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1818 -113
- package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/__tests__/codegen/input-types-generator.test.js +635 -0
- package/cli/codegen/barrel.d.ts +27 -0
- package/cli/codegen/barrel.js +163 -0
- package/cli/codegen/client.d.ts +4 -0
- package/cli/codegen/client.js +170 -0
- package/cli/codegen/custom-mutations.d.ts +38 -0
- package/cli/codegen/custom-mutations.js +149 -0
- package/cli/codegen/custom-queries.d.ts +38 -0
- package/cli/codegen/custom-queries.js +358 -0
- package/cli/codegen/filters.d.ts +27 -0
- package/cli/codegen/filters.js +357 -0
- package/cli/codegen/gql-ast.d.ts +41 -0
- package/cli/codegen/gql-ast.js +329 -0
- package/cli/codegen/index.d.ts +71 -0
- package/cli/codegen/index.js +147 -0
- package/cli/codegen/mutations.d.ts +30 -0
- package/cli/codegen/mutations.js +410 -0
- package/cli/codegen/orm/barrel.d.ts +18 -0
- package/cli/codegen/orm/barrel.js +48 -0
- package/cli/codegen/orm/client-generator.d.ts +45 -0
- package/cli/codegen/orm/client-generator.js +646 -0
- package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/cli/codegen/orm/custom-ops-generator.js +350 -0
- package/cli/codegen/orm/index.d.ts +38 -0
- package/cli/codegen/orm/index.js +88 -0
- package/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/cli/codegen/orm/input-types-generator.js +705 -0
- package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/cli/codegen/orm/input-types-generator.test.js +75 -0
- package/cli/codegen/orm/model-generator.d.ts +32 -0
- package/cli/codegen/orm/model-generator.js +264 -0
- package/cli/codegen/orm/query-builder.d.ts +161 -0
- package/cli/codegen/orm/query-builder.js +366 -0
- package/cli/codegen/orm/select-types.d.ts +169 -0
- package/cli/codegen/orm/select-types.js +16 -0
- package/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/cli/codegen/orm/select-types.test.js +22 -0
- package/cli/codegen/queries.d.ts +25 -0
- package/cli/codegen/queries.js +438 -0
- package/cli/codegen/scalars.d.ts +12 -0
- package/cli/codegen/scalars.js +71 -0
- package/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/cli/codegen/schema-gql-ast.js +385 -0
- package/cli/codegen/ts-ast.d.ts +122 -0
- package/cli/codegen/ts-ast.js +280 -0
- package/cli/codegen/type-resolver.d.ts +96 -0
- package/cli/codegen/type-resolver.js +246 -0
- package/cli/codegen/types.d.ts +12 -0
- package/cli/codegen/types.js +69 -0
- package/cli/codegen/utils.d.ts +163 -0
- package/cli/codegen/utils.js +326 -0
- package/cli/commands/generate-orm.d.ts +37 -0
- package/cli/commands/generate-orm.js +195 -0
- package/cli/commands/generate.d.ts +39 -0
- package/cli/commands/generate.js +299 -0
- package/cli/commands/index.d.ts +7 -0
- package/cli/commands/index.js +12 -0
- package/cli/commands/init.d.ts +35 -0
- package/cli/commands/init.js +176 -0
- package/cli/index.d.ts +4 -0
- package/cli/index.js +291 -0
- package/cli/introspect/fetch-meta.d.ts +31 -0
- package/cli/introspect/fetch-meta.js +108 -0
- package/cli/introspect/fetch-schema.d.ts +21 -0
- package/cli/introspect/fetch-schema.js +86 -0
- package/cli/introspect/index.d.ts +8 -0
- package/cli/introspect/index.js +16 -0
- package/cli/introspect/meta-query.d.ts +111 -0
- package/cli/introspect/meta-query.js +191 -0
- package/cli/introspect/schema-query.d.ts +20 -0
- package/cli/introspect/schema-query.js +123 -0
- package/cli/introspect/transform-schema.d.ts +74 -0
- package/cli/introspect/transform-schema.js +269 -0
- package/cli/introspect/transform-schema.test.d.ts +1 -0
- package/cli/introspect/transform-schema.test.js +67 -0
- package/cli/introspect/transform.d.ts +21 -0
- package/cli/introspect/transform.js +216 -0
- package/cli/watch/cache.d.ts +45 -0
- package/cli/watch/cache.js +111 -0
- package/cli/watch/debounce.d.ts +19 -0
- package/cli/watch/debounce.js +89 -0
- package/cli/watch/hash.d.ts +17 -0
- package/cli/watch/hash.js +48 -0
- package/cli/watch/index.d.ts +10 -0
- package/cli/watch/index.js +22 -0
- package/cli/watch/orchestrator.d.ts +63 -0
- package/cli/watch/orchestrator.js +228 -0
- package/cli/watch/poller.d.ts +65 -0
- package/cli/watch/poller.js +203 -0
- package/cli/watch/types.d.ts +67 -0
- package/cli/watch/types.js +5 -0
- package/client/error.d.ts +95 -0
- package/client/error.js +255 -0
- package/client/execute.d.ts +57 -0
- package/client/execute.js +124 -0
- package/client/index.d.ts +6 -0
- package/client/index.js +18 -0
- package/client/typed-document.d.ts +31 -0
- package/client/typed-document.js +44 -0
- package/core/ast.d.ts +10 -0
- package/core/ast.js +593 -0
- package/core/custom-ast.d.ts +35 -0
- package/core/custom-ast.js +204 -0
- package/core/index.d.ts +8 -0
- package/core/index.js +33 -0
- package/core/meta-object/convert.d.ts +65 -0
- package/core/meta-object/convert.js +63 -0
- package/core/meta-object/format.json +93 -0
- package/core/meta-object/index.d.ts +2 -0
- package/core/meta-object/index.js +18 -0
- package/core/meta-object/validate.d.ts +9 -0
- package/core/meta-object/validate.js +34 -0
- package/core/query-builder.d.ts +46 -0
- package/core/query-builder.js +412 -0
- package/core/types.d.ts +139 -0
- package/core/types.js +28 -0
- package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
- package/esm/cli/codegen/barrel.d.ts +27 -0
- package/esm/cli/codegen/barrel.js +156 -0
- package/esm/cli/codegen/client.d.ts +4 -0
- package/esm/cli/codegen/client.js +167 -0
- package/esm/cli/codegen/custom-mutations.d.ts +38 -0
- package/esm/cli/codegen/custom-mutations.js +145 -0
- package/esm/cli/codegen/custom-queries.d.ts +38 -0
- package/esm/cli/codegen/custom-queries.js +354 -0
- package/esm/cli/codegen/filters.d.ts +27 -0
- package/esm/cli/codegen/filters.js +351 -0
- package/esm/cli/codegen/gql-ast.d.ts +41 -0
- package/esm/cli/codegen/gql-ast.js +288 -0
- package/esm/cli/codegen/index.d.ts +71 -0
- package/esm/cli/codegen/index.js +124 -0
- package/esm/cli/codegen/mutations.d.ts +30 -0
- package/esm/cli/codegen/mutations.js +404 -0
- package/esm/cli/codegen/orm/barrel.d.ts +18 -0
- package/esm/cli/codegen/orm/barrel.js +44 -0
- package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
- package/esm/cli/codegen/orm/client-generator.js +640 -0
- package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
- package/esm/cli/codegen/orm/index.d.ts +38 -0
- package/esm/cli/codegen/orm/index.js +75 -0
- package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/esm/cli/codegen/orm/input-types-generator.js +700 -0
- package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
- package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
- package/esm/cli/codegen/orm/model-generator.js +260 -0
- package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
- package/esm/cli/codegen/orm/query-builder.js +353 -0
- package/esm/cli/codegen/orm/select-types.d.ts +169 -0
- package/esm/cli/codegen/orm/select-types.js +15 -0
- package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/esm/cli/codegen/orm/select-types.test.js +21 -0
- package/esm/cli/codegen/queries.d.ts +25 -0
- package/esm/cli/codegen/queries.js +433 -0
- package/esm/cli/codegen/scalars.d.ts +12 -0
- package/esm/cli/codegen/scalars.js +66 -0
- package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/esm/cli/codegen/schema-gql-ast.js +343 -0
- package/esm/cli/codegen/ts-ast.d.ts +122 -0
- package/esm/cli/codegen/ts-ast.js +260 -0
- package/esm/cli/codegen/type-resolver.d.ts +96 -0
- package/esm/cli/codegen/type-resolver.js +224 -0
- package/esm/cli/codegen/types.d.ts +12 -0
- package/esm/cli/codegen/types.js +65 -0
- package/esm/cli/codegen/utils.d.ts +163 -0
- package/esm/cli/codegen/utils.js +288 -0
- package/esm/cli/commands/generate-orm.d.ts +37 -0
- package/esm/cli/commands/generate-orm.js +192 -0
- package/esm/cli/commands/generate.d.ts +39 -0
- package/esm/cli/commands/generate.js +262 -0
- package/esm/cli/commands/index.d.ts +7 -0
- package/esm/cli/commands/index.js +5 -0
- package/esm/cli/commands/init.d.ts +35 -0
- package/esm/cli/commands/init.js +138 -0
- package/esm/cli/index.d.ts +4 -0
- package/esm/cli/index.js +256 -0
- package/esm/cli/introspect/fetch-meta.d.ts +31 -0
- package/esm/cli/introspect/fetch-meta.js +104 -0
- package/esm/cli/introspect/fetch-schema.d.ts +21 -0
- package/esm/cli/introspect/fetch-schema.js +83 -0
- package/esm/cli/introspect/index.d.ts +8 -0
- package/esm/cli/introspect/index.js +6 -0
- package/esm/cli/introspect/meta-query.d.ts +111 -0
- package/esm/cli/introspect/meta-query.js +188 -0
- package/esm/cli/introspect/schema-query.d.ts +20 -0
- package/esm/cli/introspect/schema-query.js +120 -0
- package/esm/cli/introspect/transform-schema.d.ts +74 -0
- package/esm/cli/introspect/transform-schema.js +259 -0
- package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
- package/esm/cli/introspect/transform-schema.test.js +65 -0
- package/esm/cli/introspect/transform.d.ts +21 -0
- package/esm/cli/introspect/transform.js +210 -0
- package/esm/cli/watch/cache.d.ts +45 -0
- package/esm/cli/watch/cache.js +73 -0
- package/esm/cli/watch/debounce.d.ts +19 -0
- package/esm/cli/watch/debounce.js +85 -0
- package/esm/cli/watch/hash.d.ts +17 -0
- package/esm/cli/watch/hash.js +43 -0
- package/esm/cli/watch/index.d.ts +10 -0
- package/esm/cli/watch/index.js +8 -0
- package/esm/cli/watch/orchestrator.d.ts +63 -0
- package/esm/cli/watch/orchestrator.js +223 -0
- package/esm/cli/watch/poller.d.ts +65 -0
- package/esm/cli/watch/poller.js +198 -0
- package/esm/cli/watch/types.d.ts +67 -0
- package/esm/cli/watch/types.js +4 -0
- package/esm/client/error.d.ts +95 -0
- package/esm/client/error.js +249 -0
- package/esm/client/execute.d.ts +57 -0
- package/esm/client/execute.js +120 -0
- package/esm/client/index.d.ts +6 -0
- package/esm/client/index.js +6 -0
- package/esm/client/typed-document.d.ts +31 -0
- package/esm/client/typed-document.js +40 -0
- package/esm/core/ast.d.ts +10 -0
- package/esm/core/ast.js +549 -0
- package/esm/core/custom-ast.d.ts +35 -0
- package/esm/core/custom-ast.js +161 -0
- package/esm/core/index.d.ts +8 -0
- package/esm/core/index.js +12 -0
- package/esm/core/meta-object/convert.d.ts +65 -0
- package/esm/core/meta-object/convert.js +60 -0
- package/esm/core/meta-object/format.json +93 -0
- package/esm/core/meta-object/index.d.ts +2 -0
- package/esm/core/meta-object/index.js +2 -0
- package/esm/core/meta-object/validate.d.ts +9 -0
- package/esm/core/meta-object/validate.js +28 -0
- package/esm/core/query-builder.d.ts +46 -0
- package/esm/core/query-builder.js +375 -0
- package/esm/core/types.d.ts +139 -0
- package/esm/core/types.js +24 -0
- package/esm/generators/field-selector.d.ts +30 -0
- package/esm/generators/field-selector.js +355 -0
- package/esm/generators/index.d.ts +6 -0
- package/esm/generators/index.js +9 -0
- package/esm/generators/mutations.d.ts +31 -0
- package/esm/generators/mutations.js +197 -0
- package/esm/generators/select.d.ts +50 -0
- package/esm/generators/select.js +636 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +17 -3
- package/esm/react/index.d.ts +5 -0
- package/esm/react/index.js +6 -0
- package/esm/types/config.d.ts +199 -0
- package/esm/types/config.js +106 -0
- package/esm/types/index.d.ts +9 -0
- package/esm/types/index.js +4 -0
- package/esm/types/introspection.d.ts +121 -0
- package/esm/types/introspection.js +54 -0
- package/esm/types/mutation.d.ts +45 -0
- package/esm/types/mutation.js +4 -0
- package/esm/types/query.d.ts +82 -0
- package/esm/types/query.js +4 -0
- package/esm/types/schema.d.ts +253 -0
- package/esm/types/schema.js +5 -0
- package/esm/types/selection.d.ts +43 -0
- package/esm/types/selection.js +4 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/generators/field-selector.d.ts +30 -0
- package/generators/field-selector.js +361 -0
- package/generators/index.d.ts +6 -0
- package/generators/index.js +27 -0
- package/generators/mutations.d.ts +31 -0
- package/generators/mutations.js +235 -0
- package/generators/select.d.ts +50 -0
- package/generators/select.js +679 -0
- package/index.d.ts +12 -3
- package/index.js +19 -3
- package/package.json +59 -38
- package/react/index.d.ts +5 -0
- package/react/index.js +9 -0
- package/types/config.d.ts +199 -0
- package/types/config.js +111 -0
- package/types/index.d.ts +9 -0
- package/types/index.js +10 -0
- package/types/introspection.d.ts +121 -0
- package/types/introspection.js +62 -0
- package/types/mutation.d.ts +45 -0
- package/types/mutation.js +5 -0
- package/types/query.d.ts +82 -0
- package/types/query.js +5 -0
- package/types/schema.d.ts +253 -0
- package/types/schema.js +6 -0
- package/types/selection.d.ts +43 -0
- package/types/selection.js +5 -0
- package/utils/index.d.ts +4 -0
- package/utils/index.js +7 -0
- package/codegen.d.ts +0 -13
- package/codegen.js +0 -293
- package/esm/codegen.js +0 -253
- package/esm/gql.js +0 -939
- package/esm/options.js +0 -27
- package/gql.d.ts +0 -188
- package/gql.js +0 -992
- package/options.d.ts +0 -45
- package/options.js +0 -31
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateQueriesBarrel = generateQueriesBarrel;
|
|
4
|
+
exports.generateMutationsBarrel = generateMutationsBarrel;
|
|
5
|
+
exports.generateMainBarrel = generateMainBarrel;
|
|
6
|
+
exports.generateCustomQueriesBarrel = generateCustomQueriesBarrel;
|
|
7
|
+
exports.generateCustomMutationsBarrel = generateCustomMutationsBarrel;
|
|
8
|
+
const ts_ast_1 = require("./ts-ast");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const type_resolver_1 = require("./type-resolver");
|
|
11
|
+
/**
|
|
12
|
+
* Generate the queries/index.ts barrel file
|
|
13
|
+
*/
|
|
14
|
+
function generateQueriesBarrel(tables) {
|
|
15
|
+
const lines = [
|
|
16
|
+
(0, ts_ast_1.createFileHeader)('Query hooks barrel export'),
|
|
17
|
+
'',
|
|
18
|
+
];
|
|
19
|
+
// Export all query hooks
|
|
20
|
+
for (const table of tables) {
|
|
21
|
+
const listHookName = (0, utils_1.getListQueryHookName)(table);
|
|
22
|
+
const singleHookName = (0, utils_1.getSingleQueryHookName)(table);
|
|
23
|
+
lines.push(`export * from './${listHookName}';`);
|
|
24
|
+
lines.push(`export * from './${singleHookName}';`);
|
|
25
|
+
}
|
|
26
|
+
return lines.join('\n') + '\n';
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Generate the mutations/index.ts barrel file
|
|
30
|
+
*/
|
|
31
|
+
function generateMutationsBarrel(tables) {
|
|
32
|
+
const lines = [
|
|
33
|
+
(0, ts_ast_1.createFileHeader)('Mutation hooks barrel export'),
|
|
34
|
+
'',
|
|
35
|
+
];
|
|
36
|
+
// Export all mutation hooks
|
|
37
|
+
for (const table of tables) {
|
|
38
|
+
const createHookName = (0, utils_1.getCreateMutationHookName)(table);
|
|
39
|
+
const updateHookName = (0, utils_1.getUpdateMutationHookName)(table);
|
|
40
|
+
const deleteHookName = (0, utils_1.getDeleteMutationHookName)(table);
|
|
41
|
+
lines.push(`export * from './${createHookName}';`);
|
|
42
|
+
// Only add update/delete if they exist
|
|
43
|
+
if (table.query?.update !== null) {
|
|
44
|
+
lines.push(`export * from './${updateHookName}';`);
|
|
45
|
+
}
|
|
46
|
+
if (table.query?.delete !== null) {
|
|
47
|
+
lines.push(`export * from './${deleteHookName}';`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return lines.join('\n') + '\n';
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Generate the main index.ts barrel file
|
|
54
|
+
*/
|
|
55
|
+
function generateMainBarrel(tables) {
|
|
56
|
+
const tableNames = tables.map((t) => t.name).join(', ');
|
|
57
|
+
return `/**
|
|
58
|
+
* Auto-generated GraphQL SDK
|
|
59
|
+
* @generated by @constructive-io/graphql-codegen
|
|
60
|
+
*
|
|
61
|
+
* Tables: ${tableNames}
|
|
62
|
+
*
|
|
63
|
+
* Usage:
|
|
64
|
+
*
|
|
65
|
+
* 1. Configure the client:
|
|
66
|
+
* \`\`\`ts
|
|
67
|
+
* import { configure } from './generated';
|
|
68
|
+
*
|
|
69
|
+
* configure({
|
|
70
|
+
* endpoint: 'https://api.example.com/graphql',
|
|
71
|
+
* headers: { Authorization: 'Bearer <token>' },
|
|
72
|
+
* });
|
|
73
|
+
* \`\`\`
|
|
74
|
+
*
|
|
75
|
+
* 2. Use the hooks:
|
|
76
|
+
* \`\`\`tsx
|
|
77
|
+
* import { useCarsQuery, useCreateCarMutation } from './generated';
|
|
78
|
+
*
|
|
79
|
+
* function MyComponent() {
|
|
80
|
+
* const { data, isLoading } = useCarsQuery({ first: 10 });
|
|
81
|
+
* const { mutate } = useCreateCarMutation();
|
|
82
|
+
* // ...
|
|
83
|
+
* }
|
|
84
|
+
* \`\`\`
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
// Client configuration
|
|
88
|
+
export * from './client';
|
|
89
|
+
|
|
90
|
+
// Entity and filter types
|
|
91
|
+
export * from './types';
|
|
92
|
+
|
|
93
|
+
// Query hooks
|
|
94
|
+
export * from './queries';
|
|
95
|
+
|
|
96
|
+
// Mutation hooks
|
|
97
|
+
export * from './mutations';
|
|
98
|
+
`;
|
|
99
|
+
}
|
|
100
|
+
// ============================================================================
|
|
101
|
+
// Custom operation barrels (includes both table and custom hooks)
|
|
102
|
+
// ============================================================================
|
|
103
|
+
/**
|
|
104
|
+
* Generate queries barrel including custom query operations
|
|
105
|
+
*/
|
|
106
|
+
function generateCustomQueriesBarrel(tables, customQueryNames) {
|
|
107
|
+
const lines = [
|
|
108
|
+
(0, ts_ast_1.createFileHeader)('Query hooks barrel export'),
|
|
109
|
+
'',
|
|
110
|
+
'// Table-based query hooks',
|
|
111
|
+
];
|
|
112
|
+
// Export all table query hooks
|
|
113
|
+
for (const table of tables) {
|
|
114
|
+
const listHookName = (0, utils_1.getListQueryHookName)(table);
|
|
115
|
+
const singleHookName = (0, utils_1.getSingleQueryHookName)(table);
|
|
116
|
+
lines.push(`export * from './${listHookName}';`);
|
|
117
|
+
lines.push(`export * from './${singleHookName}';`);
|
|
118
|
+
}
|
|
119
|
+
// Add custom query hooks
|
|
120
|
+
if (customQueryNames.length > 0) {
|
|
121
|
+
lines.push('');
|
|
122
|
+
lines.push('// Custom query hooks');
|
|
123
|
+
for (const name of customQueryNames) {
|
|
124
|
+
const hookName = (0, type_resolver_1.getOperationHookName)(name, 'query');
|
|
125
|
+
lines.push(`export * from './${hookName}';`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return lines.join('\n') + '\n';
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Generate mutations barrel including custom mutation operations
|
|
132
|
+
*/
|
|
133
|
+
function generateCustomMutationsBarrel(tables, customMutationNames) {
|
|
134
|
+
const lines = [
|
|
135
|
+
(0, ts_ast_1.createFileHeader)('Mutation hooks barrel export'),
|
|
136
|
+
'',
|
|
137
|
+
'// Table-based mutation hooks',
|
|
138
|
+
];
|
|
139
|
+
// Export all table mutation hooks
|
|
140
|
+
for (const table of tables) {
|
|
141
|
+
const createHookName = (0, utils_1.getCreateMutationHookName)(table);
|
|
142
|
+
const updateHookName = (0, utils_1.getUpdateMutationHookName)(table);
|
|
143
|
+
const deleteHookName = (0, utils_1.getDeleteMutationHookName)(table);
|
|
144
|
+
lines.push(`export * from './${createHookName}';`);
|
|
145
|
+
// Only add update/delete if they exist
|
|
146
|
+
if (table.query?.update !== null) {
|
|
147
|
+
lines.push(`export * from './${updateHookName}';`);
|
|
148
|
+
}
|
|
149
|
+
if (table.query?.delete !== null) {
|
|
150
|
+
lines.push(`export * from './${deleteHookName}';`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Add custom mutation hooks
|
|
154
|
+
if (customMutationNames.length > 0) {
|
|
155
|
+
lines.push('');
|
|
156
|
+
lines.push('// Custom mutation hooks');
|
|
157
|
+
for (const name of customMutationNames) {
|
|
158
|
+
const hookName = (0, type_resolver_1.getOperationHookName)(name, 'mutation');
|
|
159
|
+
lines.push(`export * from './${hookName}';`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return lines.join('\n') + '\n';
|
|
163
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateClientFile = generateClientFile;
|
|
4
|
+
/**
|
|
5
|
+
* Client generator - generates client.ts with configure() and execute()
|
|
6
|
+
*/
|
|
7
|
+
const utils_1 = require("./utils");
|
|
8
|
+
/**
|
|
9
|
+
* Generate client.ts content
|
|
10
|
+
*/
|
|
11
|
+
function generateClientFile() {
|
|
12
|
+
return `${(0, utils_1.getGeneratedFileHeader)('GraphQL client configuration and execution')}
|
|
13
|
+
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Configuration
|
|
16
|
+
// ============================================================================
|
|
17
|
+
|
|
18
|
+
export interface GraphQLClientConfig {
|
|
19
|
+
/** GraphQL endpoint URL */
|
|
20
|
+
endpoint: string;
|
|
21
|
+
/** Default headers to include in all requests */
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let globalConfig: GraphQLClientConfig | null = null;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Configure the GraphQL client
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* \`\`\`ts
|
|
32
|
+
* import { configure } from './generated';
|
|
33
|
+
*
|
|
34
|
+
* configure({
|
|
35
|
+
* endpoint: 'https://api.example.com/graphql',
|
|
36
|
+
* headers: {
|
|
37
|
+
* Authorization: 'Bearer <token>',
|
|
38
|
+
* },
|
|
39
|
+
* });
|
|
40
|
+
* \`\`\`
|
|
41
|
+
*/
|
|
42
|
+
export function configure(config: GraphQLClientConfig): void {
|
|
43
|
+
globalConfig = config;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Get the current configuration
|
|
48
|
+
* @throws Error if not configured
|
|
49
|
+
*/
|
|
50
|
+
export function getConfig(): GraphQLClientConfig {
|
|
51
|
+
if (!globalConfig) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
'GraphQL client not configured. Call configure() before making requests.'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return globalConfig;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ============================================================================
|
|
60
|
+
// Error handling
|
|
61
|
+
// ============================================================================
|
|
62
|
+
|
|
63
|
+
export interface GraphQLError {
|
|
64
|
+
message: string;
|
|
65
|
+
locations?: Array<{ line: number; column: number }>;
|
|
66
|
+
path?: Array<string | number>;
|
|
67
|
+
extensions?: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class GraphQLClientError extends Error {
|
|
71
|
+
constructor(
|
|
72
|
+
message: string,
|
|
73
|
+
public errors: GraphQLError[],
|
|
74
|
+
public response?: Response
|
|
75
|
+
) {
|
|
76
|
+
super(message);
|
|
77
|
+
this.name = 'GraphQLClientError';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ============================================================================
|
|
82
|
+
// Execution
|
|
83
|
+
// ============================================================================
|
|
84
|
+
|
|
85
|
+
export interface ExecuteOptions {
|
|
86
|
+
/** Override headers for this request */
|
|
87
|
+
headers?: Record<string, string>;
|
|
88
|
+
/** AbortSignal for request cancellation */
|
|
89
|
+
signal?: AbortSignal;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Execute a GraphQL operation
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* \`\`\`ts
|
|
97
|
+
* const result = await execute<CarsQueryResult, CarsQueryVariables>(
|
|
98
|
+
* carsQueryDocument,
|
|
99
|
+
* { first: 10 }
|
|
100
|
+
* );
|
|
101
|
+
* \`\`\`
|
|
102
|
+
*/
|
|
103
|
+
export async function execute<TData = unknown, TVariables = Record<string, unknown>>(
|
|
104
|
+
document: string,
|
|
105
|
+
variables?: TVariables,
|
|
106
|
+
options?: ExecuteOptions
|
|
107
|
+
): Promise<TData> {
|
|
108
|
+
const config = getConfig();
|
|
109
|
+
|
|
110
|
+
const response = await fetch(config.endpoint, {
|
|
111
|
+
method: 'POST',
|
|
112
|
+
headers: {
|
|
113
|
+
'Content-Type': 'application/json',
|
|
114
|
+
...config.headers,
|
|
115
|
+
...options?.headers,
|
|
116
|
+
},
|
|
117
|
+
body: JSON.stringify({
|
|
118
|
+
query: document,
|
|
119
|
+
variables,
|
|
120
|
+
}),
|
|
121
|
+
signal: options?.signal,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const json = await response.json();
|
|
125
|
+
|
|
126
|
+
if (json.errors && json.errors.length > 0) {
|
|
127
|
+
throw new GraphQLClientError(
|
|
128
|
+
json.errors[0].message || 'GraphQL request failed',
|
|
129
|
+
json.errors,
|
|
130
|
+
response
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return json.data as TData;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Execute a GraphQL operation with full response (data + errors)
|
|
139
|
+
* Useful when you want to handle partial data with errors
|
|
140
|
+
*/
|
|
141
|
+
export async function executeWithErrors<TData = unknown, TVariables = Record<string, unknown>>(
|
|
142
|
+
document: string,
|
|
143
|
+
variables?: TVariables,
|
|
144
|
+
options?: ExecuteOptions
|
|
145
|
+
): Promise<{ data: TData | null; errors: GraphQLError[] | null }> {
|
|
146
|
+
const config = getConfig();
|
|
147
|
+
|
|
148
|
+
const response = await fetch(config.endpoint, {
|
|
149
|
+
method: 'POST',
|
|
150
|
+
headers: {
|
|
151
|
+
'Content-Type': 'application/json',
|
|
152
|
+
...config.headers,
|
|
153
|
+
...options?.headers,
|
|
154
|
+
},
|
|
155
|
+
body: JSON.stringify({
|
|
156
|
+
query: document,
|
|
157
|
+
variables,
|
|
158
|
+
}),
|
|
159
|
+
signal: options?.signal,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const json = await response.json();
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
data: json.data ?? null,
|
|
166
|
+
errors: json.errors ?? null,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
`;
|
|
170
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom mutation hook generators for non-table operations
|
|
3
|
+
*
|
|
4
|
+
* Generates hooks for operations discovered via schema introspection
|
|
5
|
+
* that are NOT table CRUD operations (e.g., login, register, etc.)
|
|
6
|
+
*
|
|
7
|
+
* Output structure:
|
|
8
|
+
* mutations/
|
|
9
|
+
* useLoginMutation.ts
|
|
10
|
+
* useRegisterMutation.ts
|
|
11
|
+
* ...
|
|
12
|
+
*/
|
|
13
|
+
import type { CleanOperation, TypeRegistry } from '../../types/schema';
|
|
14
|
+
export interface GeneratedCustomMutationFile {
|
|
15
|
+
fileName: string;
|
|
16
|
+
content: string;
|
|
17
|
+
operationName: string;
|
|
18
|
+
}
|
|
19
|
+
export interface GenerateCustomMutationHookOptions {
|
|
20
|
+
operation: CleanOperation;
|
|
21
|
+
typeRegistry: TypeRegistry;
|
|
22
|
+
maxDepth?: number;
|
|
23
|
+
skipQueryField?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generate a custom mutation hook file
|
|
27
|
+
*/
|
|
28
|
+
export declare function generateCustomMutationHook(options: GenerateCustomMutationHookOptions): GeneratedCustomMutationFile;
|
|
29
|
+
export interface GenerateAllCustomMutationHooksOptions {
|
|
30
|
+
operations: CleanOperation[];
|
|
31
|
+
typeRegistry: TypeRegistry;
|
|
32
|
+
maxDepth?: number;
|
|
33
|
+
skipQueryField?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Generate all custom mutation hook files
|
|
37
|
+
*/
|
|
38
|
+
export declare function generateAllCustomMutationHooks(options: GenerateAllCustomMutationHooksOptions): GeneratedCustomMutationFile[];
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCustomMutationHook = generateCustomMutationHook;
|
|
4
|
+
exports.generateAllCustomMutationHooks = generateAllCustomMutationHooks;
|
|
5
|
+
const ts_ast_1 = require("./ts-ast");
|
|
6
|
+
const schema_gql_ast_1 = require("./schema-gql-ast");
|
|
7
|
+
const type_resolver_1 = require("./type-resolver");
|
|
8
|
+
/**
|
|
9
|
+
* Generate a custom mutation hook file
|
|
10
|
+
*/
|
|
11
|
+
function generateCustomMutationHook(options) {
|
|
12
|
+
const { operation } = options;
|
|
13
|
+
try {
|
|
14
|
+
return generateCustomMutationHookInternal(options);
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
console.error(`Error generating hook for mutation: ${operation.name}`);
|
|
18
|
+
console.error(` Args: ${operation.args.length}, Return type: ${operation.returnType.kind}/${operation.returnType.name}`);
|
|
19
|
+
throw err;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function generateCustomMutationHookInternal(options) {
|
|
23
|
+
const { operation, typeRegistry, maxDepth = 2, skipQueryField = true } = options;
|
|
24
|
+
const project = (0, ts_ast_1.createProject)();
|
|
25
|
+
const hookName = (0, type_resolver_1.getOperationHookName)(operation.name, 'mutation');
|
|
26
|
+
const fileName = (0, type_resolver_1.getOperationFileName)(operation.name, 'mutation');
|
|
27
|
+
const variablesTypeName = (0, type_resolver_1.getOperationVariablesTypeName)(operation.name, 'mutation');
|
|
28
|
+
const resultTypeName = (0, type_resolver_1.getOperationResultTypeName)(operation.name, 'mutation');
|
|
29
|
+
const documentConstName = (0, type_resolver_1.getDocumentConstName)(operation.name, 'mutation');
|
|
30
|
+
// Generate GraphQL document
|
|
31
|
+
const mutationDocument = (0, schema_gql_ast_1.buildCustomMutationString)({
|
|
32
|
+
operation,
|
|
33
|
+
typeRegistry,
|
|
34
|
+
maxDepth,
|
|
35
|
+
skipQueryField,
|
|
36
|
+
});
|
|
37
|
+
const sourceFile = (0, ts_ast_1.createSourceFile)(project, fileName);
|
|
38
|
+
// Add file header
|
|
39
|
+
sourceFile.insertText(0, (0, ts_ast_1.createFileHeader)(`Custom mutation hook for ${operation.name}`) + '\n\n');
|
|
40
|
+
// Add imports
|
|
41
|
+
sourceFile.addImportDeclarations([
|
|
42
|
+
(0, ts_ast_1.createImport)({
|
|
43
|
+
moduleSpecifier: '@tanstack/react-query',
|
|
44
|
+
namedImports: ['useMutation'],
|
|
45
|
+
typeOnlyNamedImports: ['UseMutationOptions'],
|
|
46
|
+
}),
|
|
47
|
+
(0, ts_ast_1.createImport)({
|
|
48
|
+
moduleSpecifier: '../client',
|
|
49
|
+
namedImports: ['execute'],
|
|
50
|
+
}),
|
|
51
|
+
]);
|
|
52
|
+
// Add mutation document constant
|
|
53
|
+
sourceFile.addVariableStatement((0, ts_ast_1.createConst)(documentConstName, '`\n' + mutationDocument + '`', {
|
|
54
|
+
docs: ['GraphQL mutation document'],
|
|
55
|
+
}));
|
|
56
|
+
// Generate variables interface if there are arguments
|
|
57
|
+
if (operation.args.length > 0) {
|
|
58
|
+
const variablesProps = generateVariablesProperties(operation.args);
|
|
59
|
+
sourceFile.addInterface((0, ts_ast_1.createInterface)(variablesTypeName, variablesProps));
|
|
60
|
+
}
|
|
61
|
+
// Generate result interface
|
|
62
|
+
const resultType = (0, type_resolver_1.typeRefToTsType)(operation.returnType);
|
|
63
|
+
const resultProps = [
|
|
64
|
+
{ name: operation.name, type: resultType },
|
|
65
|
+
];
|
|
66
|
+
sourceFile.addInterface((0, ts_ast_1.createInterface)(resultTypeName, resultProps));
|
|
67
|
+
// Generate hook function
|
|
68
|
+
const hookParams = generateHookParameters(operation, variablesTypeName, resultTypeName);
|
|
69
|
+
const hookBody = generateHookBody(operation, documentConstName, variablesTypeName, resultTypeName);
|
|
70
|
+
// Note: docs can cause ts-morph issues with certain content, so we skip them
|
|
71
|
+
sourceFile.addFunction({
|
|
72
|
+
name: hookName,
|
|
73
|
+
isExported: true,
|
|
74
|
+
parameters: hookParams,
|
|
75
|
+
statements: hookBody,
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
fileName,
|
|
79
|
+
content: (0, ts_ast_1.getFormattedOutput)(sourceFile),
|
|
80
|
+
operationName: operation.name,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// ============================================================================
|
|
84
|
+
// Helper functions
|
|
85
|
+
// ============================================================================
|
|
86
|
+
/**
|
|
87
|
+
* Generate interface properties from CleanArguments
|
|
88
|
+
*/
|
|
89
|
+
function generateVariablesProperties(args) {
|
|
90
|
+
return args.map((arg) => ({
|
|
91
|
+
name: arg.name,
|
|
92
|
+
type: (0, type_resolver_1.typeRefToTsType)(arg.type),
|
|
93
|
+
optional: !(0, type_resolver_1.isTypeRequired)(arg.type),
|
|
94
|
+
docs: arg.description ? [arg.description] : undefined,
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Generate hook function parameters
|
|
99
|
+
*/
|
|
100
|
+
function generateHookParameters(operation, variablesTypeName, resultTypeName) {
|
|
101
|
+
const hasArgs = operation.args.length > 0;
|
|
102
|
+
// Mutation hooks use UseMutationOptions with variables as the second type param
|
|
103
|
+
const optionsType = hasArgs
|
|
104
|
+
? `Omit<UseMutationOptions<${resultTypeName}, Error, ${variablesTypeName}>, 'mutationFn'>`
|
|
105
|
+
: `Omit<UseMutationOptions<${resultTypeName}, Error, void>, 'mutationFn'>`;
|
|
106
|
+
return [
|
|
107
|
+
{
|
|
108
|
+
name: 'options',
|
|
109
|
+
type: optionsType,
|
|
110
|
+
hasQuestionToken: true,
|
|
111
|
+
},
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Generate hook function body
|
|
116
|
+
*/
|
|
117
|
+
function generateHookBody(operation, documentConstName, variablesTypeName, resultTypeName) {
|
|
118
|
+
const hasArgs = operation.args.length > 0;
|
|
119
|
+
if (hasArgs) {
|
|
120
|
+
return `return useMutation({
|
|
121
|
+
mutationFn: (variables: ${variablesTypeName}) =>
|
|
122
|
+
execute<${resultTypeName}, ${variablesTypeName}>(
|
|
123
|
+
${documentConstName},
|
|
124
|
+
variables
|
|
125
|
+
),
|
|
126
|
+
...options,
|
|
127
|
+
});`;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return `return useMutation({
|
|
131
|
+
mutationFn: () => execute<${resultTypeName}>(${documentConstName}),
|
|
132
|
+
...options,
|
|
133
|
+
});`;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Generate all custom mutation hook files
|
|
138
|
+
*/
|
|
139
|
+
function generateAllCustomMutationHooks(options) {
|
|
140
|
+
const { operations, typeRegistry, maxDepth = 2, skipQueryField = true } = options;
|
|
141
|
+
return operations
|
|
142
|
+
.filter((op) => op.kind === 'mutation')
|
|
143
|
+
.map((operation) => generateCustomMutationHook({
|
|
144
|
+
operation,
|
|
145
|
+
typeRegistry,
|
|
146
|
+
maxDepth,
|
|
147
|
+
skipQueryField,
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom query hook generators for non-table operations
|
|
3
|
+
*
|
|
4
|
+
* Generates hooks for operations discovered via schema introspection
|
|
5
|
+
* that are NOT table CRUD operations (e.g., currentUser, nodeById, etc.)
|
|
6
|
+
*
|
|
7
|
+
* Output structure:
|
|
8
|
+
* queries/
|
|
9
|
+
* useCurrentUserQuery.ts
|
|
10
|
+
* useNodeQuery.ts
|
|
11
|
+
* ...
|
|
12
|
+
*/
|
|
13
|
+
import type { CleanOperation, TypeRegistry } from '../../types/schema';
|
|
14
|
+
export interface GeneratedCustomQueryFile {
|
|
15
|
+
fileName: string;
|
|
16
|
+
content: string;
|
|
17
|
+
operationName: string;
|
|
18
|
+
}
|
|
19
|
+
export interface GenerateCustomQueryHookOptions {
|
|
20
|
+
operation: CleanOperation;
|
|
21
|
+
typeRegistry: TypeRegistry;
|
|
22
|
+
maxDepth?: number;
|
|
23
|
+
skipQueryField?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generate a custom query hook file
|
|
27
|
+
*/
|
|
28
|
+
export declare function generateCustomQueryHook(options: GenerateCustomQueryHookOptions): GeneratedCustomQueryFile;
|
|
29
|
+
export interface GenerateAllCustomQueryHooksOptions {
|
|
30
|
+
operations: CleanOperation[];
|
|
31
|
+
typeRegistry: TypeRegistry;
|
|
32
|
+
maxDepth?: number;
|
|
33
|
+
skipQueryField?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Generate all custom query hook files
|
|
37
|
+
*/
|
|
38
|
+
export declare function generateAllCustomQueryHooks(options: GenerateAllCustomQueryHooksOptions): GeneratedCustomQueryFile[];
|