From 1cbcf54cb1206461866b582865e3b1a8fc728f18 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Mon, 16 Aug 2021 11:03:15 +1000 Subject: [PATCH] Split create/update field input resolvers for relationship fields (#6317) --- .changeset/brown-plums-kneel.md | 5 ++++ .../src/lib/core/mutations/create-update.ts | 23 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 .changeset/brown-plums-kneel.md diff --git a/.changeset/brown-plums-kneel.md b/.changeset/brown-plums-kneel.md new file mode 100644 index 00000000000..0e83d05832e --- /dev/null +++ b/.changeset/brown-plums-kneel.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/keystone': patch +--- + +Separated the resolving of non-relationship field from relationship fields in create/update inputs to allow for better error handling. diff --git a/packages/keystone/src/lib/core/mutations/create-update.ts b/packages/keystone/src/lib/core/mutations/create-update.ts index aa34bdef55c..289d34a9821 100644 --- a/packages/keystone/src/lib/core/mutations/create-update.ts +++ b/packages/keystone/src/lib/core/mutations/create-update.ts @@ -180,21 +180,32 @@ async function getResolvedData( ); } - // Apply field type input resolvers + // Apply non-relationship field type input resolvers resolvedData = Object.fromEntries( await promiseAllRejectWithAllErrors( Object.entries(list.fields).map(async ([fieldKey, field]) => { const inputResolver = field.input?.[operation]?.resolve; let input = resolvedData[fieldKey]; - if (inputResolver) { + if (inputResolver && field.dbField.kind !== 'relation') { + input = await inputResolver(input, context, undefined); + } + return [fieldKey, input] as const; + }) + ) + ); + + // Apply relationship field type input resolvers + resolvedData = Object.fromEntries( + await promiseAllRejectWithAllErrors( + Object.entries(list.fields).map(async ([fieldKey, field]) => { + const inputResolver = field.input?.[operation]?.resolve; + let input = resolvedData[fieldKey]; + if (inputResolver && field.dbField.kind === 'relation') { input = await inputResolver( input, context, + // This third argument only applies to relationship fields (() => { - // This third argument only applies to relationship fields - if (field.dbField.kind !== 'relation') { - return undefined; - } if (input === undefined) { // No-op: This is what we want return () => undefined;