@constructive-io/graphql-query 2.4.6 → 2.5.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/ast.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- // @ts-nocheck
3
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
3
  if (k2 === undefined) k2 = k;
5
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -37,22 +36,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
38
37
  };
39
38
  Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.deleteOne = exports.patchOne = exports.createOne = exports.getOne = exports.getMany = exports.getAll = void 0;
39
+ exports.deleteOne = exports.patchOne = exports.createOne = exports.getOne = exports.getMany = exports.getCount = exports.getAll = void 0;
41
40
  exports.getSelections = getSelections;
42
41
  const t = __importStar(require("gql-ast"));
43
42
  const inflection_1 = __importDefault(require("inflection"));
44
43
  const custom_ast_1 = require("./custom-ast");
45
- const isObject = val => val !== null && typeof val === 'object';
46
44
  const NON_MUTABLE_PROPS = ['createdAt', 'createdBy', 'updatedAt', 'updatedBy'];
47
- const objectToArray = (obj) => Object.keys(obj).map((k) => ({ name: k, ...obj[k] }));
48
- const createGqlMutation = ({ operationName, mutationName, selectArgs, selections, variableDefinitions, modelName, useModel = true }) => {
45
+ const objectToArray = (obj) => Object.keys(obj).map((k) => ({
46
+ key: k,
47
+ name: obj[k].name || k,
48
+ type: obj[k].type,
49
+ isNotNull: obj[k].isNotNull,
50
+ isArray: obj[k].isArray,
51
+ isArrayNotNull: obj[k].isArrayNotNull,
52
+ properties: obj[k].properties,
53
+ }));
54
+ const createGqlMutation = ({ operationName, mutationName, selectArgs, selections, variableDefinitions, modelName, useModel = true, }) => {
49
55
  const opSel = !modelName
50
56
  ? [
51
57
  t.field({
52
58
  name: operationName,
53
59
  args: selectArgs,
54
- selectionSet: t.selectionSet({ selections })
55
- })
60
+ selectionSet: t.selectionSet({ selections }),
61
+ }),
56
62
  ]
57
63
  : [
58
64
  t.field({
@@ -63,12 +69,12 @@ const createGqlMutation = ({ operationName, mutationName, selectArgs, selections
63
69
  ? [
64
70
  t.field({
65
71
  name: modelName,
66
- selectionSet: t.selectionSet({ selections })
67
- })
72
+ selectionSet: t.selectionSet({ selections }),
73
+ }),
68
74
  ]
69
- : selections
70
- })
71
- })
75
+ : selections,
76
+ }),
77
+ }),
72
78
  ];
73
79
  return t.document({
74
80
  definitions: [
@@ -76,227 +82,197 @@ const createGqlMutation = ({ operationName, mutationName, selectArgs, selections
76
82
  operation: 'mutation',
77
83
  name: mutationName,
78
84
  variableDefinitions,
79
- selectionSet: t.selectionSet({ selections: opSel })
80
- })
81
- ]
85
+ selectionSet: t.selectionSet({ selections: opSel }),
86
+ }),
87
+ ],
82
88
  });
83
89
  };
84
- const getAll = ({ queryName, operationName, query, selection }) => {
90
+ const getAll = ({ queryName, operationName, query: _query, selection, }) => {
85
91
  const selections = getSelections(selection);
86
92
  const opSel = [
87
93
  t.field({
88
94
  name: operationName,
89
- selectionSet: t.objectValue({
90
- fields: [
95
+ selectionSet: t.selectionSet({
96
+ selections: [
91
97
  t.field({
92
- name: 'totalCount'
98
+ name: 'totalCount',
93
99
  }),
94
100
  t.field({
95
101
  name: 'nodes',
96
- selectionSet: t.selectionSet({ selections })
97
- })
98
- ]
99
- })
100
- })
102
+ selectionSet: t.selectionSet({ selections }),
103
+ }),
104
+ ],
105
+ }),
106
+ }),
101
107
  ];
