@constructive-io/graphql-codegen 2.28.5 → 2.30.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 +79 -46
- 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 +79 -46
- package/esm/cli/codegen/orm/client.d.ts +36 -10
- package/esm/cli/codegen/orm/client.js +43 -15
- package/package.json +3 -2
|
@@ -55,56 +55,22 @@ function generateOrmClientFile() {
|
|
|
55
55
|
* DO NOT EDIT - changes will be overwritten
|
|
56
56
|
*/
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
endpoint: string;
|
|
60
|
-
headers?: Record<string, string>;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface GraphQLError {
|
|
64
|
-
message: string;
|
|
65
|
-
locations?: { line: number; column: number }[];
|
|
66
|
-
path?: (string | number)[];
|
|
67
|
-
extensions?: Record<string, unknown>;
|
|
68
|
-
}
|
|
69
|
-
|
|
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
|
-
}
|
|
58
|
+
import type { GraphQLAdapter, GraphQLError, QueryResult } from '@constructive-io/graphql-types';
|
|
83
59
|
|
|
84
|
-
|
|
85
|
-
* Discriminated union for query results
|
|
86
|
-
* Use .ok to check success, or use .unwrap() to get data or throw
|
|
87
|
-
*/
|
|
88
|
-
export type QueryResult<T> =
|
|
89
|
-
| { ok: true; data: T; errors: undefined }
|
|
90
|
-
| { ok: false; data: null; errors: GraphQLError[] };
|
|
60
|
+
export type { GraphQLAdapter, GraphQLError, QueryResult } from '@constructive-io/graphql-types';
|
|
91
61
|
|
|
92
62
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
63
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
64
|
+
* This is used when no custom adapter is provided.
|
|
95
65
|
*/
|
|
96
|
-
export
|
|
97
|
-
data: T | null;
|
|
98
|
-
errors?: GraphQLError[];
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export class OrmClient {
|
|
102
|
-
private endpoint: string;
|
|
66
|
+
export class FetchAdapter implements GraphQLAdapter {
|
|
103
67
|
private headers: Record<string, string>;
|
|
104
68
|
|
|
105
|
-
constructor(
|
|
106
|
-
|
|
107
|
-
|
|
69
|
+
constructor(
|
|
70
|
+
private endpoint: string,
|
|
71
|
+
headers?: Record<string, string>
|
|
72
|
+
) {
|
|
73
|
+
this.headers = headers ?? {};
|
|
108
74
|
}
|
|
109
75
|
|
|
110
76
|
async execute<T>(
|
|
@@ -137,7 +103,6 @@ export class OrmClient {
|
|
|
137
103
|
errors?: GraphQLError[];
|
|
138
104
|
};
|
|
139
105
|
|
|
140
|
-
// Return discriminated union based on presence of errors
|
|
141
106
|
if (json.errors && json.errors.length > 0) {
|
|
142
107
|
return {
|
|
143
108
|
ok: false,
|
|
@@ -161,6 +126,73 @@ export class OrmClient {
|
|
|
161
126
|
return this.endpoint;
|
|
162
127
|
}
|
|
163
128
|
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Configuration for creating an ORM client.
|
|
132
|
+
* Either provide endpoint (and optional headers) for HTTP requests,
|
|
133
|
+
* or provide a custom adapter for alternative execution strategies.
|
|
134
|
+
*/
|
|
135
|
+
export interface OrmClientConfig {
|
|
136
|
+
/** GraphQL endpoint URL (required if adapter not provided) */
|
|
137
|
+
endpoint?: string;
|
|
138
|
+
/** Default headers for HTTP requests (only used with endpoint) */
|
|
139
|
+
headers?: Record<string, string>;
|
|
140
|
+
/** Custom adapter for GraphQL execution (overrides endpoint/headers) */
|
|
141
|
+
adapter?: GraphQLAdapter;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Error thrown when GraphQL request fails
|
|
146
|
+
*/
|
|
147
|
+
export class GraphQLRequestError extends Error {
|
|
148
|
+
constructor(
|
|
149
|
+
public readonly errors: GraphQLError[],
|
|
150
|
+
public readonly data: unknown = null
|
|
151
|
+
) {
|
|
152
|
+
const messages = errors.map(e => e.message).join('; ');
|
|
153
|
+
super(\`GraphQL Error: \${messages}\`);
|
|
154
|
+
this.name = 'GraphQLRequestError';
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export class OrmClient {
|
|
159
|
+
private adapter: GraphQLAdapter;
|
|
160
|
+
|
|
161
|
+
constructor(config: OrmClientConfig) {
|
|
162
|
+
if (config.adapter) {
|
|
163
|
+
this.adapter = config.adapter;
|
|
164
|
+
} else if (config.endpoint) {
|
|
165
|
+
this.adapter = new FetchAdapter(config.endpoint, config.headers);
|
|
166
|
+
} else {
|
|
167
|
+
throw new Error('OrmClientConfig requires either an endpoint or a custom adapter');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async execute<T>(
|
|
172
|
+
document: string,
|
|
173
|
+
variables?: Record<string, unknown>
|
|
174
|
+
): Promise<QueryResult<T>> {
|
|
175
|
+
return this.adapter.execute<T>(document, variables);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Set headers for requests.
|
|
180
|
+
* Only works if the adapter supports headers.
|
|
181
|
+
*/
|
|
182
|
+
setHeaders(headers: Record<string, string>): void {
|
|
183
|
+
if (this.adapter.setHeaders) {
|
|
184
|
+
this.adapter.setHeaders(headers);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Get the endpoint URL.
|
|
190
|
+
* Returns empty string if the adapter doesn't have an endpoint.
|
|
191
|
+
*/
|
|
192
|
+
getEndpoint(): string {
|
|
193
|
+
return this.adapter.getEndpoint?.() ?? '';
|
|
194
|
+
}
|
|
195
|
+
}
|
|
164
196
|
`;
|
|
165
197
|
return {
|
|
166
198
|
fileName: 'client.ts',
|
|
@@ -359,11 +391,12 @@ function generateCreateClientFile(tables, hasCustomQueries, hasCustomMutations)
|
|
|
359
391
|
statements.push(createImportDeclaration('./mutation', ['createMutationOperations']));
|
|
360
392
|
}
|
|
361
393
|
// Re-export types and classes
|
|
362
|
-
// export type { OrmClientConfig, QueryResult, GraphQLError } from './client';
|
|
394
|
+
// export type { OrmClientConfig, QueryResult, GraphQLError, GraphQLAdapter } from './client';
|
|
363
395
|
const typeExportDecl = t.exportNamedDeclaration(null, [
|
|
364
396
|
t.exportSpecifier(t.identifier('OrmClientConfig'), t.identifier('OrmClientConfig')),
|
|
365
397
|
t.exportSpecifier(t.identifier('QueryResult'), t.identifier('QueryResult')),
|
|
366
398
|
t.exportSpecifier(t.identifier('GraphQLError'), t.identifier('GraphQLError')),
|
|
399
|
+
t.exportSpecifier(t.identifier('GraphQLAdapter'), t.identifier('GraphQLAdapter')),
|
|
367
400
|
], t.stringLiteral('./client'));
|
|
368
401
|
typeExportDecl.exportKind = 'type';
|
|
369
402
|
statements.push(typeExportDecl);
|
|
@@ -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,56 +16,22 @@ export function generateOrmClientFile() {
|
|
|
16
16
|
* DO NOT EDIT - changes will be overwritten
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
endpoint: string;
|
|
21
|
-
headers?: Record<string, string>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface GraphQLError {
|
|
25
|
-
message: string;
|
|
26
|
-
locations?: { line: number; column: number }[];
|
|
27
|
-
path?: (string | number)[];
|
|
28
|
-
extensions?: Record<string, unknown>;
|
|
29
|
-
}
|
|
30
|
-
|
|
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
|
-
}
|
|
19
|
+
import type { GraphQLAdapter, GraphQLError, QueryResult } from '@constructive-io/graphql-types';
|
|
44
20
|
|
|
45
|
-
|
|
46
|
-
* Discriminated union for query results
|
|
47
|
-
* Use .ok to check success, or use .unwrap() to get data or throw
|
|
48
|
-
*/
|
|
49
|
-
export type QueryResult<T> =
|
|
50
|
-
| { ok: true; data: T; errors: undefined }
|
|
51
|
-
| { ok: false; data: null; errors: GraphQLError[] };
|
|
21
|
+
export type { GraphQLAdapter, GraphQLError, QueryResult } from '@constructive-io/graphql-types';
|
|
52
22
|
|
|
53
23
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
24
|
+
* Default adapter that uses fetch for HTTP requests.
|
|
25
|
+
* This is used when no custom adapter is provided.
|
|
56
26
|
*/
|
|
57
|
-
export
|
|
58
|
-
data: T | null;
|
|
59
|
-
errors?: GraphQLError[];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export class OrmClient {
|
|
63
|
-
private endpoint: string;
|
|
27
|
+
export class FetchAdapter implements GraphQLAdapter {
|
|
64
28
|
private headers: Record<string, string>;
|
|
65
29
|
|
|
66
|
-
constructor(
|
|
67
|
-
|
|
68
|
-
|
|
30
|
+
constructor(
|
|
31
|
+
private endpoint: string,
|
|
32
|
+
headers?: Record<string, string>
|
|
33
|
+
) {
|
|
34
|
+
this.headers = headers ?? {};
|
|
69
35
|
}
|
|
70
36
|
|
|
71
37
|
async execute<T>(
|
|
@@ -98,7 +64,6 @@ export class OrmClient {
|
|
|
98
64
|
errors?: GraphQLError[];
|
|
99
65
|
};
|
|
100
66
|
|
|
101
|
-
// Return discriminated union based on presence of errors
|
|
102
67
|
if (json.errors && json.errors.length > 0) {
|
|
103
68
|
return {
|
|
104
69
|
ok: false,
|
|
@@ -122,6 +87,73 @@ export class OrmClient {
|
|
|
122
87
|
return this.endpoint;
|
|
123
88
|
}
|
|
124
89
|
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Configuration for creating an ORM client.
|
|
93
|
+
* Either provide endpoint (and optional headers) for HTTP requests,
|
|
94
|
+
* or provide a custom adapter for alternative execution strategies.
|
|
95
|
+
*/
|
|
96
|
+
export interface OrmClientConfig {
|
|
97
|
+
/** GraphQL endpoint URL (required if adapter not provided) */
|
|
98
|
+
endpoint?: string;
|
|
99
|
+
/** Default headers for HTTP requests (only used with endpoint) */
|
|
100
|
+
headers?: Record<string, string>;
|
|
101
|
+
/** Custom adapter for GraphQL execution (overrides endpoint/headers) */
|
|
102
|
+
adapter?: GraphQLAdapter;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Error thrown when GraphQL request fails
|
|
107
|
+
*/
|
|
108
|
+
export class GraphQLRequestError extends Error {
|
|
109
|
+
constructor(
|
|
110
|
+
public readonly errors: GraphQLError[],
|
|
111
|
+
public readonly data: unknown = null
|
|
112
|
+
) {
|
|
113
|
+
const messages = errors.map(e => e.message).join('; ');
|
|
114
|
+
super(\`GraphQL Error: \${messages}\`);
|
|
115
|
+
this.name = 'GraphQLRequestError';
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export class OrmClient {
|
|
120
|
+
private adapter: GraphQLAdapter;
|
|
121
|
+
|
|
122
|
+
constructor(config: OrmClientConfig) {
|
|
123
|
+
if (config.adapter) {
|
|
124
|
+
this.adapter = config.adapter;
|
|
125
|
+
} else if (config.endpoint) {
|
|
126
|
+
this.adapter = new FetchAdapter(config.endpoint, config.headers);
|
|
127
|
+
} else {
|
|
128
|
+
throw new Error('OrmClientConfig requires either an endpoint or a custom adapter');
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async execute<T>(
|
|
133
|
+
document: string,
|
|
134
|
+
variables?: Record<string, unknown>
|
|
135
|
+
): Promise<QueryResult<T>> {
|
|
136
|
+
return this.adapter.execute<T>(document, variables);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Set headers for requests.
|
|
141
|
+
* Only works if the adapter supports headers.
|
|
142
|
+
*/
|
|
143
|
+
setHeaders(headers: Record<string, string>): void {
|
|
144
|
+
if (this.adapter.setHeaders) {
|
|
145
|
+
this.adapter.setHeaders(headers);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Get the endpoint URL.
|
|
151
|
+
* Returns empty string if the adapter doesn't have an endpoint.
|
|
152
|
+
*/
|
|
153
|
+
getEndpoint(): string {
|
|
154
|
+
return this.adapter.getEndpoint?.() ?? '';
|
|
155
|
+
}
|
|
156
|
+
}
|
|
125
157
|
`;
|
|
126
158
|
return {
|
|
127
159
|
fileName: 'client.ts',
|
|
@@ -320,11 +352,12 @@ export function generateCreateClientFile(tables, hasCustomQueries, hasCustomMuta
|
|
|
320
352
|
statements.push(createImportDeclaration('./mutation', ['createMutationOperations']));
|
|
321
353
|
}
|
|
322
354
|
// Re-export types and classes
|
|
323
|
-
// export type { OrmClientConfig, QueryResult, GraphQLError } from './client';
|
|
355
|
+
// export type { OrmClientConfig, QueryResult, GraphQLError, GraphQLAdapter } from './client';
|
|
324
356
|
const typeExportDecl = t.exportNamedDeclaration(null, [
|
|
325
357
|
t.exportSpecifier(t.identifier('OrmClientConfig'), t.identifier('OrmClientConfig')),
|
|
326
358
|
t.exportSpecifier(t.identifier('QueryResult'), t.identifier('QueryResult')),
|
|
327
359
|
t.exportSpecifier(t.identifier('GraphQLError'), t.identifier('GraphQLError')),
|
|
360
|
+
t.exportSpecifier(t.identifier('GraphQLAdapter'), t.identifier('GraphQLAdapter')),
|
|
328
361
|
], t.stringLiteral('./client'));
|
|
329
362
|
typeExportDecl.exportKind = 'type';
|
|
330
363
|
statements.push(typeExportDecl);
|
|
@@ -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.30.0",
|
|
4
4
|
"description": "CLI-based GraphQL SDK generator for PostGraphile endpoints with React Query hooks",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graphql",
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@babel/generator": "^7.28.6",
|
|
57
57
|
"@babel/types": "^7.28.6",
|
|
58
|
+
"@constructive-io/graphql-types": "^2.14.0",
|
|
58
59
|
"@inquirerer/utils": "^3.2.0",
|
|
59
60
|
"ajv": "^8.17.1",
|
|
60
61
|
"find-and-require-package-json": "^0.9.0",
|
|
@@ -89,5 +90,5 @@
|
|
|
89
90
|
"tsx": "^4.21.0",
|
|
90
91
|
"typescript": "^5.9.3"
|
|
91
92
|
},
|
|
92
|
-
"gitHead": "
|
|
93
|
+
"gitHead": "0f129a3a6f3e939d2c8c0597a9f3a689442b3dcc"
|
|
93
94
|
}
|