@pgpmjs/migrate-client 0.1.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/LICENSE +23 -0
- package/README.md +112 -0
- package/esm/index.d.ts +1 -0
- package/esm/index.js +1 -0
- package/esm/migrate/index.d.ts +5 -0
- package/esm/migrate/index.js +5 -0
- package/esm/migrate/orm/client.d.ts +55 -0
- package/esm/migrate/orm/client.js +99 -0
- package/esm/migrate/orm/index.d.ts +48 -0
- package/esm/migrate/orm/index.js +45 -0
- package/esm/migrate/orm/input-types.d.ts +513 -0
- package/esm/migrate/orm/input-types.js +2 -0
- package/esm/migrate/orm/models/index.d.ts +7 -0
- package/esm/migrate/orm/models/index.js +7 -0
- package/esm/migrate/orm/models/migrateFile.d.ts +56 -0
- package/esm/migrate/orm/models/migrateFile.js +94 -0
- package/esm/migrate/orm/models/sqlAction.d.ts +56 -0
- package/esm/migrate/orm/models/sqlAction.js +94 -0
- package/esm/migrate/orm/mutation/index.d.ts +27 -0
- package/esm/migrate/orm/mutation/index.js +30 -0
- package/esm/migrate/orm/query-builder.d.ts +91 -0
- package/esm/migrate/orm/query-builder.js +572 -0
- package/esm/migrate/orm/select-types.d.ts +103 -0
- package/esm/migrate/orm/select-types.js +1 -0
- package/esm/migrate/orm/types.d.ts +6 -0
- package/esm/migrate/orm/types.js +7 -0
- package/index.d.ts +1 -0
- package/index.js +17 -0
- package/migrate/index.d.ts +5 -0
- package/migrate/index.js +21 -0
- package/migrate/orm/client.d.ts +55 -0
- package/migrate/orm/client.js +105 -0
- package/migrate/orm/index.d.ts +48 -0
- package/migrate/orm/index.js +66 -0
- package/migrate/orm/input-types.d.ts +513 -0
- package/migrate/orm/input-types.js +5 -0
- package/migrate/orm/models/index.d.ts +7 -0
- package/migrate/orm/models/index.js +12 -0
- package/migrate/orm/models/migrateFile.d.ts +56 -0
- package/migrate/orm/models/migrateFile.js +98 -0
- package/migrate/orm/models/sqlAction.d.ts +56 -0
- package/migrate/orm/models/sqlAction.js +98 -0
- package/migrate/orm/mutation/index.d.ts +27 -0
- package/migrate/orm/mutation/index.js +33 -0
- package/migrate/orm/query-builder.d.ts +91 -0
- package/migrate/orm/query-builder.js +619 -0
- package/migrate/orm/select-types.d.ts +103 -0
- package/migrate/orm/select-types.js +2 -0
- package/migrate/orm/types.d.ts +6 -0
- package/migrate/orm/types.js +23 -0
- package/package.json +53 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.QueryBuilder = void 0;
|
|
37
|
+
exports.buildSelections = buildSelections;
|
|
38
|
+
exports.buildFindManyDocument = buildFindManyDocument;
|
|
39
|
+
exports.buildFindFirstDocument = buildFindFirstDocument;
|
|
40
|
+
exports.buildCreateDocument = buildCreateDocument;
|
|
41
|
+
exports.buildUpdateDocument = buildUpdateDocument;
|
|
42
|
+
exports.buildUpdateByPkDocument = buildUpdateByPkDocument;
|
|
43
|
+
exports.buildFindOneDocument = buildFindOneDocument;
|
|
44
|
+
exports.buildDeleteDocument = buildDeleteDocument;
|
|
45
|
+
exports.buildDeleteByPkDocument = buildDeleteByPkDocument;
|
|
46
|
+
exports.buildCustomDocument = buildCustomDocument;
|
|
47
|
+
/**
|
|
48
|
+
* Query Builder - Builds and executes GraphQL operations
|
|
49
|
+
* @generated by @constructive-io/graphql-codegen
|
|
50
|
+
* DO NOT EDIT - changes will be overwritten
|
|
51
|
+
*/
|
|
52
|
+
const graphql_web_1 = require("@0no-co/graphql.web");
|
|
53
|
+
const t = __importStar(require("gql-ast"));
|
|
54
|
+
const client_1 = require("./client");
|
|
55
|
+
class QueryBuilder {
|
|
56
|
+
config;
|
|
57
|
+
constructor(config) {
|
|
58
|
+
this.config = config;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Execute the query and return a discriminated union result
|
|
62
|
+
* Use result.ok to check success, or .unwrap() to throw on error
|
|
63
|
+
*/
|
|
64
|
+
async execute() {
|
|
65
|
+
const rawResult = await this.config.client.execute(this.config.document, this.config.variables);
|
|
66
|
+
if (!rawResult.ok) {
|
|
67
|
+
return rawResult;
|
|
68
|
+
}
|
|
69
|
+
if (!this.config.transform) {
|
|
70
|
+
return rawResult;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
ok: true,
|
|
74
|
+
data: this.config.transform(rawResult.data),
|
|
75
|
+
errors: undefined,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Execute and unwrap the result, throwing GraphQLRequestError on failure
|
|
80
|
+
* @throws {GraphQLRequestError} If the query returns errors
|
|
81
|
+
*/
|
|
82
|
+
async unwrap() {
|
|
83
|
+
const result = await this.execute();
|
|
84
|
+
if (!result.ok) {
|
|
85
|
+
throw new client_1.GraphQLRequestError(result.errors, result.data);
|
|
86
|
+
}
|
|
87
|
+
return result.data;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Execute and unwrap, returning defaultValue on error instead of throwing
|
|
91
|
+
*/
|
|
92
|
+
async unwrapOr(defaultValue) {
|
|
93
|
+
const result = await this.execute();
|
|
94
|
+
if (!result.ok) {
|
|
95
|
+
return defaultValue;
|
|
96
|
+
}
|
|
97
|
+
return result.data;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Execute and unwrap, calling onError callback on failure
|
|
101
|
+
*/
|
|
102
|
+
async unwrapOrElse(onError) {
|
|
103
|
+
const result = await this.execute();
|
|
104
|
+
if (!result.ok) {
|
|
105
|
+
return onError(result.errors);
|
|
106
|
+
}
|
|
107
|
+
return result.data;
|
|
108
|
+
}
|
|
109
|
+
toGraphQL() {
|
|
110
|
+
return this.config.document;
|
|
111
|
+
}
|
|
112
|
+
getVariables() {
|
|
113
|
+
return this.config.variables;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.QueryBuilder = QueryBuilder;
|
|
117
|
+
const OP_QUERY = 'query';
|
|
118
|
+
const OP_MUTATION = 'mutation';
|
|
119
|
+
const ENUM_VALUE_KIND = 'EnumValue';
|
|
120
|
+
// ============================================================================
|
|
121
|
+
// Selection Builders
|
|
122
|
+
// ============================================================================
|
|
123
|
+
function buildSelections(select, connectionFieldsMap, entityType) {
|
|
124
|
+
if (!select) {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
const fields = [];
|
|
128
|
+
const entityConnections = entityType ? connectionFieldsMap?.[entityType] : undefined;
|
|
129
|
+
for (const [key, value] of Object.entries(select)) {
|
|
130
|
+
if (value === false || value === undefined) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (value === true) {
|
|
134
|
+
fields.push(t.field({ name: key }));
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (typeof value === 'object' && value !== null) {
|
|
138
|
+
const nested = value;
|
|
139
|
+
if (!nested.select || typeof nested.select !== 'object') {
|
|
140
|
+
throw new Error(`Invalid selection for field "${key}": nested selections must include a "select" object.`);
|
|
141
|
+
}
|
|
142
|
+
const relatedEntityType = entityConnections?.[key];
|
|
143
|
+
const nestedSelections = buildSelections(nested.select, connectionFieldsMap, relatedEntityType);
|
|
144
|
+
const isConnection = nested.connection === true ||
|
|
145
|
+
nested.first !== undefined ||
|
|
146
|
+
nested.filter !== undefined ||
|
|
147
|
+
relatedEntityType !== undefined;
|
|
148
|
+
const args = buildArgs([
|
|
149
|
+
buildOptionalArg('first', nested.first),
|
|
150
|
+
nested.filter
|
|
151
|
+
? t.argument({
|
|
152
|
+
name: 'filter',
|
|
153
|
+
value: buildValueAst(nested.filter),
|
|
154
|
+
})
|
|
155
|
+
: null,
|
|
156
|
+
buildEnumListArg('orderBy', nested.orderBy),
|
|
157
|
+
]);
|
|
158
|
+
if (isConnection) {
|
|
159
|
+
fields.push(t.field({
|
|
160
|
+
name: key,
|
|
161
|
+
args,
|
|
162
|
+
selectionSet: t.selectionSet({
|
|
163
|
+
selections: buildConnectionSelections(nestedSelections),
|
|
164
|
+
}),
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
fields.push(t.field({
|
|
169
|
+
name: key,
|
|
170
|
+
args,
|
|
171
|
+
selectionSet: t.selectionSet({ selections: nestedSelections }),
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return fields;
|
|
177
|
+
}
|
|
178
|
+
// ============================================================================
|
|
179
|
+
// Document Builders
|
|
180
|
+
// ============================================================================
|
|
181
|
+
function buildFindManyDocument(operationName, queryField, select, args, filterTypeName, orderByTypeName, connectionFieldsMap, conditionTypeName) {
|
|
182
|
+
const selections = select
|
|
183
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
184
|
+
: [t.field({ name: 'id' })];
|
|
185
|
+
const variableDefinitions = [];
|
|
186
|
+
const queryArgs = [];
|
|
187
|
+
const variables = {};
|
|
188
|
+
addVariable({
|
|
189
|
+
varName: 'condition',
|
|
190
|
+
typeName: conditionTypeName,
|
|
191
|
+
value: args.condition,
|
|
192
|
+
}, variableDefinitions, queryArgs, variables);
|
|
193
|
+
addVariable({
|
|
194
|
+
varName: 'where',
|
|
195
|
+
typeName: filterTypeName,
|
|
196
|
+
value: args.where,
|
|
197
|
+
}, variableDefinitions, queryArgs, variables);
|
|
198
|
+
addVariable({
|
|
199
|
+
varName: 'orderBy',
|
|
200
|
+
typeName: '[' + orderByTypeName + '!]',
|
|
201
|
+
value: args.orderBy?.length ? args.orderBy : undefined,
|
|
202
|
+
}, variableDefinitions, queryArgs, variables);
|
|
203
|
+
addVariable({ varName: 'first', typeName: 'Int', value: args.first }, variableDefinitions, queryArgs, variables);
|
|
204
|
+
addVariable({ varName: 'last', typeName: 'Int', value: args.last }, variableDefinitions, queryArgs, variables);
|
|
205
|
+
addVariable({ varName: 'after', typeName: 'Cursor', value: args.after }, variableDefinitions, queryArgs, variables);
|
|
206
|
+
addVariable({ varName: 'before', typeName: 'Cursor', value: args.before }, variableDefinitions, queryArgs, variables);
|
|
207
|
+
addVariable({ varName: 'offset', typeName: 'Int', value: args.offset }, variableDefinitions, queryArgs, variables);
|
|
208
|
+
const document = t.document({
|
|
209
|
+
definitions: [
|
|
210
|
+
t.operationDefinition({
|
|
211
|
+
operation: OP_QUERY,
|
|
212
|
+
name: operationName + 'Query',
|
|
213
|
+
variableDefinitions: variableDefinitions.length ? variableDefinitions : undefined,
|
|
214
|
+
selectionSet: t.selectionSet({
|
|
215
|
+
selections: [
|
|
216
|
+
t.field({
|
|
217
|
+
name: queryField,
|
|
218
|
+
args: queryArgs.length ? queryArgs : undefined,
|
|
219
|
+
selectionSet: t.selectionSet({
|
|
220
|
+
selections: buildConnectionSelections(selections),
|
|
221
|
+
}),
|
|
222
|
+
}),
|
|
223
|
+
],
|
|
224
|
+
}),
|
|
225
|
+
}),
|
|
226
|
+
],
|
|
227
|
+
});
|
|
228
|
+
return { document: (0, graphql_web_1.print)(document), variables };
|
|
229
|
+
}
|
|
230
|
+
function buildFindFirstDocument(operationName, queryField, select, args, filterTypeName, connectionFieldsMap, conditionTypeName) {
|
|
231
|
+
const selections = select
|
|
232
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
233
|
+
: [t.field({ name: 'id' })];
|
|
234
|
+
const variableDefinitions = [];
|
|
235
|
+
const queryArgs = [];
|
|
236
|
+
const variables = {};
|
|
237
|
+
// Always add first: 1 for findFirst
|
|
238
|
+
addVariable({ varName: 'first', typeName: 'Int', value: 1 }, variableDefinitions, queryArgs, variables);
|
|
239
|
+
addVariable({
|
|
240
|
+
varName: 'condition',
|
|
241
|
+
typeName: conditionTypeName,
|
|
242
|
+
value: args.condition,
|
|
243
|
+
}, variableDefinitions, queryArgs, variables);
|
|
244
|
+
addVariable({
|
|
245
|
+
varName: 'where',
|
|
246
|
+
typeName: filterTypeName,
|
|
247
|
+
value: args.where,
|
|
248
|
+
}, variableDefinitions, queryArgs, variables);
|
|
249
|
+
const document = t.document({
|
|
250
|
+
definitions: [
|
|
251
|
+
t.operationDefinition({
|
|
252
|
+
operation: OP_QUERY,
|
|
253
|
+
name: operationName + 'Query',
|
|
254
|
+
variableDefinitions,
|
|
255
|
+
selectionSet: t.selectionSet({
|
|
256
|
+
selections: [
|
|
257
|
+
t.field({
|
|
258
|
+
name: queryField,
|
|
259
|
+
args: queryArgs,
|
|
260
|
+
selectionSet: t.selectionSet({
|
|
261
|
+
selections: [
|
|
262
|
+
t.field({
|
|
263
|
+
name: 'nodes',
|
|
264
|
+
selectionSet: t.selectionSet({ selections }),
|
|
265
|
+
}),
|
|
266
|
+
],
|
|
267
|
+
}),
|
|
268
|
+
}),
|
|
269
|
+
],
|
|
270
|
+
}),
|
|
271
|
+
}),
|
|
272
|
+
],
|
|
273
|
+
});
|
|
274
|
+
return { document: (0, graphql_web_1.print)(document), variables };
|
|
275
|
+
}
|
|
276
|
+
function buildCreateDocument(operationName, mutationField, entityField, select, data, inputTypeName, connectionFieldsMap) {
|
|
277
|
+
const selections = select
|
|
278
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
279
|
+
: [t.field({ name: 'id' })];
|
|
280
|
+
return {
|
|
281
|
+
document: buildInputMutationDocument({
|
|
282
|
+
operationName,
|
|
283
|
+
mutationField,
|
|
284
|
+
inputTypeName,
|
|
285
|
+
resultSelections: [
|
|
286
|
+
t.field({
|
|
287
|
+
name: entityField,
|
|
288
|
+
selectionSet: t.selectionSet({ selections }),
|
|
289
|
+
}),
|
|
290
|
+
],
|
|
291
|
+
}),
|
|
292
|
+
variables: {
|
|
293
|
+
input: {
|
|
294
|
+
[entityField]: data,
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function buildUpdateDocument(operationName, mutationField, entityField, select, where, data, inputTypeName, patchFieldName, connectionFieldsMap) {
|
|
300
|
+
const selections = select
|
|
301
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
302
|
+
: [t.field({ name: 'id' })];
|
|
303
|
+
return {
|
|
304
|
+
document: buildInputMutationDocument({
|
|
305
|
+
operationName,
|
|
306
|
+
mutationField,
|
|
307
|
+
inputTypeName,
|
|
308
|
+
resultSelections: [
|
|
309
|
+
t.field({
|
|
310
|
+
name: entityField,
|
|
311
|
+
selectionSet: t.selectionSet({ selections }),
|
|
312
|
+
}),
|
|
313
|
+
],
|
|
314
|
+
}),
|
|
315
|
+
variables: {
|
|
316
|
+
input: {
|
|
317
|
+
id: where.id,
|
|
318
|
+
[patchFieldName]: data,
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function buildUpdateByPkDocument(operationName, mutationField, entityField, select, id, data, inputTypeName, idFieldName, patchFieldName, connectionFieldsMap) {
|
|
324
|
+
const selections = select
|
|
325
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
326
|
+
: [t.field({ name: 'id' })];
|
|
327
|
+
return {
|
|
328
|
+
document: buildInputMutationDocument({
|
|
329
|
+
operationName,
|
|
330
|
+
mutationField,
|
|
331
|
+
inputTypeName,
|
|
332
|
+
resultSelections: [
|
|
333
|
+
t.field({
|
|
334
|
+
name: entityField,
|
|
335
|
+
selectionSet: t.selectionSet({ selections }),
|
|
336
|
+
}),
|
|
337
|
+
],
|
|
338
|
+
}),
|
|
339
|
+
variables: {
|
|
340
|
+
input: {
|
|
341
|
+
[idFieldName]: id,
|
|
342
|
+
[patchFieldName]: data,
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
function buildFindOneDocument(operationName, queryField, id, select, idArgName, idTypeName, connectionFieldsMap) {
|
|
348
|
+
const selections = select
|
|
349
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
350
|
+
: [t.field({ name: 'id' })];
|
|
351
|
+
const variableDefinitions = [
|
|
352
|
+
t.variableDefinition({
|
|
353
|
+
variable: t.variable({ name: idArgName }),
|
|
354
|
+
type: (0, graphql_web_1.parseType)(idTypeName),
|
|
355
|
+
}),
|
|
356
|
+
];
|
|
357
|
+
const queryArgs = [
|
|
358
|
+
t.argument({
|
|
359
|
+
name: idArgName,
|
|
360
|
+
value: t.variable({ name: idArgName }),
|
|
361
|
+
}),
|
|
362
|
+
];
|
|
363
|
+
const document = t.document({
|
|
364
|
+
definitions: [
|
|
365
|
+
t.operationDefinition({
|
|
366
|
+
operation: OP_QUERY,
|
|
367
|
+
name: operationName + 'Query',
|
|
368
|
+
variableDefinitions,
|
|
369
|
+
selectionSet: t.selectionSet({
|
|
370
|
+
selections: [
|
|
371
|
+
t.field({
|
|
372
|
+
name: queryField,
|
|
373
|
+
args: queryArgs,
|
|
374
|
+
selectionSet: t.selectionSet({ selections }),
|
|
375
|
+
}),
|
|
376
|
+
],
|
|
377
|
+
}),
|
|
378
|
+
}),
|
|
379
|
+
],
|
|
380
|
+
});
|
|
381
|
+
return {
|
|
382
|
+
document: (0, graphql_web_1.print)(document),
|
|
383
|
+
variables: { [idArgName]: id },
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
function buildDeleteDocument(operationName, mutationField, entityField, where, inputTypeName, select, connectionFieldsMap) {
|
|
387
|
+
const entitySelections = select
|
|
388
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
389
|
+
: [t.field({ name: 'id' })];
|
|
390
|
+
return {
|
|
391
|
+
document: buildInputMutationDocument({
|
|
392
|
+
operationName,
|
|
393
|
+
mutationField,
|
|
394
|
+
inputTypeName,
|
|
395
|
+
resultSelections: [
|
|
396
|
+
t.field({
|
|
397
|
+
name: entityField,
|
|
398
|
+
selectionSet: t.selectionSet({
|
|
399
|
+
selections: entitySelections,
|
|
400
|
+
}),
|
|
401
|
+
}),
|
|
402
|
+
],
|
|
403
|
+
}),
|
|
404
|
+
variables: {
|
|
405
|
+
input: {
|
|
406
|
+
id: where.id,
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
function buildDeleteByPkDocument(operationName, mutationField, entityField, id, inputTypeName, idFieldName, select, connectionFieldsMap) {
|
|
412
|
+
const entitySelections = select
|
|
413
|
+
? buildSelections(select, connectionFieldsMap, operationName)
|
|
414
|
+
: [t.field({ name: 'id' })];
|
|
415
|
+
return {
|
|
416
|
+
document: buildInputMutationDocument({
|
|
417
|
+
operationName,
|
|
418
|
+
mutationField,
|
|
419
|
+
inputTypeName,
|
|
420
|
+
resultSelections: [
|
|
421
|
+
t.field({
|
|
422
|
+
name: entityField,
|
|
423
|
+
selectionSet: t.selectionSet({ selections: entitySelections }),
|
|
424
|
+
}),
|
|
425
|
+
],
|
|
426
|
+
}),
|
|
427
|
+
variables: {
|
|
428
|
+
input: {
|
|
429
|
+
[idFieldName]: id,
|
|
430
|
+
},
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
function buildCustomDocument(operationType, operationName, fieldName, select, args, variableDefinitions, connectionFieldsMap, entityType) {
|
|
435
|
+
let actualSelect = select;
|
|
436
|
+
let isConnection = false;
|
|
437
|
+
if (isCustomSelectionWrapper(select)) {
|
|
438
|
+
actualSelect = select.select;
|
|
439
|
+
isConnection = select.connection === true;
|
|
440
|
+
}
|
|
441
|
+
const selections = actualSelect
|
|
442
|
+
? buildSelections(actualSelect, connectionFieldsMap, entityType)
|
|
443
|
+
: [];
|
|
444
|
+
const variableDefs = variableDefinitions.map((definition) => t.variableDefinition({
|
|
445
|
+
variable: t.variable({ name: definition.name }),
|
|
446
|
+
type: (0, graphql_web_1.parseType)(definition.type),
|
|
447
|
+
}));
|
|
448
|
+
const fieldArgs = variableDefinitions.map((definition) => t.argument({
|
|
449
|
+
name: definition.name,
|
|
450
|
+
value: t.variable({ name: definition.name }),
|
|
451
|
+
}));
|
|
452
|
+
const fieldSelections = isConnection ? buildConnectionSelections(selections) : selections;
|
|
453
|
+
const document = t.document({
|
|
454
|
+
definitions: [
|
|
455
|
+
t.operationDefinition({
|
|
456
|
+
operation: operationType === 'mutation' ? OP_MUTATION : OP_QUERY,
|
|
457
|
+
name: operationName,
|
|
458
|
+
variableDefinitions: variableDefs.length ? variableDefs : undefined,
|
|
459
|
+
selectionSet: t.selectionSet({
|
|
460
|
+
selections: [
|
|
461
|
+
t.field({
|
|
462
|
+
name: fieldName,
|
|
463
|
+
args: fieldArgs.length ? fieldArgs : undefined,
|
|
464
|
+
selectionSet: fieldSelections.length
|
|
465
|
+
? t.selectionSet({ selections: fieldSelections })
|
|
466
|
+
: undefined,
|
|
467
|
+
}),
|
|
468
|
+
],
|
|
469
|
+
}),
|
|
470
|
+
}),
|
|
471
|
+
],
|
|
472
|
+
});
|
|
473
|
+
return {
|
|
474
|
+
document: (0, graphql_web_1.print)(document),
|
|
475
|
+
variables: (args ?? {}),
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function isCustomSelectionWrapper(value) {
|
|
479
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
const record = value;
|
|
483
|
+
const keys = Object.keys(record);
|
|
484
|
+
if (!keys.includes('select') || !keys.includes('connection')) {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
if (keys.some((key) => key !== 'select' && key !== 'connection')) {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
return !!record.select && typeof record.select === 'object' && !Array.isArray(record.select);
|
|
491
|
+
}
|
|
492
|
+
// ============================================================================
|
|
493
|
+
// Helper Functions
|
|
494
|
+
// ============================================================================
|
|
495
|
+
function buildArgs(args) {
|
|
496
|
+
return args.filter((arg) => arg !== null);
|
|
497
|
+
}
|
|
498
|
+
function buildOptionalArg(name, value) {
|
|
499
|
+
if (value === undefined) {
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
const valueNode = typeof value === 'number' ? t.intValue({ value: value.toString() }) : t.stringValue({ value });
|
|
503
|
+
return t.argument({ name, value: valueNode });
|
|
504
|
+
}
|
|
505
|
+
function buildEnumListArg(name, values) {
|
|
506
|
+
if (!values || values.length === 0) {
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
return t.argument({
|
|
510
|
+
name,
|
|
511
|
+
value: t.listValue({
|
|
512
|
+
values: values.map((value) => buildEnumValue(value)),
|
|
513
|
+
}),
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
function buildEnumValue(value) {
|
|
517
|
+
return {
|
|
518
|
+
kind: ENUM_VALUE_KIND,
|
|
519
|
+
value,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function buildPageInfoSelections() {
|
|
523
|
+
return [
|
|
524
|
+
t.field({ name: 'hasNextPage' }),
|
|
525
|
+
t.field({ name: 'hasPreviousPage' }),
|
|
526
|
+
t.field({ name: 'startCursor' }),
|
|
527
|
+
t.field({ name: 'endCursor' }),
|
|
528
|
+
];
|
|
529
|
+
}
|
|
530
|
+
function buildConnectionSelections(nodeSelections) {
|
|
531
|
+
return [
|
|
532
|
+
t.field({
|
|
533
|
+
name: 'nodes',
|
|
534
|
+
selectionSet: t.selectionSet({ selections: nodeSelections }),
|
|
535
|
+
}),
|
|
536
|
+
t.field({ name: 'totalCount' }),
|
|
537
|
+
t.field({
|
|
538
|
+
name: 'pageInfo',
|
|
539
|
+
selectionSet: t.selectionSet({ selections: buildPageInfoSelections() }),
|
|
540
|
+
}),
|
|
541
|
+
];
|
|
542
|
+
}
|
|
543
|
+
function buildInputMutationDocument(config) {
|
|
544
|
+
const document = t.document({
|
|
545
|
+
definitions: [
|
|
546
|
+
t.operationDefinition({
|
|
547
|
+
operation: OP_MUTATION,
|
|
548
|
+
name: config.operationName + 'Mutation',
|
|
549
|
+
variableDefinitions: [
|
|
550
|
+
t.variableDefinition({
|
|
551
|
+
variable: t.variable({ name: 'input' }),
|
|
552
|
+
type: (0, graphql_web_1.parseType)(config.inputTypeName + '!'),
|
|
553
|
+
}),
|
|
554
|
+
],
|
|
555
|
+
selectionSet: t.selectionSet({
|
|
556
|
+
selections: [
|
|
557
|
+
t.field({
|
|
558
|
+
name: config.mutationField,
|
|
559
|
+
args: [
|
|
560
|
+
t.argument({
|
|
561
|
+
name: 'input',
|
|
562
|
+
value: t.variable({ name: 'input' }),
|
|
563
|
+
}),
|
|
564
|
+
],
|
|
565
|
+
selectionSet: t.selectionSet({
|
|
566
|
+
selections: config.resultSelections,
|
|
567
|
+
}),
|
|
568
|
+
}),
|
|
569
|
+
],
|
|
570
|
+
}),
|
|
571
|
+
}),
|
|
572
|
+
],
|
|
573
|
+
});
|
|
574
|
+
return (0, graphql_web_1.print)(document);
|
|
575
|
+
}
|
|
576
|
+
function addVariable(spec, definitions, args, variables) {
|
|
577
|
+
if (spec.value === undefined || !spec.typeName)
|
|
578
|
+
return;
|
|
579
|
+
definitions.push(t.variableDefinition({
|
|
580
|
+
variable: t.variable({ name: spec.varName }),
|
|
581
|
+
type: (0, graphql_web_1.parseType)(spec.typeName),
|
|
582
|
+
}));
|
|
583
|
+
args.push(t.argument({
|
|
584
|
+
name: spec.argName ?? spec.varName,
|
|
585
|
+
value: t.variable({ name: spec.varName }),
|
|
586
|
+
}));
|
|
587
|
+
variables[spec.varName] = spec.value;
|
|
588
|
+
}
|
|
589
|
+
function buildValueAst(value) {
|
|
590
|
+
if (value === null) {
|
|
591
|
+
return t.nullValue();
|
|
592
|
+
}
|
|
593
|
+
if (typeof value === 'boolean') {
|
|
594
|
+
return t.booleanValue({ value });
|
|
595
|
+
}
|
|
596
|
+
if (typeof value === 'number') {
|
|
597
|
+
return Number.isInteger(value)
|
|
598
|
+
? t.intValue({ value: value.toString() })
|
|
599
|
+
: t.floatValue({ value: value.toString() });
|
|
600
|
+
}
|
|
601
|
+
if (typeof value === 'string') {
|
|
602
|
+
return t.stringValue({ value });
|
|
603
|
+
}
|
|
604
|
+
if (Array.isArray(value)) {
|
|
605
|
+
return t.listValue({
|
|
606
|
+
values: value.map((item) => buildValueAst(item)),
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
if (typeof value === 'object' && value !== null) {
|
|
610
|
+
const obj = value;
|
|
611
|
+
return t.objectValue({
|
|
612
|
+
fields: Object.entries(obj).map(([key, val]) => t.objectField({
|
|
613
|
+
name: key,
|
|
614
|
+
value: buildValueAst(val),
|
|
615
|
+
})),
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
throw new Error('Unsupported value type: ' + typeof value);
|
|
619
|
+
}
|