102
108
  const ast = t.document({
103
109
  definitions: [
104
110
  t.operationDefinition({
105
111
  operation: 'query',
106
112
  name: queryName,
107
- selectionSet: t.selectionSet({ selections: opSel })
108
- })
109
- ]
113
+ selectionSet: t.selectionSet({ selections: opSel }),
114
+ }),
115
+ ],
110
116
  });
111
117
  return ast;
112
118
  };
113
119
  exports.getAll = getAll;
114
- const getMany = ({ builder, // we can use props here to enable pagination, etc
115
- queryName, operationName, query, selection }) => {
120
+ const getCount = ({ queryName, operationName, query, }) => {
121
+ const Singular = query.model;
122
+ const Filter = `${Singular}Filter`;
123
+ const Condition = `${Singular}Condition`;
124
+ const variableDefinitions = [
125
+ t.variableDefinition({
126
+ variable: t.variable({ name: 'condition' }),
127
+ type: t.namedType({ type: Condition }),
128
+ }),
129
+ t.variableDefinition({
130
+ variable: t.variable({ name: 'filter' }),
131
+ type: t.namedType({ type: Filter }),
132
+ }),
133
+ ];
134
+ const args = [
135
+ t.argument({ name: 'condition', value: t.variable({ name: 'condition' }) }),
136
+ t.argument({ name: 'filter', value: t.variable({ name: 'filter' }) }),
137
+ ];
138
+ // PostGraphile supports totalCount through connections
139
+ const opSel = [
140
+ t.field({
141
+ name: operationName,
142
+ args,
143
+ selectionSet: t.selectionSet({
144
+ selections: [
145
+ t.field({
146
+ name: 'totalCount',
147
+ }),
148
+ ],
149
+ }),
150
+ }),
151
+ ];
152
+ const ast = t.document({
153
+ definitions: [
154
+ t.operationDefinition({
155
+ operation: 'query',
156
+ name: queryName,
157
+ variableDefinitions,
158
+ selectionSet: t.selectionSet({ selections: opSel }),
159
+ }),
160
+ ],
161
+ });
162
+ return ast;
163
+ };
164
+ exports.getCount = getCount;
165
+ const getMany = ({ builder, queryName, operationName, query, selection, }) => {
116
166
  const Singular = query.model;
117
167
  const Plural = operationName.charAt(0).toUpperCase() + operationName.slice(1);
118
168
  const Condition = `${Singular}Condition`;
119
169
  const Filter = `${Singular}Filter`;
120
170
  const OrderBy = `${Plural}OrderBy`;
121
171
  const selections = getSelections(selection);
172
+ const variableDefinitions = [
173
+ t.variableDefinition({
174
+ variable: t.variable({ name: 'first' }),
175
+ type: t.namedType({ type: 'Int' }),
176
+ }),
177
+ t.variableDefinition({
178
+ variable: t.variable({ name: 'last' }),
179
+ type: t.namedType({ type: 'Int' }),
180
+ }),
181
+ t.variableDefinition({
182
+ variable: t.variable({ name: 'after' }),
183
+ type: t.namedType({ type: 'Cursor' }),
184
+ }),
185
+ t.variableDefinition({
186
+ variable: t.variable({ name: 'before' }),
187
+ type: t.namedType({ type: 'Cursor' }),
188
+ }),
189
+ t.variableDefinition({
190
+ variable: t.variable({ name: 'offset' }),
191
+ type: t.namedType({ type: 'Int' }),
192
+ }),
193
+ t.variableDefinition({
194
+ variable: t.variable({ name: 'condition' }),
195
+ type: t.namedType({ type: Condition }),
196
+ }),
197
+ t.variableDefinition({
198
+ variable: t.variable({ name: 'filter' }),
199
+ type: t.namedType({ type: Filter }),
200
+ }),
201
+ t.variableDefinition({
202
+ variable: t.variable({ name: 'orderBy' }),
203
+ type: t.listType({
204
+ type: t.nonNullType({ type: t.namedType({ type: OrderBy }) }),
205
+ }),
206
+ }),
207
+ ];
208
+ const args = [
209
+ t.argument({ name: 'first', value: t.variable({ name: 'first' }) }),
210
+ t.argument({ name: 'last', value: t.variable({ name: 'last' }) }),
211
+ t.argument({ name: 'offset', value: t.variable({ name: 'offset' }) }),
212
+ t.argument({ name: 'after', value: t.variable({ name: 'after' }) }),
213
+ t.argument({ name: 'before', value: t.variable({ name: 'before' }) }),
214
+ t.argument({ name: 'condition', value: t.variable({ name: 'condition' }) }),
215
+ t.argument({ name: 'filter', value: t.variable({ name: 'filter' }) }),
216
+ t.argument({ name: 'orderBy', value: t.variable({ name: 'orderBy' }) }),
217
+ ];
218
+ const pageInfoFields = [
219
+ t.field({ name: 'hasNextPage' }),
220
+ t.field({ name: 'hasPreviousPage' }),
221
+ t.field({ name: 'endCursor' }),
222
+ t.field({ name: 'startCursor' }),
223
+ ];
224
+ const dataField = builder?._edges
225
+ ? t.field({
226
+ name: 'edges',
227
+ selectionSet: t.selectionSet({
228
+ selections: [
229
+ t.field({ name: 'cursor' }),
230
+ t.field({
231
+ name: 'node',
232
+ selectionSet: t.selectionSet({ selections }),
233
+ }),
234
+ ],
235
+ }),
236
+ })
237
+ : t.field({
238
+ name: 'nodes',
239
+ selectionSet: t.selectionSet({ selections }),
240
+ });
241
+ const connectionFields = [
242
+ t.field({ name: 'totalCount' }),
243
+ t.field({
244
+ name: 'pageInfo',
245
+ selectionSet: t.selectionSet({ selections: pageInfoFields }),
246
+ }),
247
+ dataField,
248
+ ];
122
249
  const ast = t.document({
123
250
  definitions: [
124
251
  t.operationDefinition({
125
252
  operation: 'query',
126
253
  name: queryName,
127
- variableDefinitions: [
128
- t.variableDefinition({
129
- variable: t.variable({
130
- name: 'first'
131
- }),
132
- type: t.namedType({
133
- type: 'Int'
134
- })
135
- }),
136
- t.variableDefinition({
137
- variable: t.variable({
138
- name: 'last'
139
- }),
140
- type: t.namedType({
141
- type: 'Int'
142
- })
143
- }),
144
- t.variableDefinition({
145
- variable: t.variable({
146
- name: 'after'
147
- }),
148
- type: t.namedType({
149
- type: 'Cursor'
150
- })
151
- }),
152
- t.variableDefinition({
153
- variable: t.variable({
154
- name: 'before'
155
- }),
156
- type: t.namedType({
157
- type: 'Cursor'
158
- })
159
- }),
160
- t.variableDefinition({
161
- variable: t.variable({
162
- name: 'offset'
163
- }),
164
- type: t.namedType({
165
- type: 'Int'
166
- })
167
- }),
168
- t.variableDefinition({
169
- variable: t.variable({
170
- name: 'condition'
171
- }),
172
- type: t.namedType({
173
- type: Condition
174
- })
175
- }),
176
- t.variableDefinition({
177
- variable: t.variable({
178
- name: 'filter'
179
- }),
180
- type: t.namedType({
181
- type: Filter
182
- })
183
- }),
184
- t.variableDefinition({
185
- variable: t.variable({
186
- name: 'orderBy'
187
- }),
188
- type: t.listType({
189
- type: t.nonNullType({ type: t.namedType({ type: OrderBy }) })
190
- })
191
- })
192
- ],
254
+ variableDefinitions,
193
255
  selectionSet: t.selectionSet({
194
256
  selections: [
195
257
  t.field({
196
258
  name: operationName,
197
- args: [
198
- t.argument({
199
- name: 'first',
200
- value: t.variable({
201
- name: 'first'
202
- })
203
- }),
204
- t.argument({
205
- name: 'last',
206
- value: t.variable({
207
- name: 'last'
208
- })
209
- }),
210
- t.argument({
211
- name: 'offset',
212
- value: t.variable({
213
- name: 'offset'
214
- })
215
- }),
216
- t.argument({
217
- name: 'after',
218
- value: t.variable({
219
- name: 'after'
220
- })
221
- }),
222
- t.argument({
223
- name: 'before',
224
- value: t.variable({
225
- name: 'before'
226
- })
227
- }),
228
- t.argument({
229
- name: 'condition',
230
- value: t.variable({
231
- name: 'condition'
232
- })
233
- }),
234
- t.argument({
235
- name: 'filter',
236
- value: t.variable({
237
- name: 'filter'
238
- })
239
- }),
240
- t.argument({
241
- name: 'orderBy',
242
- value: t.variable({
243
- name: 'orderBy'
244
- })
245
- })
246
- ],
247
- selectionSet: t.objectValue({
248
- fields: [
249
- t.field({
250
- name: 'totalCount'
251
- }),
252
- t.field({
253
- name: 'pageInfo',
254
- selectionSet: t.selectionSet({
255
- selections: [
256
- t.field({ name: 'hasNextPage' }),
257
- t.field({ name: 'hasPreviousPage' }),
258
- t.field({ name: 'endCursor' }),
259
- t.field({ name: 'startCursor' })
260
- ]
261
- })
262
- }),
263
- builder._edges
264
- ? t.field({
265
- name: 'edges',
266
- selectionSet: t.selectionSet({
267
- selections: [
268
- t.field({ name: 'cursor' }),
269
- t.field({
270
- name: 'node',
271
- selectionSet: t.selectionSet({ selections })
272
- })
273
- ]
274
- })
275
- })
276
- : t.field({
277
- name: 'nodes',
278
- selectionSet: t.selectionSet({
279
- selections
280
- })
281
- })
282
- ]
283
- })
284
- })
285
- ]
286
- })
287
- })
288
- ]
259
+ args,
260
+ selectionSet: t.selectionSet({ selections: connectionFields }),
261
+ }),
262
+ ],
263
+ }),
264
+ }),
265
+ ],
289
266
  });
