@newmo/graphql-codegen-fake-server-client 0.22.0 → 0.23.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/dist/graphql-codegen-fake-server-client.js +89 -207
- package/dist/runtime/fake-client-runtime.js +141 -0
- package/dist/runtime-template.js +175 -0
- package/dist/template-helpers.js +291 -0
- package/dist/templates/method-generators.js +294 -0
- package/dist/templates/runtime.js +175 -0
- package/package.json +2 -2
- package/src/graphql-codegen-fake-server-client.ts +192 -276
- package/src/templates/method-generators.ts +324 -0
- package/src/templates/runtime.ts +172 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRegisterQuery = generateRegisterQuery;
|
|
4
|
+
exports.generateRegisterQueryError = generateRegisterQueryError;
|
|
5
|
+
exports.generateRegisterMutation = generateRegisterMutation;
|
|
6
|
+
exports.generateRegisterMutationError = generateRegisterMutationError;
|
|
7
|
+
exports.generateCalledQuery = generateCalledQuery;
|
|
8
|
+
exports.generateCalledMutation = generateCalledMutation;
|
|
9
|
+
/**
|
|
10
|
+
* Helper to indent each line of a string
|
|
11
|
+
*/
|
|
12
|
+
function indent(str, spaces = 4) {
|
|
13
|
+
const indentStr = ' '.repeat(spaces);
|
|
14
|
+
return str.split('\n').map(line => line ? indentStr + line : line).join('\n');
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Helper to create method templates with better readability
|
|
18
|
+
*/
|
|
19
|
+
function createMethod(config) {
|
|
20
|
+
return `async ${config.name}(${config.params}): Promise<${config.returnType}> {
|
|
21
|
+
${indent(config.body)}
|
|
22
|
+
}`;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Generate register query method template
|
|
26
|
+
*/
|
|
27
|
+
function generateRegisterQuery(params) {
|
|
28
|
+
return createMethod({
|
|
29
|
+
name: `register${params.name}QueryResponse`,
|
|
30
|
+
params: `sequenceId:string, queryResponse: ${params.name}Query, sequenceOptions?: FakeClientRegisterSequenceOptions<${params.variablesType}>`,
|
|
31
|
+
returnType: params.responseType,
|
|
32
|
+
body: `const requestCondition = sequenceOptions?.requestCondition ?? { type: "always" };
|
|
33
|
+
const response = await requestQueue.add(() => fetchWithRetry(
|
|
34
|
+
${params.endpoint},
|
|
35
|
+
{
|
|
36
|
+
method: 'POST',
|
|
37
|
+
headers: {
|
|
38
|
+
'Content-Type': 'application/json',
|
|
39
|
+
'sequence-id': sequenceId
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify({
|
|
42
|
+
type: "operation",
|
|
43
|
+
operationName: "${params.name}",
|
|
44
|
+
data: queryResponse,
|
|
45
|
+
requestCondition: requestCondition
|
|
46
|
+
}),
|
|
47
|
+
}
|
|
48
|
+
));
|
|
49
|
+
|
|
50
|
+
const result = await response.json();
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
const errorResult = result as { errors?: string[] };
|
|
53
|
+
throw new Error(\`Failed to register fake response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
54
|
+
}
|
|
55
|
+
return result as ${params.responseType};`
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Generate register query error method template
|
|
60
|
+
*/
|
|
61
|
+
function generateRegisterQueryError(params) {
|
|
62
|
+
return createMethod({
|
|
63
|
+
name: `register${params.name}QueryErrorResponse`,
|
|
64
|
+
params: `sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }`,
|
|
65
|
+
returnType: params.responseType,
|
|
66
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
67
|
+
${params.endpoint},
|
|
68
|
+
{
|
|
69
|
+
method: 'POST',
|
|
70
|
+
headers: {
|
|
71
|
+
'Content-Type': 'application/json',
|
|
72
|
+
'sequence-id': sequenceId
|
|
73
|
+
},
|
|
74
|
+
body: JSON.stringify({
|
|
75
|
+
type: "network-error",
|
|
76
|
+
operationName: "${params.name}",
|
|
77
|
+
responseStatusCode,
|
|
78
|
+
errors
|
|
79
|
+
}),
|
|
80
|
+
}
|
|
81
|
+
));
|
|
82
|
+
|
|
83
|
+
const result = await response.json();
|
|
84
|
+
if (!response.ok) {
|
|
85
|
+
const errorResult = result as { errors?: string[] };
|
|
86
|
+
throw new Error(\`Failed to register fake error response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
87
|
+
}
|
|
88
|
+
return result as ${params.responseType};`
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Generate register mutation method template
|
|
93
|
+
*/
|
|
94
|
+
function generateRegisterMutation(params) {
|
|
95
|
+
return createMethod({
|
|
96
|
+
name: `register${params.name}MutationResponse`,
|
|
97
|
+
params: `sequenceId:string, mutationResponse: ${params.name}Mutation, sequenceOptions?: FakeClientRegisterSequenceOptions<${params.variablesType}>`,
|
|
98
|
+
returnType: params.responseType,
|
|
99
|
+
body: `const requestCondition = sequenceOptions?.requestCondition ?? { type: "always" };
|
|
100
|
+
const response = await requestQueue.add(() => fetchWithRetry(
|
|
101
|
+
${params.endpoint},
|
|
102
|
+
{
|
|
103
|
+
method: 'POST',
|
|
104
|
+
headers: {
|
|
105
|
+
'Content-Type': 'application/json',
|
|
106
|
+
'sequence-id': sequenceId
|
|
107
|
+
},
|
|
108
|
+
body: JSON.stringify({
|
|
109
|
+
type: "operation",
|
|
110
|
+
operationName: "${params.name}",
|
|
111
|
+
data: mutationResponse,
|
|
112
|
+
requestCondition: requestCondition
|
|
113
|
+
}),
|
|
114
|
+
}
|
|
115
|
+
));
|
|
116
|
+
|
|
117
|
+
const result = await response.json();
|
|
118
|
+
if (!response.ok) {
|
|
119
|
+
const errorResult = result as { errors?: string[] };
|
|
120
|
+
throw new Error(\`Failed to register fake response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
121
|
+
}
|
|
122
|
+
return result as ${params.responseType};`
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Generate register mutation error method template
|
|
127
|
+
*/
|
|
128
|
+
function generateRegisterMutationError(params) {
|
|
129
|
+
return createMethod({
|
|
130
|
+
name: `register${params.name}MutationErrorResponse`,
|
|
131
|
+
params: `sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }`,
|
|
132
|
+
returnType: params.responseType,
|
|
133
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
134
|
+
${params.endpoint},
|
|
135
|
+
{
|
|
136
|
+
method: 'POST',
|
|
137
|
+
headers: {
|
|
138
|
+
'Content-Type': 'application/json',
|
|
139
|
+
'sequence-id': sequenceId
|
|
140
|
+
},
|
|
141
|
+
body: JSON.stringify({
|
|
142
|
+
type: "network-error",
|
|
143
|
+
operationName: "${params.name}",
|
|
144
|
+
responseStatusCode,
|
|
145
|
+
errors
|
|
146
|
+
}),
|
|
147
|
+
}
|
|
148
|
+
));
|
|
149
|
+
|
|
150
|
+
const result = await response.json();
|
|
151
|
+
if (!response.ok) {
|
|
152
|
+
const errorResult = result as { errors?: string[] };
|
|
153
|
+
throw new Error(\`Failed to register fake error response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
154
|
+
}
|
|
155
|
+
return result as ${params.responseType};`
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Generate called query method template
|
|
160
|
+
*/
|
|
161
|
+
function generateCalledQuery(params) {
|
|
162
|
+
return createMethod({
|
|
163
|
+
name: `called${params.name}Query`,
|
|
164
|
+
params: `sequenceId:string`,
|
|
165
|
+
returnType: `{
|
|
166
|
+
ok: true;
|
|
167
|
+
data: {
|
|
168
|
+
requestTimestamp: number;
|
|
169
|
+
request: {
|
|
170
|
+
headers: Record<string, unknown>;
|
|
171
|
+
body: {
|
|
172
|
+
operationName: string;
|
|
173
|
+
query: string;
|
|
174
|
+
variables: ${params.variablesType};
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
response: {
|
|
178
|
+
statusCode: number;
|
|
179
|
+
headers: Record<string, unknown>;
|
|
180
|
+
body: ${params.name}Query;
|
|
181
|
+
};
|
|
182
|
+
}[]
|
|
183
|
+
}`,
|
|
184
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
185
|
+
${params.endpoint},
|
|
186
|
+
{
|
|
187
|
+
method: 'POST',
|
|
188
|
+
headers: {
|
|
189
|
+
'Content-Type': 'application/json',
|
|
190
|
+
'sequence-id': sequenceId
|
|
191
|
+
},
|
|
192
|
+
body: JSON.stringify({
|
|
193
|
+
operationName: "${params.name}"
|
|
194
|
+
}),
|
|
195
|
+
}
|
|
196
|
+
));
|
|
197
|
+
|
|
198
|
+
const result = await response.json();
|
|
199
|
+
if (!response.ok) {
|
|
200
|
+
const errorResult = result as { errors?: string[] };
|
|
201
|
+
throw new Error(\`Failed to get called data: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return result as {
|
|
205
|
+
ok: true;
|
|
206
|
+
data: {
|
|
207
|
+
requestTimestamp: number;
|
|
208
|
+
request: {
|
|
209
|
+
headers: Record<string, unknown>;
|
|
210
|
+
body: {
|
|
211
|
+
operationName: string;
|
|
212
|
+
query: string;
|
|
213
|
+
variables: ${params.variablesType};
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
response: {
|
|
217
|
+
statusCode: number;
|
|
218
|
+
headers: Record<string, unknown>;
|
|
219
|
+
body: ${params.name}Query;
|
|
220
|
+
};
|
|
221
|
+
}[];
|
|
222
|
+
};`
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Generate called mutation method template
|
|
227
|
+
*/
|
|
228
|
+
function generateCalledMutation(params) {
|
|
229
|
+
return createMethod({
|
|
230
|
+
name: `called${params.name}Mutation`,
|
|
231
|
+
params: `sequenceId:string`,
|
|
232
|
+
returnType: `{
|
|
233
|
+
ok: true;
|
|
234
|
+
data: {
|
|
235
|
+
requestTimestamp: number;
|
|
236
|
+
request: {
|
|
237
|
+
headers: Record<string, unknown>;
|
|
238
|
+
body: {
|
|
239
|
+
operationName: string;
|
|
240
|
+
query: string;
|
|
241
|
+
variables: ${params.variablesType};
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
response: {
|
|
245
|
+
statusCode: number;
|
|
246
|
+
headers: Record<string, unknown>;
|
|
247
|
+
body: ${params.name}Mutation;
|
|
248
|
+
};
|
|
249
|
+
}[];
|
|
250
|
+
}`,
|
|
251
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
252
|
+
${params.endpoint},
|
|
253
|
+
{
|
|
254
|
+
method: 'POST',
|
|
255
|
+
headers: {
|
|
256
|
+
'Content-Type': 'application/json',
|
|
257
|
+
'sequence-id': sequenceId
|
|
258
|
+
},
|
|
259
|
+
body: JSON.stringify({
|
|
260
|
+
operationName: "${params.name}"
|
|
261
|
+
}),
|
|
262
|
+
}
|
|
263
|
+
));
|
|
264
|
+
|
|
265
|
+
const result = await response.json();
|
|
266
|
+
if (!response.ok) {
|
|
267
|
+
const errorResult = result as { errors?: string[] };
|
|
268
|
+
throw new Error(\`Failed to get called data: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return result as {
|
|
272
|
+
ok: true;
|
|
273
|
+
data: {
|
|
274
|
+
requestTimestamp: number;
|
|
275
|
+
request: {
|
|
276
|
+
headers: Record<string, unknown>;
|
|
277
|
+
body: {
|
|
278
|
+
operationName: string;
|
|
279
|
+
query: string;
|
|
280
|
+
variables: ${params.variablesType};
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
response: {
|
|
284
|
+
statusCode: number;
|
|
285
|
+
headers: Record<string, unknown>;
|
|
286
|
+
body: ${params.name}Mutation;
|
|
287
|
+
};
|
|
288
|
+
}[];
|
|
289
|
+
};`
|
|
290
|
+
});
|
|
291
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRegisterQuery = generateRegisterQuery;
|
|
4
|
+
exports.generateRegisterQueryError = generateRegisterQueryError;
|
|
5
|
+
exports.generateRegisterMutation = generateRegisterMutation;
|
|
6
|
+
exports.generateRegisterMutationError = generateRegisterMutationError;
|
|
7
|
+
exports.generateCalledQuery = generateCalledQuery;
|
|
8
|
+
exports.generateCalledMutation = generateCalledMutation;
|
|
9
|
+
/**
|
|
10
|
+
* Helper to indent each line of a string
|
|
11
|
+
*/
|
|
12
|
+
function indent(str, spaces = 4) {
|
|
13
|
+
const indentStr = " ".repeat(spaces);
|
|
14
|
+
return str
|
|
15
|
+
.split("\n")
|
|
16
|
+
.map((line) => (line ? indentStr + line : line))
|
|
17
|
+
.join("\n");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Helper to create method templates with better readability
|
|
21
|
+
*/
|
|
22
|
+
function createMethod(config) {
|
|
23
|
+
return `async ${config.name}(${config.params}): Promise<${config.returnType}> {
|
|
24
|
+
${indent(config.body)}
|
|
25
|
+
}`;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Generate register query method template
|
|
29
|
+
*/
|
|
30
|
+
function generateRegisterQuery(params) {
|
|
31
|
+
return createMethod({
|
|
32
|
+
name: `register${params.name}QueryResponse`,
|
|
33
|
+
params: `sequenceId:string, queryResponse: ${params.name}Query, sequenceOptions?: FakeClientRegisterSequenceOptions<${params.variablesType}>`,
|
|
34
|
+
returnType: params.responseType,
|
|
35
|
+
body: `const requestCondition = sequenceOptions?.requestCondition ?? { type: "always" };
|
|
36
|
+
const response = await requestQueue.add(() => fetchWithRetry(
|
|
37
|
+
${params.endpoint},
|
|
38
|
+
{
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers: {
|
|
41
|
+
'Content-Type': 'application/json',
|
|
42
|
+
'sequence-id': sequenceId
|
|
43
|
+
},
|
|
44
|
+
body: JSON.stringify({
|
|
45
|
+
type: "operation",
|
|
46
|
+
operationName: "${params.name}",
|
|
47
|
+
data: queryResponse,
|
|
48
|
+
requestCondition: requestCondition
|
|
49
|
+
}),
|
|
50
|
+
}
|
|
51
|
+
));
|
|
52
|
+
|
|
53
|
+
const result = await response.json();
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
const errorResult = result as { errors?: string[] };
|
|
56
|
+
throw new Error(\`Failed to register fake response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
57
|
+
}
|
|
58
|
+
return result as ${params.responseType};`,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Generate register query error method template
|
|
63
|
+
*/
|
|
64
|
+
function generateRegisterQueryError(params) {
|
|
65
|
+
return createMethod({
|
|
66
|
+
name: `register${params.name}QueryErrorResponse`,
|
|
67
|
+
params: "sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }",
|
|
68
|
+
returnType: params.responseType,
|
|
69
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
70
|
+
${params.endpoint},
|
|
71
|
+
{
|
|
72
|
+
method: 'POST',
|
|
73
|
+
headers: {
|
|
74
|
+
'Content-Type': 'application/json',
|
|
75
|
+
'sequence-id': sequenceId
|
|
76
|
+
},
|
|
77
|
+
body: JSON.stringify({
|
|
78
|
+
type: "network-error",
|
|
79
|
+
operationName: "${params.name}",
|
|
80
|
+
responseStatusCode,
|
|
81
|
+
errors
|
|
82
|
+
}),
|
|
83
|
+
}
|
|
84
|
+
));
|
|
85
|
+
|
|
86
|
+
const result = await response.json();
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
const errorResult = result as { errors?: string[] };
|
|
89
|
+
throw new Error(\`Failed to register fake error response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
90
|
+
}
|
|
91
|
+
return result as ${params.responseType};`,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Generate register mutation method template
|
|
96
|
+
*/
|
|
97
|
+
function generateRegisterMutation(params) {
|
|
98
|
+
return createMethod({
|
|
99
|
+
name: `register${params.name}MutationResponse`,
|
|
100
|
+
params: `sequenceId:string, mutationResponse: ${params.name}Mutation, sequenceOptions?: FakeClientRegisterSequenceOptions<${params.variablesType}>`,
|
|
101
|
+
returnType: params.responseType,
|
|
102
|
+
body: `const requestCondition = sequenceOptions?.requestCondition ?? { type: "always" };
|
|
103
|
+
const response = await requestQueue.add(() => fetchWithRetry(
|
|
104
|
+
${params.endpoint},
|
|
105
|
+
{
|
|
106
|
+
method: 'POST',
|
|
107
|
+
headers: {
|
|
108
|
+
'Content-Type': 'application/json',
|
|
109
|
+
'sequence-id': sequenceId
|
|
110
|
+
},
|
|
111
|
+
body: JSON.stringify({
|
|
112
|
+
type: "operation",
|
|
113
|
+
operationName: "${params.name}",
|
|
114
|
+
data: mutationResponse,
|
|
115
|
+
requestCondition: requestCondition
|
|
116
|
+
}),
|
|
117
|
+
}
|
|
118
|
+
));
|
|
119
|
+
|
|
120
|
+
const result = await response.json();
|
|
121
|
+
if (!response.ok) {
|
|
122
|
+
const errorResult = result as { errors?: string[] };
|
|
123
|
+
throw new Error(\`Failed to register fake response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
124
|
+
}
|
|
125
|
+
return result as ${params.responseType};`,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Generate register mutation error method template
|
|
130
|
+
*/
|
|
131
|
+
function generateRegisterMutationError(params) {
|
|
132
|
+
return createMethod({
|
|
133
|
+
name: `register${params.name}MutationErrorResponse`,
|
|
134
|
+
params: "sequenceId:string, { errors, responseStatusCode }: { errors: Record<string, unknown>[]; responseStatusCode: number }",
|
|
135
|
+
returnType: params.responseType,
|
|
136
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
137
|
+
${params.endpoint},
|
|
138
|
+
{
|
|
139
|
+
method: 'POST',
|
|
140
|
+
headers: {
|
|
141
|
+
'Content-Type': 'application/json',
|
|
142
|
+
'sequence-id': sequenceId
|
|
143
|
+
},
|
|
144
|
+
body: JSON.stringify({
|
|
145
|
+
type: "network-error",
|
|
146
|
+
operationName: "${params.name}",
|
|
147
|
+
responseStatusCode,
|
|
148
|
+
errors
|
|
149
|
+
}),
|
|
150
|
+
}
|
|
151
|
+
));
|
|
152
|
+
|
|
153
|
+
const result = await response.json();
|
|
154
|
+
if (!response.ok) {
|
|
155
|
+
const errorResult = result as { errors?: string[] };
|
|
156
|
+
throw new Error(\`Failed to register fake error response: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
157
|
+
}
|
|
158
|
+
return result as ${params.responseType};`,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Generate called query method template
|
|
163
|
+
*/
|
|
164
|
+
function generateCalledQuery(params) {
|
|
165
|
+
return createMethod({
|
|
166
|
+
name: `called${params.name}Query`,
|
|
167
|
+
params: "sequenceId:string",
|
|
168
|
+
returnType: `{
|
|
169
|
+
ok: true;
|
|
170
|
+
data: {
|
|
171
|
+
requestTimestamp: number;
|
|
172
|
+
request: {
|
|
173
|
+
headers: Record<string, unknown>;
|
|
174
|
+
body: {
|
|
175
|
+
operationName: string;
|
|
176
|
+
query: string;
|
|
177
|
+
variables: ${params.variablesType};
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
response: {
|
|
181
|
+
statusCode: number;
|
|
182
|
+
headers: Record<string, unknown>;
|
|
183
|
+
body: ${params.name}Query;
|
|
184
|
+
};
|
|
185
|
+
}[]
|
|
186
|
+
}`,
|
|
187
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
188
|
+
${params.endpoint},
|
|
189
|
+
{
|
|
190
|
+
method: 'POST',
|
|
191
|
+
headers: {
|
|
192
|
+
'Content-Type': 'application/json',
|
|
193
|
+
'sequence-id': sequenceId
|
|
194
|
+
},
|
|
195
|
+
body: JSON.stringify({
|
|
196
|
+
operationName: "${params.name}"
|
|
197
|
+
}),
|
|
198
|
+
}
|
|
199
|
+
));
|
|
200
|
+
|
|
201
|
+
const result = await response.json();
|
|
202
|
+
if (!response.ok) {
|
|
203
|
+
const errorResult = result as { errors?: string[] };
|
|
204
|
+
throw new Error(\`Failed to get called data: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return result as {
|
|
208
|
+
ok: true;
|
|
209
|
+
data: {
|
|
210
|
+
requestTimestamp: number;
|
|
211
|
+
request: {
|
|
212
|
+
headers: Record<string, unknown>;
|
|
213
|
+
body: {
|
|
214
|
+
operationName: string;
|
|
215
|
+
query: string;
|
|
216
|
+
variables: ${params.variablesType};
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
response: {
|
|
220
|
+
statusCode: number;
|
|
221
|
+
headers: Record<string, unknown>;
|
|
222
|
+
body: ${params.name}Query;
|
|
223
|
+
};
|
|
224
|
+
}[];
|
|
225
|
+
};`,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Generate called mutation method template
|
|
230
|
+
*/
|
|
231
|
+
function generateCalledMutation(params) {
|
|
232
|
+
return createMethod({
|
|
233
|
+
name: `called${params.name}Mutation`,
|
|
234
|
+
params: "sequenceId:string",
|
|
235
|
+
returnType: `{
|
|
236
|
+
ok: true;
|
|
237
|
+
data: {
|
|
238
|
+
requestTimestamp: number;
|
|
239
|
+
request: {
|
|
240
|
+
headers: Record<string, unknown>;
|
|
241
|
+
body: {
|
|
242
|
+
operationName: string;
|
|
243
|
+
query: string;
|
|
244
|
+
variables: ${params.variablesType};
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
response: {
|
|
248
|
+
statusCode: number;
|
|
249
|
+
headers: Record<string, unknown>;
|
|
250
|
+
body: ${params.name}Mutation;
|
|
251
|
+
};
|
|
252
|
+
}[];
|
|
253
|
+
}`,
|
|
254
|
+
body: `const response = await requestQueue.add(() => fetchWithRetry(
|
|
255
|
+
${params.endpoint},
|
|
256
|
+
{
|
|
257
|
+
method: 'POST',
|
|
258
|
+
headers: {
|
|
259
|
+
'Content-Type': 'application/json',
|
|
260
|
+
'sequence-id': sequenceId
|
|
261
|
+
},
|
|
262
|
+
body: JSON.stringify({
|
|
263
|
+
operationName: "${params.name}"
|
|
264
|
+
}),
|
|
265
|
+
}
|
|
266
|
+
));
|
|
267
|
+
|
|
268
|
+
const result = await response.json();
|
|
269
|
+
if (!response.ok) {
|
|
270
|
+
const errorResult = result as { errors?: string[] };
|
|
271
|
+
throw new Error(\`Failed to get called data: \${response.status} \${response.statusText}\${errorResult.errors ? ' - ' + JSON.stringify(errorResult.errors) : ''}\`);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return result as {
|
|
275
|
+
ok: true;
|
|
276
|
+
data: {
|
|
277
|
+
requestTimestamp: number;
|
|
278
|
+
request: {
|
|
279
|
+
headers: Record<string, unknown>;
|
|
280
|
+
body: {
|
|
281
|
+
operationName: string;
|
|
282
|
+
query: string;
|
|
283
|
+
variables: ${params.variablesType};
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
response: {
|
|
287
|
+
statusCode: number;
|
|
288
|
+
headers: Record<string, unknown>;
|
|
289
|
+
body: ${params.name}Mutation;
|
|
290
|
+
};
|
|
291
|
+
}[];
|
|
292
|
+
};`,
|
|
293
|
+
});
|
|
294
|
+
}
|