diff --git a/.changeset/wild-lizards-tie.md b/.changeset/wild-lizards-tie.md new file mode 100644 index 00000000000..32379d9e6ea --- /dev/null +++ b/.changeset/wild-lizards-tie.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/keystone': patch +--- + +Simplified the internal implementation of nested mutations. diff --git a/packages/keystone/src/lib/core/mutations/create-update.ts b/packages/keystone/src/lib/core/mutations/create-update.ts index 5d164204a5f..9fd8b0cebe8 100644 --- a/packages/keystone/src/lib/core/mutations/create-update.ts +++ b/packages/keystone/src/lib/core/mutations/create-update.ts @@ -26,17 +26,15 @@ export class NestedMutationState { constructor(context: KeystoneContext) { this.#context = context; } - async create( - input: Record, - list: InitialisedList - ): Promise<{ kind: 'connect'; id: IdType } | { kind: 'create'; data: Record }> { + async create(input: Record, 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())); } diff --git a/packages/keystone/src/lib/core/mutations/nested-mutation-many-input-resolvers.ts b/packages/keystone/src/lib/core/mutations/nested-mutation-many-input-resolvers.ts index f819b1a47fe..3cdda6a03e3 100644 --- a/packages/keystone/src/lib/core/mutations/nested-mutation-many-input-resolvers.ts +++ b/packages/keystone/src/lib/core/mutations/nested-mutation-many-input-resolvers.ts @@ -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[], - }; - + 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 @@ -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 }; }; } diff --git a/packages/keystone/src/lib/core/mutations/nested-mutation-one-input-resolvers.ts b/packages/keystone/src/lib/core/mutations/nested-mutation-one-input-resolvers.ts index 3e4d97b7e9b..6d5c37fa0b6 100644 --- a/packages/keystone/src/lib/core/mutations/nested-mutation-one-input-resolvers.ts +++ b/packages/keystone/src/lib/core/mutations/nested-mutation-one-input-resolvers.ts @@ -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 } }; } }