@smartive/graphql-magic 23.11.0-next.1 → 24.0.0-next.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,5 @@
1
- ## [23.11.0-next.1](https://github.com/smartive/graphql-magic/compare/v23.10.0...v23.11.0-next.1) (2026-03-16)
1
+ ## [24.0.0-next.2](https://github.com/smartive/graphql-magic/compare/v24.0.0-next.1...v24.0.0-next.2) (2026-03-16)
2
2
 
3
3
  ### Features
4
4
 
5
- * Enable mutation args ([6f67ed4](https://github.com/smartive/graphql-magic/commit/6f67ed48eb539c52c7374078b72ff6cdf9fe6f2c))
5
+ * Delete/restore args ([aad65c1](https://github.com/smartive/graphql-magic/commit/aad65c1c39f8ef6c6aeef2ec4512faf702f642ef))
package/dist/bin/gqm.cjs CHANGED
@@ -41,47 +41,65 @@ var import_graphql = require("graphql");
41
41
  // src/client/mutations.ts
42
42
  var import_upperCase = __toESM(require("lodash/upperCase"), 1);
43
43
  var constantCase = (str) => (0, import_upperCase.default)(str).replace(/ /g, "_");
44
+ var argTypeStr = (field) => {
45
+ const base = field.list ? `[${field.type}!]` : field.type;
46
+ return field.nonNull ? `${base}!` : base;
47
+ };
48
+ var argsToVariables = (args2) => args2.map((a) => `$${a.name}: ${argTypeStr(a)}`).join(", ");
49
+ var argsToMutationArgs = (args2) => args2.map((a) => `${a.name}: $${a.name}`).join(", ");
44
50
  var generateMutations = (models) => {
45
51
  const parts = [];
46
52
  for (const { name: name2, creatable, updatable, deletable } of models.entities.filter(not(isRootModel))) {
47
53
  if (creatable) {
54
+ const extraArgs = creatable !== true && creatable.args ? creatable.args : [];
55
+ const variables = extraArgs.length ? `$data: Create${name2}!, ${argsToVariables(extraArgs)}` : `$data: Create${name2}!`;
56
+ const mutationArgs = extraArgs.length ? `data: $data, ${argsToMutationArgs(extraArgs)}` : `data: $data`;
48
57
  parts.push(
49
58
  `export const CREATE_${constantCase(
50
59
  name2
51
60
  )} = gql\`
52
- mutation Create${name2}Mutation($data: Create${name2}!) {
53
- create${name2}(data: $data) { id }
61
+ mutation Create${name2}Mutation(${variables}) {
62
+ create${name2}(${mutationArgs}) { id }
54
63
  }
55
64
  \`;`
56
65
  );
57
66
  }
58
67
  if (updatable) {
68
+ const extraArgs = updatable !== true && updatable.args ? updatable.args : [];
69
+ const variables = extraArgs.length ? `$id: ID!, $data: Update${name2}!, ${argsToVariables(extraArgs)}` : `$id: ID!, $data: Update${name2}!`;
70
+ const mutationArgs = extraArgs.length ? `where: { id: $id }, data: $data, ${argsToMutationArgs(extraArgs)}` : `where: { id: $id }, data: $data`;
59
71
  parts.push(
60
72
  `export const UPDATE_${constantCase(
61
73
  name2
62
74
  )} = gql\`
63
- mutation Update${name2}Mutation($id: ID!, $data: Update${name2}!) {
64
- update${name2}(where: { id: $id }, data: $data) { id }
75
+ mutation Update${name2}Mutation(${variables}) {
76
+ update${name2}(${mutationArgs}) { id }
65
77
  }
66
78
  \`;`
67
79
  );
68
80
  }
69
81
  if (deletable) {
82
+ const deleteExtraArgs = deletable !== true && deletable.args ? deletable.args : [];
83
+ const deleteVariables = deleteExtraArgs.length ? `$id: ID!, ${argsToVariables(deleteExtraArgs)}` : `$id: ID!`;
84
+ const deleteMutationArgs = deleteExtraArgs.length ? `where: { id: $id }, ${argsToMutationArgs(deleteExtraArgs)}` : `where: { id: $id }`;
70
85
  parts.push(
71
86
  `export const DELETE_${constantCase(
72
87
  name2
73
88
  )} = gql\`
74
- mutation Delete${name2}Mutation($id: ID!) {
75
- delete${name2}(where: { id: $id })
89
+ mutation Delete${name2}Mutation(${deleteVariables}) {
90
+ delete${name2}(${deleteMutationArgs})
76
91
  }
77
92
  \`;`
78
93
  );
94
+ const restoreExtraArgs = deletable !== true && deletable.restoreArgs ? deletable.restoreArgs : [];
95
+ const restoreVariables = restoreExtraArgs.length ? `$id: ID!, ${argsToVariables(restoreExtraArgs)}` : `$id: ID!`;
96
+ const restoreMutationArgs = restoreExtraArgs.length ? `where: { id: $id }, ${argsToMutationArgs(restoreExtraArgs)}` : `where: { id: $id }`;
79
97
  parts.push(
80
98
  `export const RESTORE_${constantCase(
81
99
  name2
82
100
  )} = gql\`
83
- mutation Restore${name2}Mutation($id: ID!) {
84
- restore${name2}(where: { id: $id })
101
+ mutation Restore${name2}Mutation(${restoreVariables}) {
102
+ restore${name2}(${restoreMutationArgs})
85
103
  }
86
104
  \`;`
87
105
  );
@@ -1884,11 +1902,7 @@ var MigrationGenerator = class _MigrationGenerator {
1884
1902
  }
1885
1903
  return -1;
1886
1904
  }
1887
- static TRIGGER_EVENT_ORDER = [
1888
- "INSERT",
1889
- "UPDATE",
1890
- "DELETE"
1891
- ];
1905
+ static TRIGGER_EVENT_ORDER = ["INSERT", "UPDATE", "DELETE"];
1892
1906
  static sortTriggerEvents(events) {
1893
1907
  return [...events].sort(
1894
1908
  (a, b) => _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) - _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b)
@@ -1896,7 +1910,9 @@ var MigrationGenerator = class _MigrationGenerator {
1896
1910
  }
1897
1911
  normalizeTriggerDef(def) {
1898
1912
  let s = def.replace(/\s+/g, " ").replace(/\s*\(\s*/g, "(").replace(/\s*\)\s*/g, ")").replace(/\bON\s+[a-zA-Z_][a-zA-Z0-9_]*\./gi, "ON ").trim();
1899
- const eventsMatch = s.match(/\b(AFTER|BEFORE)\s+((?:INSERT|UPDATE|DELETE)(?:\s+OR\s+(?:INSERT|UPDATE|DELETE))+)\s+ON\b/i);
1913
+ const eventsMatch = s.match(
1914
+ /\b(AFTER|BEFORE)\s+((?:INSERT|UPDATE|DELETE)(?:\s+OR\s+(?:INSERT|UPDATE|DELETE))+)\s+ON\b/i
1915
+ );
1900
1916
  if (eventsMatch) {
1901
1917
  const events = eventsMatch[2].split(/\s+OR\s+/).map((e) => e.toUpperCase());
1902
1918
  const sorted = [...events].sort(
@@ -2843,7 +2859,8 @@ var generateDefinitions = ({
2843
2859
  {
2844
2860
  name: "dryRun",
2845
2861
  type: "Boolean"
2846
- }
2862
+ },
2863
+ ...model.deletable && model.deletable !== true && model.deletable.args || []
2847
2864
  ]
2848
2865
  });
2849
2866
  mutations2.push({
@@ -2855,7 +2872,8 @@ var generateDefinitions = ({
2855
2872
  name: "where",
2856
2873
  type: `${model.name}WhereUnique`,
2857
2874
  nonNull: true
2858
- }
2875
+ },
2876
+ ...model.deletable && model.deletable !== true && model.deletable.restoreArgs || []
2859
2877
  ]
2860
2878
  });
2861
2879
  }
@@ -307,47 +307,65 @@ var gql = (chunks, ...variables) => {
307
307
  // src/client/mutations.ts
308
308
  var import_upperCase = __toESM(require("lodash/upperCase"), 1);
309
309
  var constantCase = (str) => (0, import_upperCase.default)(str).replace(/ /g, "_");
310
+ var argTypeStr = (field) => {
311
+ const base = field.list ? `[${field.type}!]` : field.type;
312
+ return field.nonNull ? `${base}!` : base;
313
+ };
314
+ var argsToVariables = (args2) => args2.map((a) => `$${a.name}: ${argTypeStr(a)}`).join(", ");
315
+ var argsToMutationArgs = (args2) => args2.map((a) => `${a.name}: $${a.name}`).join(", ");
310
316
  var generateMutations = (models) => {
311
317
  const parts = [];
312
318
  for (const { name: name2, creatable, updatable, deletable } of models.entities.filter(not(isRootModel))) {
313
319
  if (creatable) {
320
+ const extraArgs = creatable !== true && creatable.args ? creatable.args : [];
321
+ const variables = extraArgs.length ? `$data: Create${name2}!, ${argsToVariables(extraArgs)}` : `$data: Create${name2}!`;
322
+ const mutationArgs = extraArgs.length ? `data: $data, ${argsToMutationArgs(extraArgs)}` : `data: $data`;
314
323
  parts.push(
315
324
  `export const CREATE_${constantCase(
316
325
  name2
317
326
  )} = gql\`
318
- mutation Create${name2}Mutation($data: Create${name2}!) {
319
- create${name2}(data: $data) { id }
327
+ mutation Create${name2}Mutation(${variables}) {
328
+ create${name2}(${mutationArgs}) { id }
320
329
  }
321
330
  \`;`
322
331
  );
323
332
  }
324
333
  if (updatable) {
334
+ const extraArgs = updatable !== true && updatable.args ? updatable.args : [];
335
+ const variables = extraArgs.length ? `$id: ID!, $data: Update${name2}!, ${argsToVariables(extraArgs)}` : `$id: ID!, $data: Update${name2}!`;
336
+ const mutationArgs = extraArgs.length ? `where: { id: $id }, data: $data, ${argsToMutationArgs(extraArgs)}` : `where: { id: $id }, data: $data`;
325
337
  parts.push(
326
338
  `export const UPDATE_${constantCase(
327
339
  name2
328
340
  )} = gql\`
329
- mutation Update${name2}Mutation($id: ID!, $data: Update${name2}!) {
330
- update${name2}(where: { id: $id }, data: $data) { id }
341
+ mutation Update${name2}Mutation(${variables}) {
342
+ update${name2}(${mutationArgs}) { id }
331
343
  }
332
344
  \`;`
333
345
  );
334
346
  }
335
347
  if (deletable) {
348
+ const deleteExtraArgs = deletable !== true && deletable.args ? deletable.args : [];
349
+ const deleteVariables = deleteExtraArgs.length ? `$id: ID!, ${argsToVariables(deleteExtraArgs)}` : `$id: ID!`;
350
+ const deleteMutationArgs = deleteExtraArgs.length ? `where: { id: $id }, ${argsToMutationArgs(deleteExtraArgs)}` : `where: { id: $id }`;
336
351
  parts.push(
337
352
  `export const DELETE_${constantCase(
338
353
  name2
339
354
  )} = gql\`
340
- mutation Delete${name2}Mutation($id: ID!) {
341
- delete${name2}(where: { id: $id })
355
+ mutation Delete${name2}Mutation(${deleteVariables}) {
356
+ delete${name2}(${deleteMutationArgs})
342
357
  }
343
358
  \`;`
344
359
  );
360
+ const restoreExtraArgs = deletable !== true && deletable.restoreArgs ? deletable.restoreArgs : [];
361
+ const restoreVariables = restoreExtraArgs.length ? `$id: ID!, ${argsToVariables(restoreExtraArgs)}` : `$id: ID!`;
362
+ const restoreMutationArgs = restoreExtraArgs.length ? `where: { id: $id }, ${argsToMutationArgs(restoreExtraArgs)}` : `where: { id: $id }`;
345
363
  parts.push(
346
364
  `export const RESTORE_${constantCase(
347
365
  name2
348
366
  )} = gql\`
349
- mutation Restore${name2}Mutation($id: ID!) {
350
- restore${name2}(where: { id: $id })
367
+ mutation Restore${name2}Mutation(${restoreVariables}) {
368
+ restore${name2}(${restoreMutationArgs})
351
369
  }
352
370
  \`;`
353
371
  );
@@ -2337,30 +2355,31 @@ var mutationResolver = async (_parent, args2, partialCtx, info) => withTransacti
2337
2355
  const [, mutation, modelName] = it(info.fieldName.match(/^(create|update|delete|restore)(.+)$/));
2338
2356
  switch (mutation) {
2339
2357
  case "create": {
2340
- const id = await createEntity(modelName, args2.data, { ...ctx, args: args2 }, "mutation");
2358
+ const id = await createEntity(modelName, args2.data, ctx, { trigger: "mutation", args: args2 });
2341
2359
  return await resolve(ctx, id);
2342
2360
  }
2343
2361
  case "update": {
2344
2362
  const id = args2.where.id;
2345
- await updateEntity(modelName, id, args2.data, { ...ctx, args: args2 }, "mutation");
2363
+ await updateEntity(modelName, id, args2.data, ctx, { trigger: "mutation", args: args2 });
2346
2364
  return await resolve(ctx, id);
2347
2365
  }
2348
2366
  case "delete": {
2349
2367
  const id = args2.where.id;
2350
2368
  await deleteEntity(modelName, id, ctx, {
2351
2369
  dryRun: args2.dryRun,
2352
- trigger: "mutation"
2370
+ trigger: "mutation",
2371
+ args: args2
2353
2372
  });
2354
2373
  return id;
2355
2374
  }
2356
2375
  case "restore": {
2357
2376
  const id = args2.where.id;
2358
- await restoreEntity(modelName, id, ctx, "mutation");
2377
+ await restoreEntity(modelName, id, ctx, { trigger: "mutation", args: args2 });
2359
2378
  return id;
2360
2379
  }
2361
2380
  }
2362
2381
  });
2363
- var createEntity = async (modelName, input2, ctx, trigger = "direct-call") => withTransaction(ctx, async (ctx2) => {
2382
+ var createEntity = async (modelName, input2, ctx, { args: args2, trigger = "direct-call" } = {}) => withTransaction(ctx, async (ctx2) => {
2364
2383
  const model = ctx2.models.getModel(modelName, "entity");
2365
2384
  const normalizedInput = { ...input2 };
2366
2385
  if (!normalizedInput.id) {
@@ -2384,7 +2403,7 @@ var createEntity = async (modelName, input2, ctx, trigger = "direct-call") => wi
2384
2403
  trigger,
2385
2404
  when: "before",
2386
2405
  data: { prev: {}, input: input2, normalizedInput, next: normalizedInput },
2387
- args: ctx2.args ?? {},
2406
+ args: args2,
2388
2407
  ctx: ctx2
2389
2408
  });
2390
2409
  if (model.parent) {
@@ -2417,18 +2436,18 @@ var createEntity = async (modelName, input2, ctx, trigger = "direct-call") => wi
2417
2436
  trigger,
2418
2437
  when: "after",
2419
2438
  data: { prev: {}, input: input2, normalizedInput, next: normalizedInput },
2420
- args: ctx2.args ?? {},
2439
+ args: args2,
2421
2440
  ctx: ctx2
2422
2441
  });
2423
2442
  return normalizedInput.id;
2424
2443
  });
2425
- var updateEntities = async (modelName, where, updateFields, ctx) => withTransaction(ctx, async (ctx2) => {
2444
+ var updateEntities = async (modelName, where, updateFields, ctx, { args: args2 } = {}) => withTransaction(ctx, async (ctx2) => {
2426
2445
  const entities = await ctx2.knex(modelName).where(where).select("id");
2427
2446
  for (const entity of entities) {
2428
- await updateEntity(modelName, entity.id, updateFields, ctx2);
2447
+ await updateEntity(modelName, entity.id, updateFields, ctx2, args2);
2429
2448
  }
2430
2449
  });
2431
- var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") => withTransaction(ctx, async (ctx2) => {
2450
+ var updateEntity = async (modelName, id, input2, ctx, { args: args2, trigger = "direct-call" } = {}) => withTransaction(ctx, async (ctx2) => {
2432
2451
  const model = ctx2.models.getModel(modelName, "entity");
2433
2452
  const normalizedInput = { ...input2 };
2434
2453
  sanitize(ctx2, model, normalizedInput);
@@ -2446,7 +2465,7 @@ var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") =
2446
2465
  trigger,
2447
2466
  when: "before",
2448
2467
  data: { prev: currentEntity, input: input2, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
2449
- args: ctx2.args ?? {},
2468
+ args: args2,
2450
2469
  ctx: ctx2
2451
2470
  });
2452
2471
  await doUpdate(model, currentEntity, normalizedInput, ctx2);
@@ -2456,16 +2475,17 @@ var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") =
2456
2475
  trigger,
2457
2476
  when: "after",
2458
2477
  data: { prev: currentEntity, input: input2, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
2459
- args: ctx2.args ?? {},
2478
+ args: args2,
2460
2479
  ctx: ctx2
2461
2480
  });
2462
2481
  }
2463
2482
  });
2464
- var deleteEntities = async (modelName, where, ctx) => withTransaction(ctx, async (ctx2) => {
2483
+ var deleteEntities = async (modelName, where, ctx, args2) => withTransaction(ctx, async (ctx2) => {
2465
2484
  const entities = await ctx2.knex(modelName).where(where).select("id");
2466
2485
  for (const entity of entities) {
2467
2486
  await deleteEntity(modelName, entity.id, ctx2, {
2468
- trigger: "direct-call"
2487
+ trigger: "direct-call",
2488
+ args: args2
2469
2489
  });
2470
2490
  }
2471
2491
  });
@@ -2486,7 +2506,7 @@ var deleteEntity = async (modelName, id, ctx, {
2486
2506
  const mutations = [];
2487
2507
  const afterHooks = [];
2488
2508
  const mutationHook = ctx2.mutationHook;
2489
- const deleteCascade = async (currentModel, currentEntity, currentTrigger) => {
2509
+ const deleteCascade = async (currentModel, currentEntity, currentTrigger, args2) => {
2490
2510
  if (!(currentModel.name in toDelete)) {
2491
2511
  toDelete[currentModel.name] = {};
2492
2512
  }
@@ -2510,6 +2530,7 @@ var deleteEntity = async (modelName, id, ctx, {
2510
2530
  trigger: currentTrigger,
2511
2531
  when: "before",
2512
2532
  data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
2533
+ args: args2,
2513
2534
  ctx: ctx2
2514
2535
  });
2515
2536
  });
@@ -2525,6 +2546,7 @@ var deleteEntity = async (modelName, id, ctx, {
2525
2546
  trigger: currentTrigger,
2526
2547
  when: "after",
2527
2548
  data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
2549
+ args: args2,
2528
2550
  ctx: ctx2
2529
2551
  });
2530
2552
  });
@@ -2565,6 +2587,7 @@ var deleteEntity = async (modelName, id, ctx, {
2565
2587
  trigger: "set-null",
2566
2588
  when: "before",
2567
2589
  data: { prev: descendant, input: {}, normalizedInput, next: { ...descendant, ...normalizedInput } },
2590
+ args: args2,
2568
2591
  ctx: ctx2
2569
2592
  });
2570
2593
  });
@@ -2580,6 +2603,7 @@ var deleteEntity = async (modelName, id, ctx, {
2580
2603
  trigger: "set-null",
2581
2604
  when: "after",
2582
2605
  data: { prev: descendant, input: {}, normalizedInput, next: { ...descendant, ...normalizedInput } },
2606
+ args: args2,
2583
2607
  ctx: ctx2
2584
2608
  });
2585
2609
  });
@@ -2646,7 +2670,7 @@ var deleteEntity = async (modelName, id, ctx, {
2646
2670
  });
2647
2671
  }
2648
2672
  });
2649
- var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withTransaction(ctx, async (ctx2) => {
2673
+ var restoreEntity = async (modelName, id, ctx, { args: args2, trigger = "direct-call" } = {}) => withTransaction(ctx, async (ctx2) => {
2650
2674
  const model = ctx2.models.getModel(modelName, "entity");
2651
2675
  const rootModel = model.rootModel;
2652
2676
  const entity = await getEntityToMutate(ctx2, rootModel, { id }, "RESTORE");
@@ -2691,6 +2715,7 @@ var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withT
2691
2715
  trigger: currentTrigger,
2692
2716
  when: "before",
2693
2717
  data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
2718
+ args: args2,
2694
2719
  ctx: ctx2
2695
2720
  });
2696
2721
  });
@@ -2721,6 +2746,7 @@ var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withT
2721
2746
  trigger: currentTrigger,
2722
2747
  when: "after",
2723
2748
  data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
2749
+ args: args2,
2724
2750
  ctx: ctx2
2725
2751
  });
