@constructive-io/graphql-query 2.4.7 → 2.5.1

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