/
nested-mutation-many-input-resolvers.ts
137 lines (121 loc) · 4.31 KB
/
nested-mutation-many-input-resolvers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { KeystoneContext, TypesForList, schema } from '@keystone-next/types';
import { resolveUniqueWhereInput, UniqueInputFilter, UniquePrismaFilter } from '../where-inputs';
import { InitialisedList } from '../types-for-lists';
import { isRejected, isFulfilled } from '../utils';
import { NestedMutationState } from './create-update';
const isNotNull = <T>(arg: T): arg is Exclude<T, null> => arg !== null;
type _CreateValueType = Exclude<
schema.InferValueFromArg<schema.Arg<TypesForList['relateTo']['many']['create']>>,
null | undefined
>;
type _UpdateValueType = Exclude<
schema.InferValueFromArg<schema.Arg<TypesForList['relateTo']['many']['update']>>,
null | undefined
>;
async function getDisconnects(
uniqueInputs: (UniqueInputFilter | null)[],
context: KeystoneContext,
foreignList: InitialisedList
): Promise<UniquePrismaFilter[]> {
return (
await Promise.all(
uniqueInputs.map(async uniqueInput => {
if (uniqueInput === null) return [];
let uniqueWhere;
try {
// Validate and resolve the input filter
uniqueWhere = await resolveUniqueWhereInput(uniqueInput, foreignList.fields, context);
// Check whether the item exists
await context.sudo().db.lists[foreignList.listKey].findOne({ where: uniqueInput });
} catch (err) {
return [];
}
return [uniqueWhere];
})
)
).flat();
}
function getConnects(
uniqueInputs: UniqueInputFilter[],
context: KeystoneContext,
foreignList: InitialisedList
): Promise<UniquePrismaFilter>[] {
return uniqueInputs.map(async uniqueInput => {
// Validate and resolve the input filter
const uniqueWhere = await resolveUniqueWhereInput(uniqueInput, foreignList.fields, context);
// Check whether the item exists
await context.db.lists[foreignList.listKey].findOne({ where: uniqueInput });
return uniqueWhere;
});
}
async function resolveCreateAndConnect(
value: _UpdateValueType,
nestedMutationState: NestedMutationState,
context: KeystoneContext,
foreignList: InitialisedList,
target: string
) {
// Perform queries for the connections
const connects = Promise.allSettled(
getConnects((value.connect || []).filter(isNotNull), context, foreignList)
);
// Perform nested mutations for the creations
const creates = Promise.allSettled(
(value.create || []).filter(isNotNull).map(x => nestedMutationState.create(x, foreignList))
);
const [connectResult, createResult] = await Promise.all([connects, creates]);
// Collect all the errors
const errors = [...connectResult.filter(isRejected), ...createResult.filter(isRejected)].map(
x => x.reason
);
if (errors.length) {
throw new Error(`Unable to create and/or connect ${errors.length} ${target}`);
}
const result = { connect: connectResult.filter(isFulfilled).map(x => x.value) };
for (const createData of createResult.filter(isFulfilled).map(x => x.value)) {
result.connect.push({ id: createData.id });
}
// Perform queries for the connections
return result;
}
function assertValidManyOperation(val: _UpdateValueType, target: string) {
if (
!Array.isArray(val.connect) &&
!Array.isArray(val.create) &&
!Array.isArray(val.disconnect) &&
!val.disconnectAll
) {
throw new Error(`Nested mutation operation invalid for ${target}`);
}
}
export function resolveRelateToManyForCreateInput(
nestedMutationState: NestedMutationState,
context: KeystoneContext,
foreignList: InitialisedList,
target: string
) {
return async (value: _CreateValueType) => {
assertValidManyOperation(value, target);
return resolveCreateAndConnect(value, nestedMutationState, context, foreignList, target);
};
}
export function resolveRelateToManyForUpdateInput(
nestedMutationState: NestedMutationState,
context: KeystoneContext,
foreignList: InitialisedList,
target: string
) {
return async (value: _UpdateValueType) => {
assertValidManyOperation(value, target);
const disconnects = getDisconnects(
value.disconnectAll ? [] : value.disconnect || [],
context,
foreignList
);
const [disconnect, connect] = await Promise.all([
disconnects,
resolveCreateAndConnect(value, nestedMutationState, context, foreignList, target),
]);
return { set: value.disconnectAll ? [] : undefined, disconnect, ...connect };
};
}