290
267
  return ast;
291
268
  };
292
269
  exports.getMany = getMany;
293
- const getOne = ({ builder, // we can use props here to enable pagination, etc
294
- queryName, operationName, query, selection }) => {
270
+ const getOne = ({ queryName, operationName, query, selection, }) => {
295
271
  const variableDefinitions = Object.keys(query.properties)
296
- .map((key) => ({ name: key, ...query.properties[key] }))
272
+ .map((key) => ({ key, ...query.properties[key] }))
297
273
  .filter((field) => field.isNotNull)
298
274
  .map((field) => {
299
- const { name: fieldName, type: fieldType, isNotNull, isArray, isArrayNotNull } = field;
275
+ const { key: fieldName, type: fieldType, isNotNull, isArray, isArrayNotNull, } = field;
300
276
  let type = t.namedType({ type: fieldType });
301
277
  if (isNotNull)
302
278
  type = t.nonNullType({ type });
@@ -307,7 +283,7 @@ queryName, operationName, query, selection }) => {
307
283
  }
308
284
  return t.variableDefinition({
309
285
  variable: t.variable({ name: fieldName }),
310
- type
286
+ type,
311
287
  });
312
288
  });
313
289
  const props = objectToArray(query.properties);
@@ -316,7 +292,7 @@ queryName, operationName, query, selection }) => {
316
292
  .map((field) => {
317
293
  return t.argument({
318
294
  name: field.name,
319
- value: t.variable({ name: field.name })
295
+ value: t.variable({ name: field.name }),
320
296
  });
321
297
  });
322
298
  const selections = getSelections(selection);
@@ -324,8 +300,8 @@ queryName, operationName, query, selection }) => {
324
300
  t.field({
325
301
  name: operationName,
326
302
  args: selectArgs,
327
- selectionSet: t.selectionSet({ selections })
328
- })
303
+ selectionSet: t.selectionSet({ selections }),
304
+ }),
329
305
  ];
