@smartive/graphql-magic 23.10.0 → 23.11.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/.github/workflows/docs.yml +1 -1
- package/.github/workflows/release.yml +1 -1
- package/CHANGELOG.md +2 -2
- package/dist/bin/gqm.cjs +38 -18
- package/dist/cjs/index.cjs +63 -30
- package/dist/esm/client/mutations.js +30 -4
- package/dist/esm/client/mutations.js.map +1 -1
- package/dist/esm/migrations/generate.js +2 -7
- package/dist/esm/migrations/generate.js.map +1 -1
- package/dist/esm/models/model-definitions.d.ts +4 -0
- package/dist/esm/models/models.d.ts +4 -0
- package/dist/esm/models/models.js.map +1 -1
- package/dist/esm/models/mutation-hook.d.ts +1 -0
- package/dist/esm/resolvers/mutations.d.ts +18 -6
- package/dist/esm/resolvers/mutations.js +22 -10
- package/dist/esm/resolvers/mutations.js.map +1 -1
- package/dist/esm/schema/generate.js +4 -0
- package/dist/esm/schema/generate.js.map +1 -1
- package/docker-compose.yml +1 -1
- package/docs/package-lock.json +3423 -2391
- package/package.json +1 -1
- package/src/client/mutations.ts +35 -4
- package/src/migrations/generate.ts +8 -15
- package/src/models/model-definitions.ts +16 -2
- package/src/models/models.ts +16 -2
- package/src/models/mutation-hook.ts +1 -0
- package/src/resolvers/mutations.ts +33 -8
- package/src/schema/generate.ts +4 -0
|
@@ -13,7 +13,7 @@ jobs:
|
|
|
13
13
|
persist-credentials: false
|
|
14
14
|
|
|
15
15
|
- name: Setup Node
|
|
16
|
-
uses: actions/setup-node@
|
|
16
|
+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
|
|
17
17
|
with:
|
|
18
18
|
node-version: '24'
|
|
19
19
|
- name: Install and Build
|
|
@@ -22,7 +22,7 @@ jobs:
|
|
|
22
22
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
|
|
23
23
|
with:
|
|
24
24
|
submodules: true
|
|
25
|
-
- uses: actions/setup-node@
|
|
25
|
+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
|
|
26
26
|
with:
|
|
27
27
|
node-version: 24
|
|
28
28
|
registry-url: 'https://registry.npmjs.org'
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
## [23.
|
|
1
|
+
## [23.11.0](https://github.com/smartive/graphql-magic/compare/v23.10.0...v23.11.0) (2026-03-19)
|
|
2
2
|
|
|
3
3
|
### Features
|
|
4
4
|
|
|
5
|
-
*
|
|
5
|
+
* Enable mutation args ([#442](https://github.com/smartive/graphql-magic/issues/442)) ([37a5e6e](https://github.com/smartive/graphql-magic/commit/37a5e6ebff67d7284015d9bb60695f2c107a6c88))
|
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($
|
|
53
|
-
create${name2}(
|
|
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($
|
|
64
|
-
update${name2}(
|
|
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($
|
|
75
|
-
delete${name2}(
|
|
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($
|
|
84
|
-
restore${name2}(
|
|
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(
|
|
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(
|
|
@@ -2804,7 +2820,8 @@ var generateDefinitions = ({
|
|
|
2804
2820
|
name: "data",
|
|
2805
2821
|
type: `Create${model.name}`,
|
|
2806
2822
|
nonNull: true
|
|
2807
|
-
}
|
|
2823
|
+
},
|
|
2824
|
+
...model.creatable && model.creatable !== true && model.creatable.args || []
|
|
2808
2825
|
]
|
|
2809
2826
|
});
|
|
2810
2827
|
}
|
|
@@ -2823,7 +2840,8 @@ var generateDefinitions = ({
|
|
|
2823
2840
|
name: "data",
|
|
2824
2841
|
type: `Update${model.name}`,
|
|
2825
2842
|
nonNull: true
|
|
2826
|
-
}
|
|
2843
|
+
},
|
|
2844
|
+
...model.updatable && model.updatable !== true && model.updatable.args || []
|
|
2827
2845
|
]
|
|
2828
2846
|
});
|
|
2829
2847
|
}
|
|
@@ -2841,7 +2859,8 @@ var generateDefinitions = ({
|
|
|
2841
2859
|
{
|
|
2842
2860
|
name: "dryRun",
|
|
2843
2861
|
type: "Boolean"
|
|
2844
|
-
}
|
|
2862
|
+
},
|
|
2863
|
+
...model.deletable && model.deletable !== true && model.deletable.args || []
|
|
2845
2864
|
]
|
|
2846
2865
|
});
|
|
2847
2866
|
mutations2.push({
|
|
@@ -2853,7 +2872,8 @@ var generateDefinitions = ({
|
|
|
2853
2872
|
name: "where",
|
|
2854
2873
|
type: `${model.name}WhereUnique`,
|
|
2855
2874
|
nonNull: true
|
|
2856
|
-
}
|
|
2875
|
+
},
|
|
2876
|
+
...model.deletable && model.deletable !== true && model.deletable.restoreArgs || []
|
|
2857
2877
|
]
|
|
2858
2878
|
});
|
|
2859
2879
|
}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -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($
|
|
319
|
-
create${name2}(
|
|
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($
|
|
330
|
-
update${name2}(
|
|
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($
|
|
341
|
-
delete${name2}(
|
|
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($
|
|
350
|
-
restore${name2}(
|
|
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, "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, "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,6 +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 },
|
|
2406
|
+
args: args2,
|
|
2387
2407
|
ctx: ctx2
|
|
2388
2408
|
});
|
|
2389
2409
|
if (model.parent) {
|
|
@@ -2416,17 +2436,18 @@ var createEntity = async (modelName, input2, ctx, trigger = "direct-call") => wi
|
|
|
2416
2436
|
trigger,
|
|
2417
2437
|
when: "after",
|
|
2418
2438
|
data: { prev: {}, input: input2, normalizedInput, next: normalizedInput },
|
|
2439
|
+
args: args2,
|
|
2419
2440
|
ctx: ctx2
|
|
2420
2441
|
});
|
|
2421
2442
|
return normalizedInput.id;
|
|
2422
2443
|
});
|
|
2423
|
-
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) => {
|
|
2424
2445
|
const entities = await ctx2.knex(modelName).where(where).select("id");
|
|
2425
2446
|
for (const entity of entities) {
|
|
2426
|
-
await updateEntity(modelName, entity.id, updateFields, ctx2);
|
|
2447
|
+
await updateEntity(modelName, entity.id, updateFields, ctx2, args2);
|
|
2427
2448
|
}
|
|
2428
2449
|
});
|
|
2429
|
-
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) => {
|
|
2430
2451
|
const model = ctx2.models.getModel(modelName, "entity");
|
|
2431
2452
|
const normalizedInput = { ...input2 };
|
|
2432
2453
|
sanitize(ctx2, model, normalizedInput);
|
|
@@ -2444,6 +2465,7 @@ var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") =
|
|
|
2444
2465
|
trigger,
|
|
2445
2466
|
when: "before",
|
|
2446
2467
|
data: { prev: currentEntity, input: input2, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2468
|
+
args: args2,
|
|
2447
2469
|
ctx: ctx2
|
|
2448
2470
|
});
|
|
2449
2471
|
await doUpdate(model, currentEntity, normalizedInput, ctx2);
|
|
@@ -2453,21 +2475,24 @@ var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") =
|
|
|
2453
2475
|
trigger,
|
|
2454
2476
|
when: "after",
|
|
2455
2477
|
data: { prev: currentEntity, input: input2, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2478
|
+
args: args2,
|
|
2456
2479
|
ctx: ctx2
|
|
2457
2480
|
});
|
|
2458
2481
|
}
|
|
2459
2482
|
});
|
|
2460
|
-
var deleteEntities = async (modelName, where, ctx) => withTransaction(ctx, async (ctx2) => {
|
|
2483
|
+
var deleteEntities = async (modelName, where, ctx, args2) => withTransaction(ctx, async (ctx2) => {
|
|
2461
2484
|
const entities = await ctx2.knex(modelName).where(where).select("id");
|
|
2462
2485
|
for (const entity of entities) {
|
|
2463
2486
|
await deleteEntity(modelName, entity.id, ctx2, {
|
|
2464
|
-
trigger: "direct-call"
|
|
2487
|
+
trigger: "direct-call",
|
|
2488
|
+
args: args2
|
|
2465
2489
|
});
|
|
2466
2490
|
}
|
|
2467
2491
|
});
|
|
2468
2492
|
var deleteEntity = async (modelName, id, ctx, {
|
|
2469
2493
|
dryRun = false,
|
|
2470
|
-
trigger = "direct-call"
|
|
2494
|
+
trigger = "direct-call",
|
|
2495
|
+
args: args2
|
|
2471
2496
|
} = {}) => withTransaction(ctx, async (ctx2) => {
|
|
2472
2497
|
const model = ctx2.models.getModel(modelName, "entity");
|
|
2473
2498
|
const rootModel = model.rootModel;
|
|
@@ -2483,6 +2508,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2483
2508
|
const afterHooks = [];
|
|
2484
2509
|
const mutationHook = ctx2.mutationHook;
|
|
2485
2510
|
const deleteCascade = async (currentModel, currentEntity, currentTrigger) => {
|
|
2511
|
+
const isDeleteRoot = currentModel.name === rootModel.name && currentEntity.id === entity.id;
|
|
2486
2512
|
if (!(currentModel.name in toDelete)) {
|
|
2487
2513
|
toDelete[currentModel.name] = {};
|
|
2488
2514
|
}
|
|
@@ -2506,6 +2532,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2506
2532
|
trigger: currentTrigger,
|
|
2507
2533
|
when: "before",
|
|
2508
2534
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2535
|
+
...isDeleteRoot ? { args: args2 } : {},
|
|
2509
2536
|
ctx: ctx2
|
|
2510
2537
|
});
|
|
2511
2538
|
});
|
|
@@ -2521,6 +2548,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2521
2548
|
trigger: currentTrigger,
|
|
2522
2549
|
when: "after",
|
|
2523
2550
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2551
|
+
...isDeleteRoot ? { args: args2 } : {},
|
|
2524
2552
|
ctx: ctx2
|
|
2525
2553
|
});
|
|
2526
2554
|
});
|
|
@@ -2642,7 +2670,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2642
2670
|
});
|
|
2643
2671
|
}
|
|
2644
2672
|
});
|
|
2645
|
-
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) => {
|
|
2646
2674
|
const model = ctx2.models.getModel(modelName, "entity");
|
|
2647
2675
|
const rootModel = model.rootModel;
|
|
2648
2676
|
const entity = await getEntityToMutate(ctx2, rootModel, { id }, "RESTORE");
|
|
@@ -2661,6 +2689,7 @@ var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withT
|
|
|
2661
2689
|
const mutations = [];
|
|
2662
2690
|
const afterHooks = [];
|
|
2663
2691
|
const restoreCascade = async (currentModel, currentEntity, currentTrigger) => {
|
|
2692
|
+
const isRestoreRoot = currentModel.name === rootModel.name && currentEntity.id === entity.id;
|
|
2664
2693
|
if (entity.deleteRootId || currentEntity.deleteRootId) {
|
|
2665
2694
|
if (!(currentEntity.deleteRootType === model.name && currentEntity.deleteRootId === entity.id)) {
|
|
2666
2695
|
return;
|
|
@@ -2687,6 +2716,7 @@ var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withT
|
|
|
2687
2716
|
trigger: currentTrigger,
|
|
2688
2717
|
when: "before",
|
|
2689
2718
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2719
|
+
...isRestoreRoot ? { args: args2 } : {},
|
|
2690
2720
|
ctx: ctx2
|
|
2691
2721
|
});
|
|
2692
2722
|
});
|
|
@@ -2717,6 +2747,7 @@ var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withT
|
|
|
2717
2747
|
trigger: currentTrigger,
|
|
2718
2748
|
when: "after",
|
|
2719
2749
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2750
|
+
...isRestoreRoot ? { args: args2 } : {},
|
|
2720
2751
|
ctx: ctx2
|
|
2721
2752
|
});
|
|
2722
2753
|
});
|
|
@@ -3899,11 +3930,7 @@ var MigrationGenerator = class _MigrationGenerator {
|
|
|
3899
3930
|
}
|
|
3900
3931
|
return -1;
|
|
3901
3932
|
}
|
|
3902
|
-
static TRIGGER_EVENT_ORDER = [
|
|
3903
|
-
"INSERT",
|
|
3904
|
-
"UPDATE",
|
|
3905
|
-
"DELETE"
|
|
3906
|
-
];
|
|
3933
|
+
static TRIGGER_EVENT_ORDER = ["INSERT", "UPDATE", "DELETE"];
|
|
3907
3934
|
static sortTriggerEvents(events) {
|
|
3908
3935
|
return [...events].sort(
|
|
3909
3936
|
(a, b) => _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) - _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b)
|
|
@@ -3911,7 +3938,9 @@ var MigrationGenerator = class _MigrationGenerator {
|
|
|
3911
3938
|
}
|
|
3912
3939
|
normalizeTriggerDef(def) {
|
|
3913
3940
|
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();
|
|
3914
|
-
const eventsMatch = s.match(
|
|
3941
|
+
const eventsMatch = s.match(
|
|
3942
|
+
/\b(AFTER|BEFORE)\s+((?:INSERT|UPDATE|DELETE)(?:\s+OR\s+(?:INSERT|UPDATE|DELETE))+)\s+ON\b/i
|
|
3943
|
+
);
|
|
3915
3944
|
if (eventsMatch) {
|
|
3916
3945
|
const events = eventsMatch[2].split(/\s+OR\s+/).map((e) => e.toUpperCase());
|
|
3917
3946
|
const sorted = [...events].sort(
|
|
@@ -4912,7 +4941,8 @@ var generateDefinitions = ({
|
|
|
4912
4941
|
name: "data",
|
|
4913
4942
|
type: `Create${model.name}`,
|
|
4914
4943
|
nonNull: true
|
|
4915
|
-
}
|
|
4944
|
+
},
|
|
4945
|
+
...model.creatable && model.creatable !== true && model.creatable.args || []
|
|
4916
4946
|
]
|
|
4917
4947
|
});
|
|
4918
4948
|
}
|
|
@@ -4931,7 +4961,8 @@ var generateDefinitions = ({
|
|
|
4931
4961
|
name: "data",
|
|
4932
4962
|
type: `Update${model.name}`,
|
|
4933
4963
|
nonNull: true
|
|
4934
|
-
}
|
|
4964
|
+
},
|
|
4965
|
+
...model.updatable && model.updatable !== true && model.updatable.args || []
|
|
4935
4966
|
]
|
|
4936
4967
|
});
|
|
4937
4968
|
}
|
|
@@ -4949,7 +4980,8 @@ var generateDefinitions = ({
|
|
|
4949
4980
|
{
|
|
4950
4981
|
name: "dryRun",
|
|
4951
4982
|
type: "Boolean"
|
|
4952
|
-
}
|
|
4983
|
+
},
|
|
4984
|
+
...model.deletable && model.deletable !== true && model.deletable.args || []
|
|
4953
4985
|
]
|
|
4954
4986
|
});
|
|
4955
4987
|
mutations2.push({
|
|
@@ -4961,7 +4993,8 @@ var generateDefinitions = ({
|
|
|
4961
4993
|
name: "where",
|
|
4962
4994
|
type: `${model.name}WhereUnique`,
|
|
4963
4995
|
nonNull: true
|
|
4964
|
-
}
|
|
4996
|
+
},
|
|
4997
|
+
...model.deletable && model.deletable !== true && model.deletable.restoreArgs || []
|
|
4965
4998
|
]
|
|
4966
4999
|
});
|
|
4967
5000
|
}
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
15
|
-
|
|
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;
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../../src/client/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,kBAAkB,CAAC;AAEzC,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC;AAGtC,MAAM,YAAY,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AAExE,MAAM,UAAU,GAAG,CAAC,KAAY,EAAE,EAAE;IAClC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;IAE1D,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;AAC3C,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,CAAC,IAAsB,EAAE,E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C,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
|