@constructive-io/graphql-codegen 2.24.0 → 2.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +403 -279
  2. package/cli/codegen/babel-ast.d.ts +7 -0
  3. package/cli/codegen/babel-ast.js +15 -0
  4. package/cli/codegen/barrel.js +43 -14
  5. package/cli/codegen/custom-mutations.js +4 -4
  6. package/cli/codegen/custom-queries.js +12 -22
  7. package/cli/codegen/gql-ast.js +22 -1
  8. package/cli/codegen/index.js +1 -0
  9. package/cli/codegen/mutations.d.ts +2 -0
  10. package/cli/codegen/mutations.js +26 -13
  11. package/cli/codegen/orm/client-generator.js +475 -136
  12. package/cli/codegen/orm/custom-ops-generator.js +8 -3
  13. package/cli/codegen/orm/input-types-generator.js +22 -0
  14. package/cli/codegen/orm/model-generator.js +18 -5
  15. package/cli/codegen/orm/select-types.d.ts +33 -0
  16. package/cli/codegen/queries.d.ts +1 -1
  17. package/cli/codegen/queries.js +112 -35
  18. package/cli/codegen/utils.d.ts +6 -0
  19. package/cli/codegen/utils.js +19 -0
  20. package/cli/commands/generate-orm.d.ts +14 -0
  21. package/cli/commands/generate-orm.js +160 -44
  22. package/cli/commands/generate.d.ts +22 -0
  23. package/cli/commands/generate.js +195 -55
  24. package/cli/commands/init.js +29 -9
  25. package/cli/index.js +133 -28
  26. package/cli/watch/orchestrator.d.ts +4 -0
  27. package/cli/watch/orchestrator.js +4 -0
  28. package/esm/cli/codegen/babel-ast.d.ts +7 -0
  29. package/esm/cli/codegen/babel-ast.js +14 -0
  30. package/esm/cli/codegen/barrel.js +44 -15
  31. package/esm/cli/codegen/custom-mutations.js +5 -5
  32. package/esm/cli/codegen/custom-queries.js +13 -23
  33. package/esm/cli/codegen/gql-ast.js +23 -2
  34. package/esm/cli/codegen/index.js +1 -0
  35. package/esm/cli/codegen/mutations.d.ts +2 -0
  36. package/esm/cli/codegen/mutations.js +27 -14
  37. package/esm/cli/codegen/orm/client-generator.js +475 -136
  38. package/esm/cli/codegen/orm/custom-ops-generator.js +8 -3
  39. package/esm/cli/codegen/orm/input-types-generator.js +22 -0
  40. package/esm/cli/codegen/orm/model-generator.js +18 -5
  41. package/esm/cli/codegen/orm/select-types.d.ts +33 -0
  42. package/esm/cli/codegen/queries.d.ts +1 -1
  43. package/esm/cli/codegen/queries.js +114 -37
  44. package/esm/cli/codegen/utils.d.ts +6 -0
  45. package/esm/cli/codegen/utils.js +18 -0
  46. package/esm/cli/commands/generate-orm.d.ts +14 -0
  47. package/esm/cli/commands/generate-orm.js +161 -45
  48. package/esm/cli/commands/generate.d.ts +22 -0
  49. package/esm/cli/commands/generate.js +195 -56
  50. package/esm/cli/commands/init.js +29 -9
  51. package/esm/cli/index.js +134 -29
  52. package/esm/cli/watch/orchestrator.d.ts +4 -0
  53. package/esm/cli/watch/orchestrator.js +5 -1
  54. package/esm/types/config.d.ts +39 -2
  55. package/esm/types/config.js +88 -4
  56. package/esm/types/index.d.ts +2 -2
  57. package/esm/types/index.js +1 -1
  58. package/package.json +10 -7
  59. package/types/config.d.ts +39 -2
  60. package/types/config.js +91 -4
  61. package/types/index.d.ts +2 -2
  62. package/types/index.js +2 -1
  63. package/cli/codegen/orm/query-builder.d.ts +0 -161
  64. package/cli/codegen/orm/query-builder.js +0 -366
  65. package/esm/cli/codegen/orm/query-builder.d.ts +0 -161
  66. package/esm/cli/codegen/orm/query-builder.js +0 -353