330
306
  const ast = t.document({
331
307
  definitions: [
@@ -333,20 +309,25 @@ queryName, operationName, query, selection }) => {
333
309
  operation: 'query',
334
310
  name: queryName,
335
311
  variableDefinitions,
336
- selectionSet: t.selectionSet({ selections: opSel })
337
- })
338
- ]
312
+ selectionSet: t.selectionSet({ selections: opSel }),
313
+ }),
314
+ ],
339
315
  });
340
316
  return ast;
341
317
  };
342
318
  exports.getOne = getOne;
343
- const createOne = ({ mutationName, operationName, mutation, selection }) => {
319
+ const createOne = ({ mutationName, operationName, mutation, selection, }) => {
344
320
  if (!mutation.properties?.input?.properties) {
345
- console.log('no input field for mutation for' + mutationName);
346
- return;
321
+ throw new Error(`No input field for mutation: ${mutationName}`);
347
322
  }
348
323
  const modelName = inflection_1.default.camelize([inflection_1.default.singularize(mutation.model)].join('_'), true);
349
- const allAttrs = objectToArray(mutation.properties.input.properties[modelName].properties);
324
+ const inputProperties = mutation.properties.input
325
+ .properties;
326
+ const modelProperties = inputProperties[modelName];
327
+ if (!modelProperties.properties) {
328
+ throw new Error(`No properties found for model: ${modelName}`);
329
+ }
330
+ const allAttrs = objectToArray(modelProperties.properties);
350
331
  const attrs = allAttrs.filter((field) => !NON_MUTABLE_PROPS.includes(field.name));
351
332
  const variableDefinitions = getCreateVariablesAst(attrs);
352
333
  const selectArgs = [
@@ -359,15 +340,13 @@ const createOne = ({ mutationName, operationName, mutation, selection }) => {
359
340
  value: t.objectValue({
360
341
  fields: attrs.map((field) => t.objectField({
361
342
  name: field.name,
362
- value: t.variable({
363
- name: field.name
364
- })
365
- }))
366
- })
367
- })
368
- ]
369
- })
370
- })
343
+ value: t.variable({ name: field.name }),
344
+ })),
345
+ }),
346
+ }),
347
+ ],
348
+ }),
349
+ }),
371
350
  ];
