Skip to content

Commit 3df8831

Browse files
committed
updateGeiCrudUpdate
1 parent d19682e commit 3df8831

File tree

1 file changed

+17
-8
lines changed
  • packages/integrations/gei-crud/src/Mutation

1 file changed

+17
-8
lines changed

packages/integrations/gei-crud/src/Mutation/update.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ export const handler = async (input: FieldResolveInput) =>
77
const _id = prepare_id(input) || (prepareSourceParameters(input)._id as string);
88
if (!_id) throw new Error('_id not found');
99
const entries = Object.entries(input.arguments || {});
10-
if (entries.length !== 2 && entries.length !== 1) {
11-
throw new Error(
12-
'There should be only 2 arguments one "String" _id argument and one of "input" type to update this model',
13-
);
14-
}
15-
const setter = entries.find((e) => e[0] !== '_id');
16-
if (!setter) {
10+
const reconstructedObject: Record<string, any> = {};
11+
12+
const entriesWithOutId = entries.filter((e) => e[0] !== '_id');
13+
if (!entriesWithOutId) {
1714
throw new Error(`You need update input argument for this resolver to work`);
1815
}
16+
17+
if (typeof entriesWithOutId[0][1] ==='object' ) {
18+
if(entriesWithOutId[1]) throw new Error(
19+
'There should be only string arguments or _id argument and one argument of "input" type to update this model',
20+
)
21+
} else{
22+
(entriesWithOutId as [string, any][]).forEach((entry: [string, any]) => {
23+
const [key, value] = entry;
24+
reconstructedObject[key] = value;
25+
});
26+
}
27+
const setter = typeof entriesWithOutId[0][1] ==='object' ? entriesWithOutId[0][1] : reconstructedObject
1928
const filterInput: Record<string, any> = { _id, ...prepareSourceParameters(input) };
2029
const res = await db
2130
.collection(prepareModel(input))
22-
.updateOne(filterInput, { $set: { ...(setter[1] as object), updatedAt: new Date().toISOString() } })
31+
.updateOne(filterInput, { $set: { ...(setter), updatedAt: new Date().toISOString() } })
2332
if(res.matchedCount<1) throw new Error(`Object for update not found. Please check parameters: ${ JSON.stringify(filterInput)}`)
2433
return res.modifiedCount>=1
2534
});

0 commit comments

Comments
 (0)