@@ -1,161 +0,0 @@
1
- /**
2
- * Runtime query builder for ORM client
3
- *
4
- * This module provides the runtime functionality that builds GraphQL
5
- * queries/mutations from the fluent API calls and executes them.
6
- *
7
- * This file will be copied to the generated output (not generated via AST)
8
- * since it's runtime code that doesn't change based on schema.
9
- */
10
- import type { QueryResult } from './select-types';
11
- /**
12
- * ORM client configuration
13
- */
14
- export interface OrmClientConfig {
15
- endpoint: string;
16
- headers?: Record<string, string>;
17
- }
18
- /**
19
- * Internal client state
20
- */
21
- export declare class OrmClient {
22
- private endpoint;
23
- private headers;
24
- constructor(config: OrmClientConfig);
25
- /**
26
- * Execute a GraphQL query/mutation
27
- */
28
- execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
29
- /**
30
- * Update headers (e.g., for auth token refresh)
31
- */
32
- setHeaders(headers: Record<string, string>): void;
33
- /**
34
- * Get current endpoint
35
- */
36
- getEndpoint(): string;
37
- }
38
- /**
39
- * Configuration for building a query
40
- */
41
- export interface QueryBuilderConfig {
42
- client: OrmClient;
43
- operation: 'query' | 'mutation';
44
- operationName: string;
45
- fieldName: string;
46
- document: string;
47
- variables?: Record<string, unknown>;
48
- }
49
- /**
50
- * Query builder that holds the query configuration and executes it
51
- *
52
- * Usage:
53
- * ```typescript
54
- * const result = await new QueryBuilder(config).execute();
55
- * ```
56
- */
57
- export declare class QueryBuilder<TResult> {
58
- private config;
59
- constructor(config: QueryBuilderConfig);
60
- /**
61
- * Execute the query and return the result
62
- */
63
- execute(): Promise<QueryResult<TResult>>;
64
- /**
65
- * Get the GraphQL document (useful for debugging)
66
- */
67
- toGraphQL(): string;
68
- /**
69
- * Get the variables (useful for debugging)
70
- */
71
- getVariables(): Record<string, unknown> | undefined;
72
- }
73
- /**
74
- * Build field selections from a select object
75
- *
76
- * Converts:
77
- * { id: true, name: true, posts: { select: { title: true } } }
78
- *
79
- * To:
80
- * id
81
- * name
82
- * posts {
83
- * nodes { title }
84
- * totalCount
85
- * pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
86
- * }
87
- */
88
- export declare function buildSelections(select: Record<string, unknown> | undefined, fieldMeta?: Record<string, FieldMeta>): string;
89
- /**
90
- * Field metadata for determining connection vs direct relation
91
- */
92
- export interface FieldMeta {
93
- isConnection: boolean;
94
- isNullable: boolean;
95
- typeName: string;
96
- }
97
- /**
98
- * Build a findMany query document
99
- */
100
- export declare function buildFindManyDocument(operationName: string, queryField: string, select: Record<string, unknown> | undefined, args: {
101
- where?: Record<string, unknown>;
102
- orderBy?: string[];
103
- first?: number;
104
- last?: number;
105
- after?: string;
106
- before?: string;
107
- offset?: number;
108
- }, filterTypeName: string, orderByTypeName: string): {
109
- document: string;
110
- variables: Record<string, unknown>;
111
- };
112
- /**
113
- * Build a findFirst query document
114
- */
115
- export declare function buildFindFirstDocument(operationName: string, queryField: string, select: Record<string, unknown> | undefined, args: {
116
- where?: Record<string, unknown>;
117
- }, filterTypeName: string): {
118
- document: string;
119
- variables: Record<string, unknown>;
120
- };
121
- /**
122
- * Build a create mutation document
123
- */
124
- export declare function buildCreateDocument(operationName: string, mutationField: string, entityField: string, select: Record<string, unknown> | undefined, data: Record<string, unknown>, inputTypeName: string): {
125
- document: string;
126
- variables: Record<string, unknown>;
127
- };
128
- /**
129
- * Build an update mutation document
130
- */
131
- export declare function buildUpdateDocument(operationName: string, mutationField: string, entityField: string, select: Record<string, unknown> | undefined, where: Record<string, unknown>, data: Record<string, unknown>, inputTypeName: string): {
132
- document: string;
133
- variables: Record<string, unknown>;
134
- };
135
- /**
136
- * Build a delete mutation document
137
- */
138
- export declare function buildDeleteDocument(operationName: string, mutationField: string, entityField: string, where: Record<string, unknown>, inputTypeName: string): {
139
- document: string;
140
- variables: Record<string, unknown>;
141
- };
142
- /**
143
- * Build a custom query document
144
- */
145
- export declare function buildCustomQueryDocument(operationName: string, queryField: string, select: Record<string, unknown> | undefined, args: Record<string, unknown> | undefined, variableDefinitions: Array<{
146
- name: string;
147
- type: string;
148
- }>): {
149
- document: string;
150
- variables: Record<string, unknown>;
151
- };
152
- /**
153
- * Build a custom mutation document
154
- */
155
- export declare function buildCustomMutationDocument(operationName: string, mutationField: string, select: Record<string, unknown> | undefined, args: Record<string, unknown> | undefined, variableDefinitions: Array<{
156
- name: string;
157
- type: string;
158
- }>): {
159
- document: string;
160
- variables: Record<string, unknown>;
161
- };
@@ -1,353 +0,0 @@
1
- /**
2
- * Runtime query builder for ORM client
3
- *
4
- * This module provides the runtime functionality that builds GraphQL
5
- * queries/mutations from the fluent API calls and executes them.
6
- *
7
- * This file will be copied to the generated output (not generated via AST)
8
- * since it's runtime code that doesn't change based on schema.
9
- */
10
- /**
11
- * Internal client state
12
- */
13
- export class OrmClient {
14
- endpoint;
15
- headers;
16
- constructor(config) {
17
- this.endpoint = config.endpoint;
18
- this.headers = config.headers ?? {};
19
- }
20
- /**
21
- * Execute a GraphQL query/mutation
22
- */
23
- async execute(document, variables) {
24
- const response = await fetch(this.endpoint, {
25
- method: 'POST',
26
- headers: {
27
- 'Content-Type': 'application/json',
28
- Accept: 'application/json',
29
- ...this.headers,
30
- },
31
- body: JSON.stringify({
32
- query: document,
33
- variables: variables ?? {},
34
- }),
35
- });
36
- if (!response.ok) {
37
- return {
38
- data: null,
39
- errors: [
40
- {
41
- message: `HTTP ${response.status}: ${response.statusText}`,
42
- },
43
- ],
44
- };
45
- }
46
- const json = (await response.json());
47
- return {
48
- data: json.data ?? null,
49
- errors: json.errors,
50
- };
51
- }
52
- /**
53
- * Update headers (e.g., for auth token refresh)
54
- */
55
- setHeaders(headers) {
56
- this.headers = { ...this.headers, ...headers };
57
- }
58
- /**
59
- * Get current endpoint
60
- */
61
- getEndpoint() {
62
- return this.endpoint;
63
- }
64
- }
65
- /**
66
- * Query builder that holds the query configuration and executes it
67
- *
68
- * Usage:
69
- * ```typescript
70
- * const result = await new QueryBuilder(config).execute();
71
- * ```
72
- */
73
- export class QueryBuilder {
74
- config;
75
- constructor(config) {
76
- this.config = config;
77
- }
78
- /**
79
- * Execute the query and return the result
80
- */
81
- async execute() {
82
- return this.config.client.execute(this.config.document, this.config.variables);
83
- }
84
- /**
85
- * Get the GraphQL document (useful for debugging)
86
- */
87
- toGraphQL() {
88
- return this.config.document;
89
- }
90
- /**
91
- * Get the variables (useful for debugging)
92
- */
93
- getVariables() {
94
- return this.config.variables;
95
- }
96
- }
97
- // ============================================================================
98
- // GraphQL Document Builders (Runtime)
99
- // ============================================================================
100
- /**
101
- * Build field selections from a select object
102
- *
103
- * Converts:
104
- * { id: true, name: true, posts: { select: { title: true } } }
105
- *
106
- * To:
107
- * id
108
- * name
109
- * posts {
110
- * nodes { title }
111
- * totalCount
112
- * pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
113
- * }
114
- */
115
- export function buildSelections(select, fieldMeta) {
116
- if (!select) {
117
- return '';
118
- }
119
- const fields = [];
120
- for (const [key, value] of Object.entries(select)) {
121
- if (value === false || value === undefined) {
122
- continue;
123
- }
124
- if (value === true) {
125
- fields.push(key);
126
- continue;
127
- }
128
- // Nested select
129
- if (typeof value === 'object' && value !== null) {
130
- const nested = value;
131
- const meta = fieldMeta?.[key];
132
- const isConnection = meta?.isConnection ?? true; // Default to connection for relations
133
- if (nested.select) {
134
- const nestedSelections = buildSelections(nested.select);
135
- // Build arguments for the relation field
136
- const args = [];
137
- if (nested.first !== undefined) {
138
- args.push(`first: ${nested.first}`);
139
- }
140
- if (nested.filter) {
141
- args.push(`filter: ${JSON.stringify(nested.filter)}`);
142
- }
143
- if (nested.orderBy && nested.orderBy.length > 0) {
144
- args.push(`orderBy: [${nested.orderBy.join(', ')}]`);
145
- }
146
- const argsStr = args.length > 0 ? `(${args.join(', ')})` : '';
147
- if (isConnection) {
148
- // Connection type - include nodes, totalCount, pageInfo
149
- fields.push(`${key}${argsStr} {
150
- nodes { ${nestedSelections} }
151
- totalCount
152
- pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
153
- }`);
154
- }
155
- else {
156
- // Direct relation (not a connection)
157
- fields.push(`${key}${argsStr} { ${nestedSelections} }`);
158
- }
159
- }
160
- }
161
- }
162
- return fields.join('\n ');
163
- }
164
- /**
165
- * Build a findMany query document
166
- */
167
- export function buildFindManyDocument(operationName, queryField, select, args, filterTypeName, orderByTypeName) {
168
- const selections = select ? buildSelections(select) : 'id';
169
- // Build variable definitions and query arguments
170
- const varDefs = [];
171
- const queryArgs = [];
172
- const variables = {};
173
- if (args.where) {
174
- varDefs.push(`$where: ${filterTypeName}`);
175
- queryArgs.push('filter: $where');
176
- variables.where = args.where;
177
- }
178
- if (args.orderBy && args.orderBy.length > 0) {
179
- varDefs.push(`$orderBy: [${orderByTypeName}!]`);
180
- queryArgs.push('orderBy: $orderBy');
181
- variables.orderBy = args.orderBy;
182
- }
183
- if (args.first !== undefined) {
184
- varDefs.push('$first: Int');
185
- queryArgs.push('first: $first');
186
- variables.first = args.first;
187
- }
188
- if (args.last !== undefined) {
189
- varDefs.push('$last: Int');
190
- queryArgs.push('last: $last');
191
- variables.last = args.last;
192
- }
193
- if (args.after) {
194
- varDefs.push('$after: Cursor');
195
- queryArgs.push('after: $after');
196
- variables.after = args.after;
197
- }
198
- if (args.before) {
199
- varDefs.push('$before: Cursor');
200
- queryArgs.push('before: $before');
201
- variables.before = args.before;
202
- }
203
- if (args.offset !== undefined) {
204
- varDefs.push('$offset: Int');
205
- queryArgs.push('offset: $offset');
206
- variables.offset = args.offset;
207
- }
208
- const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(', ')})` : '';
209
- const queryArgsStr = queryArgs.length > 0 ? `(${queryArgs.join(', ')})` : '';
210
- const document = `query ${operationName}Query${varDefsStr} {
211
- ${queryField}${queryArgsStr} {
212
- nodes {
213
- ${selections}
214
- }
215
- totalCount
216
- pageInfo {
217
- hasNextPage
218
- hasPreviousPage
219
- startCursor
220
- endCursor
221
- }
222
- }
223
- }`;
224
- return { document, variables };
225
- }
226
- /**
227
- * Build a findFirst query document
228
- */
229
- export function buildFindFirstDocument(operationName, queryField, select, args, filterTypeName) {
230
- const selections = select ? buildSelections(select) : 'id';
231
- const varDefs = [];
232
- const queryArgs = [];
233
- const variables = {};
234
- if (args.where) {
235
- varDefs.push(`$where: ${filterTypeName}`);
236
- queryArgs.push('filter: $where');
237
- variables.where = args.where;
238
- }
239
- // findFirst uses the list query with first: 1
240
- varDefs.push('$first: Int');
241
- queryArgs.push('first: $first');
242
- variables.first = 1;
243
- const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(', ')})` : '';
244
- const queryArgsStr = queryArgs.length > 0 ? `(${queryArgs.join(', ')})` : '';
245
- const document = `query ${operationName}Query${varDefsStr} {
246
- ${queryField}${queryArgsStr} {
247
- nodes {
248
- ${selections}
249
- }
250
- }
251
- }`;
252
- return { document, variables };
253
- }
254
- /**
255
- * Build a create mutation document
256
- */
257
- export function buildCreateDocument(operationName, mutationField, entityField, select, data, inputTypeName) {
258
- const selections = select ? buildSelections(select) : 'id';
259
- const document = `mutation ${operationName}Mutation($input: ${inputTypeName}!) {
260
- ${mutationField}(input: $input) {
261
- ${entityField} {
262
- ${selections}
263
- }
264
- }
265
- }`;
266
- return {
267
- document,
268
- variables: {
269
- input: {
270
- [entityField]: data,
271
- },
272
- },
273
- };
274
- }
275
- /**
276
- * Build an update mutation document
277
- */
278
- export function buildUpdateDocument(operationName, mutationField, entityField, select, where, data, inputTypeName) {
279
- const selections = select ? buildSelections(select) : 'id';
280
- // PostGraphile update uses nodeId or primary key in input
281
- const document = `mutation ${operationName}Mutation($input: ${inputTypeName}!) {
282
- ${mutationField}(input: $input) {
283
- ${entityField} {
284
- ${selections}
285
- }
286
- }
287
- }`;
288
- return {
289
- document,
290
- variables: {
291
- input: {
292
- id: where.id, // Assumes id-based where clause
293
- patch: data,
294
- },
295
- },
296
- };
297
- }
298
- /**
299
- * Build a delete mutation document
300
- */
301
- export function buildDeleteDocument(operationName, mutationField, entityField, where, inputTypeName) {
302
- const document = `mutation ${operationName}Mutation($input: ${inputTypeName}!) {
303
- ${mutationField}(input: $input) {
304
- ${entityField} {
305
- id
306
- }
307
- }
308
- }`;
309
- return {
310
- document,
311
- variables: {
312
- input: {
313
- id: where.id,
314
- },
315
- },
316
- };
317
- }
318
- /**
319
- * Build a custom query document
320
- */
321
- export function buildCustomQueryDocument(operationName, queryField, select, args, variableDefinitions) {
322
- const selections = select ? buildSelections(select) : '';
323
- const varDefs = variableDefinitions.map((v) => `$${v.name}: ${v.type}`);
324
- const queryArgs = variableDefinitions.map((v) => `${v.name}: $${v.name}`);
325
- const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(', ')})` : '';
326
- const queryArgsStr = queryArgs.length > 0 ? `(${queryArgs.join(', ')})` : '';
327
- const selectionsBlock = selections ? ` {\n ${selections}\n }` : '';
328
- const document = `query ${operationName}Query${varDefsStr} {
329
- ${queryField}${queryArgsStr}${selectionsBlock}
330
- }`;
331
- return {
332
- document,
333
- variables: args ?? {},
334
- };
335
- }
336
- /**
337
- * Build a custom mutation document
338
- */
339
- export function buildCustomMutationDocument(operationName, mutationField, select, args, variableDefinitions) {
340
- const selections = select ? buildSelections(select) : '';
341
- const varDefs = variableDefinitions.map((v) => `$${v.name}: ${v.type}`);
342
- const mutationArgs = variableDefinitions.map((v) => `${v.name}: $${v.name}`);
343
- const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(', ')})` : '';
344
- const mutationArgsStr = mutationArgs.length > 0 ? `(${mutationArgs.join(', ')})` : '';
345
- const selectionsBlock = selections ? ` {\n ${selections}\n }` : '';
346
- const document = `mutation ${operationName}Mutation${varDefsStr} {
347
- ${mutationField}${mutationArgsStr}${selectionsBlock}
348
- }`;
349
- return {
350
- document,
351
- variables: args ?? {},
352
- };
353
- }