Skip to content

Commit

Permalink
Simplify NestedMutationState (#6123)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Jul 16, 2021
1 parent 597edbd commit 32e9879
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-lizards-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Simplified the internal implementation of nested mutations.
8 changes: 3 additions & 5 deletions packages/keystone/src/lib/core/mutations/create-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ export class NestedMutationState {
constructor(context: KeystoneContext) {
this.#context = context;
}
async create(
input: Record<string, any>,
list: InitialisedList
): Promise<{ kind: 'connect'; id: IdType } | { kind: 'create'; data: Record<string, any> }> {
async create(input: Record<string, any>, list: InitialisedList) {
const { afterChange, data } = await createOneState({ data: input }, list, this.#context);

const item = await getPrismaModelForList(this.#context.prisma, list.listKey).create({ data });

this.#afterChanges.push(() => afterChange(item));
return { kind: 'connect' as const, id: item.id as any };
return { id: item.id as IdType };
}

async afterChange() {
await promiseAllRejectWithAllErrors(this.#afterChanges.map(async x => x()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,9 @@ async function resolveCreateAndConnect(
throw new Error(`Unable to create and/or connect ${errors.length} ${target}`);
}

const result = {
connect: connectResult.filter(isFulfilled).map(x => x.value),
create: [] as Record<string, any>[],
};

const result = { connect: connectResult.filter(isFulfilled).map(x => x.value) };
for (const createData of createResult.filter(isFulfilled).map(x => x.value)) {
if (createData.kind === 'create') {
result.create.push(createData.data);
} else if (createData.kind === 'connect') {
result.connect.push({ id: createData.id });
}
result.connect.push({ id: createData.id });
}

// Perform queries for the connections
Expand Down Expand Up @@ -135,15 +127,11 @@ export function resolveRelateToManyForUpdateInput(
foreignList
);

const [disconnect, connectAndCreates] = await Promise.all([
const [disconnect, connect] = await Promise.all([
disconnects,
resolveCreateAndConnect(value, nestedMutationState, context, foreignList, target),
]);

return {
set: value.disconnectAll ? [] : undefined,
disconnect,
...connectAndCreates,
};
return { set: value.disconnectAll ? [] : undefined, disconnect, ...connect };
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ async function handleCreateAndUpdate(
}
})();

if (create.kind === 'connect') {
return { connect: { id: create.id } };
} else {
return { create: create.data };
}
return { connect: { id: create.id } };
}
}

Expand Down

1 comment on commit 32e9879

@vercel
Copy link

@vercel vercel bot commented on 32e9879 Jul 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.