372
351
  const selections = selection
373
352
  ? getSelections(selection)
@@ -378,20 +357,24 @@ const createOne = ({ mutationName, operationName, mutation, selection }) => {
378
357
  selectArgs,
379
358
  selections,
380
359
  variableDefinitions,
381
- modelName
360
+ modelName,
382
361
  });
383
362
  return ast;
384
363
  };
385
364
  exports.createOne = createOne;
386
- const patchOne = ({ mutationName, operationName, mutation, selection }) => {
365
+ const patchOne = ({ mutationName, operationName, mutation, selection, }) => {
387
366
  if (!mutation.properties?.input?.properties) {
388
- console.log('no input field for mutation for' + mutationName);
389
- return;
367
+ throw new Error(`No input field for mutation: ${mutationName}`);
390
368
  }
391
369
  const modelName = inflection_1.default.camelize([inflection_1.default.singularize(mutation.model)].join('_'), true);
392
- const allAttrs = objectToArray(mutation.properties.input.properties['patch']?.properties || {});
370
+ const inputProperties = mutation.properties.input
371
+ .properties;
372
+ const patchProperties = inputProperties['patch'];
373
+ const allAttrs = patchProperties?.properties
374
+ ? objectToArray(patchProperties.properties)
375
+ : [];
393
376
  const patchAttrs = allAttrs.filter((prop) => !NON_MUTABLE_PROPS.includes(prop.name));
394
- const patchByAttrs = objectToArray(mutation.properties.input.properties).filter((n) => n.name !== 'patch');
377
+ const patchByAttrs = objectToArray(inputProperties).filter((n) => n.name !== 'patch');
395
378
  const patchers = patchByAttrs.map((p) => p.name);
396
379
  const variableDefinitions = getUpdateVariablesAst(patchAttrs, patchers);
397
380
  const selectArgs = [
@@ -401,7 +384,7 @@ const patchOne = ({ mutationName, operationName, mutation, selection }) => {
401
384
  fields: [
402
385
  ...patchByAttrs.map((field) => t.objectField({
403
386
  name: field.name,
404
- value: t.variable({ name: field.name })
387
+ value: t.variable({ name: field.name }),
405
388
  })),
406
389
  t.objectField({
407
390
  name: 'patch',
@@ -410,15 +393,13 @@ const patchOne = ({ mutationName, operationName, mutation, selection }) => {
410
393
  .filter((field) => !patchers.includes(field.name))
411
394
  .map((field) => t.objectField({
412
395
  name: field.name,
413
- value: t.variable({
414
- name: field.name
415
- })
416
- }))
417
- })
418
- })
419
- ]
420
- })
421
- })
396
+ value: t.variable({ name: field.name }),
397
+ })),
398
+ }),
399
+ }),
400
+ ],
401
+ }),
402
+ }),
422
403
  ];
