@constructive-io/graphql-codegen 2.28.4 → 2.29.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.
- package/cli/codegen/orm/client-generator.js +108 -31
- package/cli/codegen/orm/client.d.ts +36 -10
- package/cli/codegen/orm/client.js +46 -17
- package/esm/cli/codegen/orm/client-generator.js +108 -31
- package/esm/cli/codegen/orm/client.d.ts +36 -10
- package/esm/cli/codegen/orm/client.js +43 -15
- package/package.json +2 -2
|
@@ -55,11 +55,6 @@ function generateOrmClientFile() {
|
|
|
55
55
|
* DO NOT EDIT - changes will be overwritten
|
|
56
56
|
*/
|
|
57
57
|
|
|
58
|
-
export interface OrmClientConfig {
|
|
59
|
-
endpoint: string;
|
|
60
|
-
headers?: Record<string, string>;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
58
|
export interface GraphQLError {
|
|
64
59
|
message: string;
|
|
65
60
|
locations?: { line: number; column: number }[];
|
|
@@ -67,20 +62,6 @@ export interface GraphQLError {
|
|
|
67
62
|
extensions?: Record<string, unknown>;
|
|
68
63
|
}
|
|
69
64
|
|
|
70
|
-
/**
|
|
71
|
-
* Error thrown when GraphQL request fails
|
|
72
|
-
*/
|
|
73
|
-
export class GraphQLRequestError extends Error {
|
|
74
|
-
constructor(
|
|
75
|
-
public readonly errors: GraphQLError[],
|
|
76
|
-
public readonly data: unknown = null
|
|
77
|
-
) {
|
|
78
|
-
const messages = errors.map(e => e.message).join('; ');
|
|
79
|
-
super(\`GraphQL Error: \${messages}\`);
|
|
80
|
-
this.name = 'GraphQLRequestError';
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
65
|
/**
|
|
85
66
|
* Discriminated union for query results
|
|
86
67
|
* Use .ok to check success, or use .unwrap() to get data or throw
|
|
@@ -90,21 +71,36 @@ export type QueryResult<T> =
|
|
|
90
71
|
| { ok: false; data: null; errors: GraphQLError[] };
|
|
91
72
|
|
|
92
73
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
74
|
+
* Pluggable adapter interface for GraphQL execution.
|
|
75
|
+
* Implement this interface to provide custom execution logic (e.g., for testing).
|
|
95
76
|
*/
|
|
96
|
-
export interface
|
|
97
|
-
|
|
98
|
-
|
|
77
|
+
export interface GraphQLAdapter {
|
|
78
|
+
/**
|
|
79
|
+
* Execute a GraphQL operation and return the result.
|
|
80
|
+
*/
|
|
81
|
+
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
82
|
+
/**
|
|
83
|
+
* Set headers for requests (optional - only implement if adapter supports headers).
|
|
84
|
+
*/
|
|
85
|
+
setHeaders?(headers: Record<string, string>): void;
|
|
86
|
+
/**
|
|
87
|
+
* Get the endpoint URL (optional - only implement if adapter has an endpoint).
|
|
88
|
+
*/
|
|
89
|
+
getEndpoint?(): string;
|
|
99
90
|
}
|
|
100
91
|
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
94
|
+
* This is used when no custom adapter is provided.
|
|
95
|
+
*/
|
|
96
|
+
export class FetchAdapter implements GraphQLAdapter {
|
|
103
97
|
private headers: Record<string, string>;
|
|
104
98
|
|
|
105
|
-
constructor(
|
|
106
|
-
|
|
107
|
-
|
|
99
|
+
constructor(
|
|
100
|
+
private endpoint: string,
|
|
101
|
+
headers?: Record<string, string>
|
|
102
|
+
) {
|
|
103
|
+
this.headers = headers ?? {};
|
|
108
104
|
}
|
|
109
105
|
|
|
110
106
|
async execute<T>(
|
|
@@ -137,7 +133,6 @@ export class OrmClient {
|
|
|
137
133
|
errors?: GraphQLError[];
|
|
138
134
|
};
|
|
139
135
|
|
|
140
|
-
// Return discriminated union based on presence of errors
|
|
141
136
|
if (json.errors && json.errors.length > 0) {
|
|
142
137
|
return {
|
|
143
138
|
ok: false,
|
|
@@ -161,6 +156,73 @@ export class OrmClient {
|
|
|
161
156
|
return this.endpoint;
|
|
162
157
|
}
|
|
163
158
|
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Configuration for creating an ORM client.
|
|
162
|
+
* Either provide endpoint (and optional headers) for HTTP requests,
|
|
163
|
+
* or provide a custom adapter for alternative execution strategies.
|
|
164
|
+
*/
|
|
165
|
+
export interface OrmClientConfig {
|
|
166
|
+
/** GraphQL endpoint URL (required if adapter not provided) */
|
|
167
|
+
endpoint?: string;
|
|
168
|
+
/** Default headers for HTTP requests (only used with endpoint) */
|
|
169
|
+
headers?: Record<string, string>;
|
|
170
|
+
/** Custom adapter for GraphQL execution (overrides endpoint/headers) */
|
|
171
|
+
adapter?: GraphQLAdapter;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Error thrown when GraphQL request fails
|
|
176
|
+
*/
|
|
177
|
+
export class GraphQLRequestError extends Error {
|
|
178
|
+
constructor(
|
|
179
|
+
public readonly errors: GraphQLError[],
|
|
180
|
+
public readonly data: unknown = null
|
|
181
|
+
) {
|
|
182
|
+
const messages = errors.map(e => e.message).join('; ');
|
|
183
|
+
super(\`GraphQL Error: \${messages}\`);
|
|
184
|
+
this.name = 'GraphQLRequestError';
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export class OrmClient {
|
|
189
|
+
private adapter: GraphQLAdapter;
|
|
190
|
+
|
|
191
|
+
constructor(config: OrmClientConfig) {
|
|
192
|
+
if (config.adapter) {
|
|
193
|
+
this.adapter = config.adapter;
|
|
194
|
+
} else if (config.endpoint) {
|
|
195
|
+
this.adapter = new FetchAdapter(config.endpoint, config.headers);
|
|
196
|
+
} else {
|
|
197
|
+
throw new Error('OrmClientConfig requires either an endpoint or a custom adapter');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async execute<T>(
|
|
202
|
+
document: string,
|
|
203
|
+
variables?: Record<string, unknown>
|
|
204
|
+
): Promise<QueryResult<T>> {
|
|
205
|
+
return this.adapter.execute<T>(document, variables);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Set headers for requests.
|
|
210
|
+
* Only works if the adapter supports headers.
|
|
211
|
+
*/
|
|
212
|
+
setHeaders(headers: Record<string, string>): void {
|
|
213
|
+
if (this.adapter.setHeaders) {
|
|
214
|
+
this.adapter.setHeaders(headers);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Get the endpoint URL.
|
|
220
|
+
* Returns empty string if the adapter doesn't have an endpoint.
|
|
221
|
+
*/
|
|
222
|
+
getEndpoint(): string {
|
|
223
|
+
return this.adapter.getEndpoint?.() ?? '';
|
|
224
|
+
}
|
|
225
|
+
}
|
|
164
226
|
`;
|
|
165
227
|
return {
|
|
166
228
|
fileName: 'client.ts',
|
|
@@ -359,11 +421,12 @@ function generateCreateClientFile(tables, hasCustomQueries, hasCustomMutations)
|
|
|
359
421
|
statements.push(createImportDeclaration('./mutation', ['createMutationOperations']));
|
|
360
422
|
}
|
|
361
423
|
// Re-export types and classes
|
|
362
|
-
// export type { OrmClientConfig, QueryResult, GraphQLError } from './client';
|
|
424
|
+
// export type { OrmClientConfig, QueryResult, GraphQLError, GraphQLAdapter } from './client';
|
|
363
425
|
const typeExportDecl = t.exportNamedDeclaration(null, [
|
|
364
426
|
t.exportSpecifier(t.identifier('OrmClientConfig'), t.identifier('OrmClientConfig')),
|
|
365
427
|
t.exportSpecifier(t.identifier('QueryResult'), t.identifier('QueryResult')),
|
|
366
428
|
t.exportSpecifier(t.identifier('GraphQLError'), t.identifier('GraphQLError')),
|
|
429
|
+
t.exportSpecifier(t.identifier('GraphQLAdapter'), t.identifier('GraphQLAdapter')),
|
|
367
430
|
], t.stringLiteral('./client'));
|
|
368
431
|
typeExportDecl.exportKind = 'type';
|
|
369
432
|
statements.push(typeExportDecl);
|
|
@@ -377,6 +440,20 @@ function generateCreateClientFile(tables, hasCustomQueries, hasCustomMutations)
|
|
|
377
440
|
], t.stringLiteral('./query-builder')));
|
|
378
441
|
// export * from './select-types';
|
|
379
442
|
statements.push(t.exportAllDeclaration(t.stringLiteral('./select-types')));
|
|
443
|
+
// Re-export all models for backwards compatibility
|
|
444
|
+
// export * from './models';
|
|
445
|
+
statements.push(t.exportAllDeclaration(t.stringLiteral('./models')));
|
|
446
|
+
// Re-export custom operations for backwards compatibility
|
|
447
|
+
if (hasCustomQueries) {
|
|
448
|
+
statements.push(t.exportNamedDeclaration(null, [
|
|
449
|
+
t.exportSpecifier(t.identifier('createQueryOperations'), t.identifier('createQueryOperations')),
|
|
450
|
+
], t.stringLiteral('./query')));
|
|
451
|
+
}
|
|
452
|
+
if (hasCustomMutations) {
|
|
453
|
+
statements.push(t.exportNamedDeclaration(null, [
|
|
454
|
+
t.exportSpecifier(t.identifier('createMutationOperations'), t.identifier('createMutationOperations')),
|
|
455
|
+
], t.stringLiteral('./mutation')));
|
|
456
|
+
}
|
|
380
457
|
// Build the return object properties
|
|
381
458
|
const returnProperties = [];
|
|
382
459
|
for (const table of tables) {
|
|
@@ -7,10 +7,6 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @internal This file is NOT part of the generated output
|
|
9
9
|
*/
|
|
10
|
-
export interface OrmClientConfig {
|
|
11
|
-
endpoint: string;
|
|
12
|
-
headers?: Record<string, string>;
|
|
13
|
-
}
|
|
14
10
|
export interface GraphQLError {
|
|
15
11
|
message: string;
|
|
16
12
|
locations?: {
|
|
@@ -20,11 +16,6 @@ export interface GraphQLError {
|
|
|
20
16
|
path?: (string | number)[];
|
|
21
17
|
extensions?: Record<string, unknown>;
|
|
22
18
|
}
|
|
23
|
-
export declare class GraphQLRequestError extends Error {
|
|
24
|
-
readonly errors: GraphQLError[];
|
|
25
|
-
readonly data: unknown;
|
|
26
|
-
constructor(errors: GraphQLError[], data?: unknown);
|
|
27
|
-
}
|
|
28
19
|
export type QueryResult<T> = {
|
|
29
20
|
ok: true;
|
|
30
21
|
data: T;
|
|
@@ -34,9 +25,44 @@ export type QueryResult<T> = {
|
|
|
34
25
|
data: null;
|
|
35
26
|
errors: GraphQLError[];
|
|
36
27
|
};
|
|
37
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Pluggable adapter interface for GraphQL execution.
|
|
30
|
+
* Implement this interface to provide custom execution logic (e.g., for testing).
|
|
31
|
+
*/
|
|
32
|
+
export interface GraphQLAdapter {
|
|
33
|
+
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
34
|
+
setHeaders?(headers: Record<string, string>): void;
|
|
35
|
+
getEndpoint?(): string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
39
|
+
*/
|
|
40
|
+
export declare class FetchAdapter implements GraphQLAdapter {
|
|
38
41
|
private endpoint;
|
|
39
42
|
private headers;
|
|
43
|
+
constructor(endpoint: string, headers?: Record<string, string>);
|
|
44
|
+
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
45
|
+
setHeaders(headers: Record<string, string>): void;
|
|
46
|
+
getEndpoint(): string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Configuration for creating an ORM client.
|
|
50
|
+
*/
|
|
51
|
+
export interface OrmClientConfig {
|
|
52
|
+
/** GraphQL endpoint URL (required if adapter not provided) */
|
|
53
|
+
endpoint?: string;
|
|
54
|
+
/** Default headers for HTTP requests (only used with endpoint) */
|
|
55
|
+
headers?: Record<string, string>;
|
|
56
|
+
/** Custom adapter for GraphQL execution (overrides endpoint/headers) */
|
|
57
|
+
adapter?: GraphQLAdapter;
|
|
58
|
+
}
|
|
59
|
+
export declare class GraphQLRequestError extends Error {
|
|
60
|
+
readonly errors: GraphQLError[];
|
|
61
|
+
readonly data: unknown;
|
|
62
|
+
constructor(errors: GraphQLError[], data?: unknown);
|
|
63
|
+
}
|
|
64
|
+
export declare class OrmClient {
|
|
65
|
+
private adapter;
|
|
40
66
|
constructor(config: OrmClientConfig);
|
|
41
67
|
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
42
68
|
setHeaders(headers: Record<string, string>): void;
|
|
@@ -9,25 +9,16 @@
|
|
|
9
9
|
* @internal This file is NOT part of the generated output
|
|
10
10
|
*/
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.OrmClient = exports.GraphQLRequestError = void 0;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const messages = errors.map((e) => e.message).join('; ');
|
|
18
|
-
super(`GraphQL Error: ${messages}`);
|
|
19
|
-
this.errors = errors;
|
|
20
|
-
this.data = data;
|
|
21
|
-
this.name = 'GraphQLRequestError';
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
exports.GraphQLRequestError = GraphQLRequestError;
|
|
25
|
-
class OrmClient {
|
|
12
|
+
exports.OrmClient = exports.GraphQLRequestError = exports.FetchAdapter = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
15
|
+
*/
|
|
16
|
+
class FetchAdapter {
|
|
26
17
|
endpoint;
|
|
27
18
|
headers;
|
|
28
|
-
constructor(
|
|
29
|
-
this.endpoint =
|
|
30
|
-
this.headers =
|
|
19
|
+
constructor(endpoint, headers) {
|
|
20
|
+
this.endpoint = endpoint;
|
|
21
|
+
this.headers = headers ?? {};
|
|
31
22
|
}
|
|
32
23
|
async execute(document, variables) {
|
|
33
24
|
const response = await fetch(this.endpoint, {
|
|
@@ -70,4 +61,42 @@ class OrmClient {
|
|
|
70
61
|
return this.endpoint;
|
|
71
62
|
}
|
|
72
63
|
}
|
|
64
|
+
exports.FetchAdapter = FetchAdapter;
|
|
65
|
+
class GraphQLRequestError extends Error {
|
|
66
|
+
errors;
|
|
67
|
+
data;
|
|
68
|
+
constructor(errors, data = null) {
|
|
69
|
+
const messages = errors.map((e) => e.message).join('; ');
|
|
70
|
+
super(`GraphQL Error: ${messages}`);
|
|
71
|
+
this.errors = errors;
|
|
72
|
+
this.data = data;
|
|
73
|
+
this.name = 'GraphQLRequestError';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.GraphQLRequestError = GraphQLRequestError;
|
|
77
|
+
class OrmClient {
|
|
78
|
+
adapter;
|
|
79
|
+
constructor(config) {
|
|
80
|
+
if (config.adapter) {
|
|
81
|
+
this.adapter = config.adapter;
|
|
82
|
+
}
|
|
83
|
+
else if (config.endpoint) {
|
|
84
|
+
this.adapter = new FetchAdapter(config.endpoint, config.headers);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
throw new Error('OrmClientConfig requires either an endpoint or a custom adapter');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async execute(document, variables) {
|
|
91
|
+
return this.adapter.execute(document, variables);
|
|
92
|
+
}
|
|
93
|
+
setHeaders(headers) {
|
|
94
|
+
if (this.adapter.setHeaders) {
|
|
95
|
+
this.adapter.setHeaders(headers);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
getEndpoint() {
|
|
99
|
+
return this.adapter.getEndpoint?.() ?? '';
|
|
100
|
+
}
|
|
101
|
+
}
|
|
73
102
|
exports.OrmClient = OrmClient;
|
|
@@ -16,11 +16,6 @@ export function generateOrmClientFile() {
|
|
|
16
16
|
* DO NOT EDIT - changes will be overwritten
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
export interface OrmClientConfig {
|
|
20
|
-
endpoint: string;
|
|
21
|
-
headers?: Record<string, string>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
19
|
export interface GraphQLError {
|
|
25
20
|
message: string;
|
|
26
21
|
locations?: { line: number; column: number }[];
|
|
@@ -28,20 +23,6 @@ export interface GraphQLError {
|
|
|
28
23
|
extensions?: Record<string, unknown>;
|
|
29
24
|
}
|
|
30
25
|
|
|
31
|
-
/**
|
|
32
|
-
* Error thrown when GraphQL request fails
|
|
33
|
-
*/
|
|
34
|
-
export class GraphQLRequestError extends Error {
|
|
35
|
-
constructor(
|
|
36
|
-
public readonly errors: GraphQLError[],
|
|
37
|
-
public readonly data: unknown = null
|
|
38
|
-
) {
|
|
39
|
-
const messages = errors.map(e => e.message).join('; ');
|
|
40
|
-
super(\`GraphQL Error: \${messages}\`);
|
|
41
|
-
this.name = 'GraphQLRequestError';
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
26
|
/**
|
|
46
27
|
* Discriminated union for query results
|
|
47
28
|
* Use .ok to check success, or use .unwrap() to get data or throw
|
|
@@ -51,21 +32,36 @@ export type QueryResult<T> =
|
|
|
51
32
|
| { ok: false; data: null; errors: GraphQLError[] };
|
|
52
33
|
|
|
53
34
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
35
|
+
* Pluggable adapter interface for GraphQL execution.
|
|
36
|
+
* Implement this interface to provide custom execution logic (e.g., for testing).
|
|
56
37
|
*/
|
|
57
|
-
export interface
|
|
58
|
-
|
|
59
|
-
|
|
38
|
+
export interface GraphQLAdapter {
|
|
39
|
+
/**
|
|
40
|
+
* Execute a GraphQL operation and return the result.
|
|
41
|
+
*/
|
|
42
|
+
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
43
|
+
/**
|
|
44
|
+
* Set headers for requests (optional - only implement if adapter supports headers).
|
|
45
|
+
*/
|
|
46
|
+
setHeaders?(headers: Record<string, string>): void;
|
|
47
|
+
/**
|
|
48
|
+
* Get the endpoint URL (optional - only implement if adapter has an endpoint).
|
|
49
|
+
*/
|
|
50
|
+
getEndpoint?(): string;
|
|
60
51
|
}
|
|
61
52
|
|
|
62
|
-
|
|
63
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
55
|
+
* This is used when no custom adapter is provided.
|
|
56
|
+
*/
|
|
57
|
+
export class FetchAdapter implements GraphQLAdapter {
|
|
64
58
|
private headers: Record<string, string>;
|
|
65
59
|
|
|
66
|
-
constructor(
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
constructor(
|
|
61
|
+
private endpoint: string,
|
|
62
|
+
headers?: Record<string, string>
|
|
63
|
+
) {
|
|
64
|
+
this.headers = headers ?? {};
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
async execute<T>(
|
|
@@ -98,7 +94,6 @@ export class OrmClient {
|
|
|
98
94
|
errors?: GraphQLError[];
|
|
99
95
|
};
|
|
100
96
|
|
|
101
|
-
// Return discriminated union based on presence of errors
|
|
102
97
|
if (json.errors && json.errors.length > 0) {
|
|
103
98
|
return {
|
|
104
99
|
ok: false,
|
|
@@ -122,6 +117,73 @@ export class OrmClient {
|
|
|
122
117
|
return this.endpoint;
|
|
123
118
|
}
|
|
124
119
|
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Configuration for creating an ORM client.
|
|
123
|
+
* Either provide endpoint (and optional headers) for HTTP requests,
|
|
124
|
+
* or provide a custom adapter for alternative execution strategies.
|
|
125
|
+
*/
|
|
126
|
+
export interface OrmClientConfig {
|
|
127
|
+
/** GraphQL endpoint URL (required if adapter not provided) */
|
|
128
|
+
endpoint?: string;
|
|
129
|
+
/** Default headers for HTTP requests (only used with endpoint) */
|
|
130
|
+
headers?: Record<string, string>;
|
|
131
|
+
/** Custom adapter for GraphQL execution (overrides endpoint/headers) */
|
|
132
|
+
adapter?: GraphQLAdapter;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Error thrown when GraphQL request fails
|
|
137
|
+
*/
|
|
138
|
+
export class GraphQLRequestError extends Error {
|
|
139
|
+
constructor(
|
|
140
|
+
public readonly errors: GraphQLError[],
|
|
141
|
+
public readonly data: unknown = null
|
|
142
|
+
) {
|
|
143
|
+
const messages = errors.map(e => e.message).join('; ');
|
|
144
|
+
super(\`GraphQL Error: \${messages}\`);
|
|
145
|
+
this.name = 'GraphQLRequestError';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export class OrmClient {
|
|
150
|
+
private adapter: GraphQLAdapter;
|
|
151
|
+
|
|
152
|
+
constructor(config: OrmClientConfig) {
|
|
153
|
+
if (config.adapter) {
|
|
154
|
+
this.adapter = config.adapter;
|
|
155
|
+
} else if (config.endpoint) {
|
|
156
|
+
this.adapter = new FetchAdapter(config.endpoint, config.headers);
|
|
157
|
+
} else {
|
|
158
|
+
throw new Error('OrmClientConfig requires either an endpoint or a custom adapter');
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async execute<T>(
|
|
163
|
+
document: string,
|
|
164
|
+
variables?: Record<string, unknown>
|
|
165
|
+
): Promise<QueryResult<T>> {
|
|
166
|
+
return this.adapter.execute<T>(document, variables);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Set headers for requests.
|
|
171
|
+
* Only works if the adapter supports headers.
|
|
172
|
+
*/
|
|
173
|
+
setHeaders(headers: Record<string, string>): void {
|
|
174
|
+
if (this.adapter.setHeaders) {
|
|
175
|
+
this.adapter.setHeaders(headers);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Get the endpoint URL.
|
|
181
|
+
* Returns empty string if the adapter doesn't have an endpoint.
|
|
182
|
+
*/
|
|
183
|
+
getEndpoint(): string {
|
|
184
|
+
return this.adapter.getEndpoint?.() ?? '';
|
|
185
|
+
}
|
|
186
|
+
}
|
|
125
187
|
`;
|
|
126
188
|
return {
|
|
127
189
|
fileName: 'client.ts',
|
|
@@ -320,11 +382,12 @@ export function generateCreateClientFile(tables, hasCustomQueries, hasCustomMuta
|
|
|
320
382
|
statements.push(createImportDeclaration('./mutation', ['createMutationOperations']));
|
|
321
383
|
}
|
|
322
384
|
// Re-export types and classes
|
|
323
|
-
// export type { OrmClientConfig, QueryResult, GraphQLError } from './client';
|
|
385
|
+
// export type { OrmClientConfig, QueryResult, GraphQLError, GraphQLAdapter } from './client';
|
|
324
386
|
const typeExportDecl = t.exportNamedDeclaration(null, [
|
|
325
387
|
t.exportSpecifier(t.identifier('OrmClientConfig'), t.identifier('OrmClientConfig')),
|
|
326
388
|
t.exportSpecifier(t.identifier('QueryResult'), t.identifier('QueryResult')),
|
|
327
389
|
t.exportSpecifier(t.identifier('GraphQLError'), t.identifier('GraphQLError')),
|
|
390
|
+
t.exportSpecifier(t.identifier('GraphQLAdapter'), t.identifier('GraphQLAdapter')),
|
|
328
391
|
], t.stringLiteral('./client'));
|
|
329
392
|
typeExportDecl.exportKind = 'type';
|
|
330
393
|
statements.push(typeExportDecl);
|
|
@@ -338,6 +401,20 @@ export function generateCreateClientFile(tables, hasCustomQueries, hasCustomMuta
|
|
|
338
401
|
], t.stringLiteral('./query-builder')));
|
|
339
402
|
// export * from './select-types';
|
|
340
403
|
statements.push(t.exportAllDeclaration(t.stringLiteral('./select-types')));
|
|
404
|
+
// Re-export all models for backwards compatibility
|
|
405
|
+
// export * from './models';
|
|
406
|
+
statements.push(t.exportAllDeclaration(t.stringLiteral('./models')));
|
|
407
|
+
// Re-export custom operations for backwards compatibility
|
|
408
|
+
if (hasCustomQueries) {
|
|
409
|
+
statements.push(t.exportNamedDeclaration(null, [
|
|
410
|
+
t.exportSpecifier(t.identifier('createQueryOperations'), t.identifier('createQueryOperations')),
|
|
411
|
+
], t.stringLiteral('./query')));
|
|
412
|
+
}
|
|
413
|
+
if (hasCustomMutations) {
|
|
414
|
+
statements.push(t.exportNamedDeclaration(null, [
|
|
415
|
+
t.exportSpecifier(t.identifier('createMutationOperations'), t.identifier('createMutationOperations')),
|
|
416
|
+
], t.stringLiteral('./mutation')));
|
|
417
|
+
}
|
|
341
418
|
// Build the return object properties
|
|
342
419
|
const returnProperties = [];
|
|
343
420
|
for (const table of tables) {
|
|
@@ -7,10 +7,6 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @internal This file is NOT part of the generated output
|
|
9
9
|
*/
|
|
10
|
-
export interface OrmClientConfig {
|
|
11
|
-
endpoint: string;
|
|
12
|
-
headers?: Record<string, string>;
|
|
13
|
-
}
|
|
14
10
|
export interface GraphQLError {
|
|
15
11
|
message: string;
|
|
16
12
|
locations?: {
|
|
@@ -20,11 +16,6 @@ export interface GraphQLError {
|
|
|
20
16
|
path?: (string | number)[];
|
|
21
17
|
extensions?: Record<string, unknown>;
|
|
22
18
|
}
|
|
23
|
-
export declare class GraphQLRequestError extends Error {
|
|
24
|
-
readonly errors: GraphQLError[];
|
|
25
|
-
readonly data: unknown;
|
|
26
|
-
constructor(errors: GraphQLError[], data?: unknown);
|
|
27
|
-
}
|
|
28
19
|
export type QueryResult<T> = {
|
|
29
20
|
ok: true;
|
|
30
21
|
data: T;
|
|
@@ -34,9 +25,44 @@ export type QueryResult<T> = {
|
|
|
34
25
|
data: null;
|
|
35
26
|
errors: GraphQLError[];
|
|
36
27
|
};
|
|
37
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Pluggable adapter interface for GraphQL execution.
|
|
30
|
+
* Implement this interface to provide custom execution logic (e.g., for testing).
|
|
31
|
+
*/
|
|
32
|
+
export interface GraphQLAdapter {
|
|
33
|
+
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
34
|
+
setHeaders?(headers: Record<string, string>): void;
|
|
35
|
+
getEndpoint?(): string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
39
|
+
*/
|
|
40
|
+
export declare class FetchAdapter implements GraphQLAdapter {
|
|
38
41
|
private endpoint;
|
|
39
42
|
private headers;
|
|
43
|
+
constructor(endpoint: string, headers?: Record<string, string>);
|
|
44
|
+
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
45
|
+
setHeaders(headers: Record<string, string>): void;
|
|
46
|
+
getEndpoint(): string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Configuration for creating an ORM client.
|
|
50
|
+
*/
|
|
51
|
+
export interface OrmClientConfig {
|
|
52
|
+
/** GraphQL endpoint URL (required if adapter not provided) */
|
|
53
|
+
endpoint?: string;
|
|
54
|
+
/** Default headers for HTTP requests (only used with endpoint) */
|
|
55
|
+
headers?: Record<string, string>;
|
|
56
|
+
/** Custom adapter for GraphQL execution (overrides endpoint/headers) */
|
|
57
|
+
adapter?: GraphQLAdapter;
|
|
58
|
+
}
|
|
59
|
+
export declare class GraphQLRequestError extends Error {
|
|
60
|
+
readonly errors: GraphQLError[];
|
|
61
|
+
readonly data: unknown;
|
|
62
|
+
constructor(errors: GraphQLError[], data?: unknown);
|
|
63
|
+
}
|
|
64
|
+
export declare class OrmClient {
|
|
65
|
+
private adapter;
|
|
40
66
|
constructor(config: OrmClientConfig);
|
|
41
67
|
execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
|
|
42
68
|
setHeaders(headers: Record<string, string>): void;
|
|
@@ -7,23 +7,15 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @internal This file is NOT part of the generated output
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const messages = errors.map((e) => e.message).join('; ');
|
|
15
|
-
super(`GraphQL Error: ${messages}`);
|
|
16
|
-
this.errors = errors;
|
|
17
|
-
this.data = data;
|
|
18
|
-
this.name = 'GraphQLRequestError';
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
export class OrmClient {
|
|
10
|
+
/**
|
|
11
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
12
|
+
*/
|
|
13
|
+
export class FetchAdapter {
|
|
22
14
|
endpoint;
|
|
23
15
|
headers;
|
|
24
|
-
constructor(
|
|
25
|
-
this.endpoint =
|
|
26
|
-
this.headers =
|
|
16
|
+
constructor(endpoint, headers) {
|
|
17
|
+
this.endpoint = endpoint;
|
|
18
|
+
this.headers = headers ?? {};
|
|
27
19
|
}
|
|
28
20
|
async execute(document, variables) {
|
|
29
21
|
const response = await fetch(this.endpoint, {
|
|
@@ -66,3 +58,39 @@ export class OrmClient {
|
|
|
66
58
|
return this.endpoint;
|
|
67
59
|
}
|
|
68
60
|
}
|
|
61
|
+
export class GraphQLRequestError extends Error {
|
|
62
|
+
errors;
|
|
63
|
+
data;
|
|
64
|
+
constructor(errors, data = null) {
|
|
65
|
+
const messages = errors.map((e) => e.message).join('; ');
|
|
66
|
+
super(`GraphQL Error: ${messages}`);
|
|
67
|
+
this.errors = errors;
|
|
68
|
+
this.data = data;
|
|
69
|
+
this.name = 'GraphQLRequestError';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export class OrmClient {
|
|
73
|
+
adapter;
|
|
74
|
+
constructor(config) {
|
|
75
|
+
if (config.adapter) {
|
|
76
|
+
this.adapter = config.adapter;
|
|
77
|
+
}
|
|
78
|
+
else if (config.endpoint) {
|
|
79
|
+
this.adapter = new FetchAdapter(config.endpoint, config.headers);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
throw new Error('OrmClientConfig requires either an endpoint or a custom adapter');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async execute(document, variables) {
|
|
86
|
+
return this.adapter.execute(document, variables);
|
|
87
|
+
}
|
|
88
|
+
setHeaders(headers) {
|
|
89
|
+
if (this.adapter.setHeaders) {
|
|
90
|
+
this.adapter.setHeaders(headers);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
getEndpoint() {
|
|
94
|
+
return this.adapter.getEndpoint?.() ?? '';
|
|
95
|
+
}
|
|
96
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructive-io/graphql-codegen",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.29.0",
|
|
4
4
|
"description": "CLI-based GraphQL SDK generator for PostGraphile endpoints with React Query hooks",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graphql",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"tsx": "^4.21.0",
|
|
90
90
|
"typescript": "^5.9.3"
|
|
91
91
|
},
|
|
92
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "17c82525efb87a39529a02bf24c096bf75597fae"
|
|
93
93
|
}
|