2726
2752
  });
@@ -3903,11 +3929,7 @@ var MigrationGenerator = class _MigrationGenerator {
3903
3929
  }
3904
3930
  return -1;
3905
3931
  }
3906
- static TRIGGER_EVENT_ORDER = [
3907
- "INSERT",
3908
- "UPDATE",
3909
- "DELETE"
3910
- ];
3932
+ static TRIGGER_EVENT_ORDER = ["INSERT", "UPDATE", "DELETE"];
3911
3933
  static sortTriggerEvents(events) {
3912
3934
  return [...events].sort(
3913
3935
  (a, b) => _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) - _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b)
@@ -3915,7 +3937,9 @@ var MigrationGenerator = class _MigrationGenerator {
3915
3937
  }
3916
3938
  normalizeTriggerDef(def) {
3917
3939
  let s = def.replace(/\s+/g, " ").replace(/\s*\(\s*/g, "(").replace(/\s*\)\s*/g, ")").replace(/\bON\s+[a-zA-Z_][a-zA-Z0-9_]*\./gi, "ON ").trim();
3918
- const eventsMatch = s.match(/\b(AFTER|BEFORE)\s+((?:INSERT|UPDATE|DELETE)(?:\s+OR\s+(?:INSERT|UPDATE|DELETE))+)\s+ON\b/i);
3940
+ const eventsMatch = s.match(
3941
+ /\b(AFTER|BEFORE)\s+((?:INSERT|UPDATE|DELETE)(?:\s+OR\s+(?:INSERT|UPDATE|DELETE))+)\s+ON\b/i
3942
+ );
3919
3943
  if (eventsMatch) {
3920
3944
  const events = eventsMatch[2].split(/\s+OR\s+/).map((e) => e.toUpperCase());
3921
3945
  const sorted = [...events].sort(
@@ -4955,7 +4979,8 @@ var generateDefinitions = ({
4955
4979
  {
4956
4980
  name: "dryRun",
4957
4981
  type: "Boolean"
4958
- }
4982
+ },
4983
+ ...model.deletable && model.deletable !== true && model.deletable.args || []
4959
4984
  ]
4960
4985
  });
4961
4986
  mutations2.push({
@@ -4967,7 +4992,8 @@ var generateDefinitions = ({
4967
4992
  name: "where",
4968
4993
  type: `${model.name}WhereUnique`,
4969
4994
  nonNull: true
4970
- }
4995
+ },
4996
+ ...model.deletable && model.deletable !== true && model.deletable.restoreArgs || []
4971
4997
  ]
4972
4998
  });