423
404
  const selections = selection
424
405
  ? getSelections(selection)
@@ -429,20 +410,21 @@ const patchOne = ({ mutationName, operationName, mutation, selection }) => {
429
410
  selectArgs,
430
411
  selections,
431
412
  variableDefinitions,
432
- modelName
413
+ modelName,
433
414
  });
434
415
  return ast;
435
416
  };
436
417
  exports.patchOne = patchOne;
437
- const deleteOne = ({ mutationName, operationName, mutation }) => {
418
+ const deleteOne = ({ mutationName, operationName, mutation, }) => {
438
419
  if (!mutation.properties?.input?.properties) {
439
- console.log('no input field for mutation for' + mutationName);
440
- return;
420
+ throw new Error(`No input field for mutation: ${mutationName}`);
441
421
  }
442
422
  const modelName = inflection_1.default.camelize([inflection_1.default.singularize(mutation.model)].join('_'), true);
443
- const deleteAttrs = objectToArray(mutation.properties.input.properties);
423
+ const inputProperties = mutation.properties.input
424
+ .properties;
425
+ const deleteAttrs = objectToArray(inputProperties);
444
426
  const variableDefinitions = deleteAttrs.map((field) => {
445
- const { name: fieldName, type: fieldType, isNotNull, isArray, isArrayNotNull } = field;
427
+ const { name: fieldName, type: fieldType, isNotNull, isArray } = field;
446
428
  let type = t.namedType({ type: fieldType });
447
429
  if (isNotNull)
448
430
  type = t.nonNullType({ type });
@@ -453,7 +435,7 @@ const deleteOne = ({ mutationName, operationName, mutation }) => {
453
435
  }
454
436
  return t.variableDefinition({
455
437
  variable: t.variable({ name: fieldName }),
456
- type
438
+ type,
457
439
  });
458
440
  });
459
441
  const selectArgs = [
@@ -462,10 +444,10 @@ const deleteOne = ({ mutationName, operationName, mutation }) => {
462
444
  value: t.objectValue({
463
445
  fields: deleteAttrs.map((f) => t.objectField({
464
446
  name: f.name,
465
- value: t.variable({ name: f.name })
466
- }))
467
- })
468
- })
447
+ value: t.variable({ name: f.name }),
448
+ })),
449
+ }),
450
+ }),
469
451
  ];
470
452
  // so we can support column select grants plugin
471
453
  const selections = [t.field({ name: 'clientMutationId' })];
@@ -476,7 +458,7 @@ const deleteOne = ({ mutationName, operationName, mutation }) => {
476
458
  selections,
477
459
  useModel: false,
478
460
  variableDefinitions,
479
- modelName
461
+ modelName,
480
462
  });