4973
4999
  }
@@ -1,18 +1,44 @@
1
1
  import upperCase from 'lodash/upperCase';
2
2
  import { isRootModel, not } from '..';
3
3
  const constantCase = (str) => upperCase(str).replace(/ /g, '_');
4
+ const argTypeStr = (field) => {
5
+ const base = field.list ? `[${field.type}!]` : field.type;
6
+ return field.nonNull ? `${base}!` : base;
7
+ };
8
+ const argsToVariables = (args) => args.map((a) => `$${a.name}: ${argTypeStr(a)}`).join(', ');
9
+ const argsToMutationArgs = (args) => args.map((a) => `${a.name}: $${a.name}`).join(', ');
4
10
  export const generateMutations = (models) => {
5
11
  const parts = [];
6
12
  for (const { name, creatable, updatable, deletable } of models.entities.filter(not(isRootModel))) {
7
13
  if (creatable) {
8
- parts.push(`export const CREATE_${constantCase(name)} = gql\`\n mutation Create${name}Mutation($data: Create${name}!) {\n create${name}(data: $data) { id }\n }\n\`;`);
14
+ const extraArgs = creatable !== true && creatable.args ? creatable.args : [];
15
+ const variables = extraArgs.length ? `$data: Create${name}!, ${argsToVariables(extraArgs)}` : `$data: Create${name}!`;
16
+ const mutationArgs = extraArgs.length ? `data: $data, ${argsToMutationArgs(extraArgs)}` : `data: $data`;
17
+ parts.push(`export const CREATE_${constantCase(name)} = gql\`\n mutation Create${name}Mutation(${variables}) {\n create${name}(${mutationArgs}) { id }\n }\n\`;`);
9
18
  }
10
19
  if (updatable) {
11
- parts.push(`export const UPDATE_${constantCase(name)} = gql\`\n mutation Update${name}Mutation($id: ID!, $data: Update${name}!) {\n update${name}(where: { id: $id }, data: $data) { id }\n }\n\`;`);
20
+ const extraArgs = updatable !== true && updatable.args ? updatable.args : [];
21
+ const variables = extraArgs.length
22
+ ? `$id: ID!, $data: Update${name}!, ${argsToVariables(extraArgs)}`
23
+ : `$id: ID!, $data: Update${name}!`;
24
+ const mutationArgs = extraArgs.length
25
+ ? `where: { id: $id }, data: $data, ${argsToMutationArgs(extraArgs)}`
26
+ : `where: { id: $id }, data: $data`;
27
+ parts.push(`export const UPDATE_${constantCase(name)} = gql\`\n mutation Update${name}Mutation(${variables}) {\n update${name}(${mutationArgs}) { id }\n }\n\`;`);
12
28
  }
13
29
  if (deletable) {
14
- parts.push(`export const DELETE_${constantCase(name)} = gql\`\n mutation Delete${name}Mutation($id: ID!) {\n delete${name}(where: { id: $id })\n }\n\`;`);
15
- parts.push(`export const RESTORE_${constantCase(name)} = gql\`\n mutation Restore${name}Mutation($id: ID!) {\n restore${name}(where: { id: $id })\n }\n\`;`);
30
+ const deleteExtraArgs = deletable !== true && deletable.args ? deletable.args : [];
31
+ const deleteVariables = deleteExtraArgs.length ? `$id: ID!, ${argsToVariables(deleteExtraArgs)}` : `$id: ID!`;
32
+ const deleteMutationArgs = deleteExtraArgs.length
33
+ ? `where: { id: $id }, ${argsToMutationArgs(deleteExtraArgs)}`
34
+ : `where: { id: $id }`;
35
+ parts.push(`export const DELETE_${constantCase(name)} = gql\`\n mutation Delete${name}Mutation(${deleteVariables}) {\n delete${name}(${deleteMutationArgs})\n }\n\`;`);
36
+ const restoreExtraArgs = deletable !== true && deletable.restoreArgs ? deletable.restoreArgs : [];
37
+ const restoreVariables = restoreExtraArgs.length ? `$id: ID!, ${argsToVariables(restoreExtraArgs)}` : `$id: ID!`;
38
+ const restoreMutationArgs = restoreExtraArgs.length
39
+ ? `where: { id: $id }, ${argsToMutationArgs(restoreExtraArgs)}`
40
+ : `where: { id: $id }`;
41
+ parts.push(`export const RESTORE_${constantCase(name)} = gql\`\n mutation Restore${name}Mutation(${restoreVariables}) {\n restore${name}(${restoreMutationArgs})\n }\n\`;`);
16
42
  }
17
43
  }
18
44
  return `import { gql } from "./gql";\n\n${parts.join('\n\n')}`;
@@ -1 +1 @@
1
- {"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../../src/client/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC;AAGtC,MAAM,YAAY,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,OAAO,C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CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;YAC9G,MAAM,kBAAkB,GAAG,eAAe,CAAC,MAAM;gBAC/C,CAAC,CAAC,uBAAuB,kBAAkB,CAAC,eAAe,CAAC,EAAE;gBAC9D,CAAC,CAAC,oBAAoB,CAAC;YACzB,KAAK,CAAC,IAAI,CACR,uBAAuB,YAAY,CACjC,IAAI,CACL,8BAA8B,IAAI,YAAY,eAAe,kBAAkB,IAAI,IAAI,kBAAkB,aAAa,CACxH,CAAC;YAEF,MAAM,gBAAgB,GAAG,SAAS,KAAK,IAAI,IAAI,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC;YAClG,MAAM,gBAAgB,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,aAAa,eAAe,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;YACjH,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,MAAM;gBACjD,CAAC,CAAC,uBAAuB,kBAAkB,CAAC,gBAAgB,CAAC,EAAE;gBAC/D,CAAC,CAAC,oBAAoB,CAAC;YACzB,KAAK,CAAC,IAAI,CACR,wBAAwB,YAAY,CAClC,IAAI,CACL,+BAA+B,IAAI,YAAY,gBAAgB,mBAAmB,IAAI,IAAI,mBAAmB,aAAa,CAC5H,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,mCAAmC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;AACjE,CAAC,CAAC"}
@@ -801,14 +801,9 @@ export class MigrationGenerator {
801
801
  }
802
802
  return -1;
803
803
  }
804
- static TRIGGER_EVENT_ORDER = [
805
- 'INSERT',
806
- 'UPDATE',
807
- 'DELETE',
808
- ];
804
+ static TRIGGER_EVENT_ORDER = ['INSERT', 'UPDATE', 'DELETE'];
809
805
  static sortTriggerEvents(events) {
810
- return [...events].sort((a, b) => MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) -
811
- MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b));
806
+ return [...events].sort((a, b) => MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) - MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b));
812
807
  }
813
808
  normalizeTriggerDef(def) {
814
809
  let s = def