481
463
  return ast;
482
464
  };
@@ -484,110 +466,82 @@ exports.deleteOne = deleteOne;
484
466
  function getSelections(selection = []) {
485
467
  const selectionAst = (field) => {
486
468
  return typeof field === 'string'
487
- ? t.field({
488
- name: field
489
- })
490
- : (0, custom_ast_1.getCustomAst)(field.fieldDefn);
469
+ ? t.field({ name: field })
470
+ : (0, custom_ast_1.getCustomAst)(field.fieldDefn) || t.field({ name: field.name });
491
471
  };
492
472
  return selection
493
473
  .map((selectionDefn) => {
494
474
  if (selectionDefn.isObject) {
495
475
  const { name, selection, variables = {}, isBelongTo } = selectionDefn;
476
+ const args = Object.entries(variables).reduce((acc, variable) => {
477
+ const [argName, argValue] = variable;
478
+ const argAst = t.argument({
479
+ name: argName,
480
+ value: getComplexValueAst(argValue),
481
+ });
482
+ return argAst ? [...acc, argAst] : acc;
483
+ }, []);
484
+ const subSelections = selection?.map((field) => selectionAst(field)) || [];
485
+ const selectionSet = isBelongTo
486
+ ? t.selectionSet({ selections: subSelections })
487
+ : t.selectionSet({
488
+ selections: [
489
+ t.field({ name: 'totalCount' }),
490
+ t.field({
491
+ name: 'nodes',
492
+ selectionSet: t.selectionSet({ selections: subSelections }),
493
+ }),
494
+ ],
495
+ });
496
496
  return t.field({
497
497
  name,
498
- args: Object.entries(variables).reduce((args, variable) => {
499
- const [argName, argValue] = variable;
500
- const argAst = t.argument({
501
- name: argName,
502
- value: getValueAst(argValue)
503
- });
504
- args = argAst ? [...args, argAst] : args;
505
- return args;
506
- }, []),
507
- selectionSet: isBelongTo
508
- ? t.selectionSet({
509
- selections: selection.map((field) => selectionAst(field))
510
- })
511
- : t.objectValue({
512
- fields: [
513
- t.field({
514
- name: 'totalCount'
515
- }),
516
- t.field({
517
- name: 'nodes',
518
- selectionSet: t.selectionSet({
519
- selections: selection.map((field) => selectionAst(field))
520
- })
521
- })
522
- ]
523
- })
498
+ args,
499
+ selectionSet,
524
500
  });
525
501
  }
526
502
  else {
527
- const { fieldDefn } = selectionDefn;
528
- // Field is not found in model meta, do nothing
529
- if (!fieldDefn)
530
- return null;
531
- return (0, custom_ast_1.getCustomAst)(fieldDefn);
503
+ return selectionAst(selectionDefn);
532
504
  }
533
505
  })
534
- .filter(Boolean);
506
+ .filter((node) => node !== null);
535
507
  }
536
- /**
537
- * Get argument AST from a value
538
- * @param {*} value
539
- * @returns {Object} AST for the argument
540
- */
541
- function getValueAst(value) {
542
- if (value == null) {
508
+ function getComplexValueAst(value) {
509
+ // Handle null
510
+ if (value === null) {
543
511
  return t.nullValue();
544
512
  }
513
+ // Handle primitives
514
+ if (typeof value === 'boolean') {
515
+ return t.booleanValue({ value });
516
+ }
545
517
  if (typeof value === 'number') {
546
- return t.intValue({ value });
518
+ return t.intValue({ value: value.toString() });
547
519
  }
548
520
  if (typeof value === 'string') {
549
521
  return t.stringValue({ value });
550
522
  }
551
- if (typeof value === 'boolean') {
552
- return t.booleanValue({ value });
553
- }
523
+ // Handle arrays
554
524
  if (Array.isArray(value)) {
555
- return t.listValue({ values: value.map((v) => getValueAst(v)) });
525
+ return t.listValue({
526
+ values: value.map((item) => getComplexValueAst(item)),
527
+ });
556
528
  }
557
- if (isObject(value)) {
529
+ // Handle objects
530
+ if (typeof value === 'object' && value !== null) {
531
+ const obj = value;
558
532
  return t.objectValue({
559
- fields: Object.entries(value).reduce((fields, entry) => {
560
- const [objKey, objValue] = entry;
561
- fields = [
562
- ...fields,
563
- t.objectField({
564
- name: objKey,
565
- value: getValueAst(objValue)
566
- })
567
- ];
568
- return fields;
569
- }, [])
533
+ fields: Object.entries(obj).map(([key, val]) => t.objectField({
534
+ name: key,
535
+ value: getComplexValueAst(val),
536
+ })),
570
537
  });
571
538
  }
539
+ throw new Error(`Unsupported value type: ${typeof value}`);
572
540
  }
573
- const CustomInputTypes = {
574
- interval: 'IntervalInput'
575
- };
576
- /**
577
- * Get mutation variables AST from attributes array
578
- * @param {Array} attrs
579
- * @returns {Object} AST for the variables
580
- */
581
541
  function getCreateVariablesAst(attrs) {
582
542
  return attrs.map((field) => {
583
- const { name: fieldName, type: fieldType, isNotNull, isArray, isArrayNotNull, properties } = field;
584
- let type;
585
- if (properties == null) {
586
- type = t.namedType({ type: fieldType });
587
- }
588
- else if ((0, custom_ast_1.isIntervalType)(properties)) {
589
- type = t.namedType({ type: CustomInputTypes.interval });
590
- }
543
+ const { name: fieldName, type: fieldType, isNotNull, isArray, isArrayNotNull, } = field;
544
+ let type = t.namedType({ type: fieldType });
591
545
  if (isNotNull)
592
546
  type = t.nonNullType({ type });
593
547
  if (isArray) {
@@ -597,34 +551,31 @@ function getCreateVariablesAst(attrs) {
597
551
  }
598
552
  return t.variableDefinition({
599
553
  variable: t.variable({ name: fieldName }),
600
- type
554
+ type,
601
555
  });
602
556
  });
603
557
  }
604
- /**
605
- * Get mutation variables AST from attributes array
606
- * @param {Array} attrs
607
- * @returns {Object} AST for the variables
608
- */
609
558
  function getUpdateVariablesAst(attrs, patchers) {
610
- return attrs.map((field) => {
611
- const { name: fieldName, type: fieldType, isNotNull, isArray, properties } = field;
612
- let type;
613
- if (properties == null) {
614
- type = t.namedType({ type: fieldType });
615
- }
616
- else if ((0, custom_ast_1.isIntervalType)(properties)) {
617
- type = t.namedType({ type: CustomInputTypes.interval });
618
- }
619
- if (isNotNull)
620
- type = t.nonNullType({ type });
621
- if (isArray)
559
+ const patchVariables = attrs
560
+ .filter((field) => !patchers.includes(field.name))
561
+ .map((field) => {
562
+ const { name: fieldName, type: fieldType, isArray, isArrayNotNull, } = field;
563
+ let type = t.namedType({ type: fieldType });
564
+ if (isArray) {
622
565
  type = t.listType({ type });
623
- if (patchers.includes(field.name))
624
- type = t.nonNullType({ type });
566
+ if (isArrayNotNull)
567
+ type = t.nonNullType({ type });
568
+ }
625
569
  return t.variableDefinition({
626
570
  variable: t.variable({ name: fieldName }),
627
- type
571
+ type,
572
+ });
573
+ });
574
+ const patcherVariables = patchers.map((patcher) => {
575
+ return t.variableDefinition({
576
+ variable: t.variable({ name: patcher }),
577
+ type: t.nonNullType({ type: t.namedType({ type: 'String' }) }),
628
578
  });
629
579
  });
580
+ return [...patchVariables, ...patcherVariables];
630
581
  }