Skip to content

Commit

Permalink
Fix confusing error when providing fields that don't exist to `ui.car…
Browse files Browse the repository at this point in the history
…dFields`, `ui.inlineCreate.fields` and `ui.inlineEdit.fields` in relationship field (#6045)

* Fix confusing error when providing fields that don't exist to `ui.cardFields`, `ui.inlineCreate.fields` and `ui.inlineEdit.fields`

* Fix some things

* words
  • Loading branch information
emmatown committed Jul 2, 2021
1 parent 890e3d0 commit 253df44
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/late-comics-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Adjusted when `getAdminMeta` is called on fields so that they can see the metadata (excluding the results of `getAdminMeta` on fields) of other fields to do validation or etc.
5 changes: 5 additions & 0 deletions .changeset/nasty-pugs-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/fields': patch
---

Fixed confusing error when providing fields that don't exist to `ui.cardFields`, `ui.inlineCreate.fields` and `ui.inlineEdit.fields`.
27 changes: 27 additions & 0 deletions packages-next/fields/src/types/relationship/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ export const relationship =
`The ref [${ref}] on relationship [${meta.listKey}.${meta.fieldKey}] is invalid`
);
}
if (config.ui?.displayMode === 'cards') {
// we're checking whether the field which will be in the admin meta at the time that getAdminMeta is called.
// in newer versions of keystone, it will be there and it will not be there for older versions of keystone.
// this is so that relationship fields doesn't break in confusing ways
// if people are using a slightly older version of keystone
const currentField = adminMetaRoot.listsByKey[meta.listKey].fields.find(
x => x.path === meta.fieldKey
);
if (currentField) {
const allForeignFields = new Set(
adminMetaRoot.listsByKey[foreignListKey].fields.map(x => x.path)
);
for (const [configOption, foreignFields] of [
['ui.cardFields', config.ui.cardFields],
['ui.inlineCreate.fields', config.ui.inlineCreate?.fields ?? []],
['ui.inlineEdit.fields', config.ui.inlineEdit?.fields ?? []],
] as const) {
for (const foreignField of foreignFields) {
if (!allForeignFields.has(foreignField)) {
throw new Error(
`The ${configOption} option on the relationship field at ${meta.listKey}.${meta.fieldKey} includes the "${foreignField}" field but that field does not exist on the "${foreignListKey}" list`
);
}
}
}
}
}
return {
refListKey: foreignListKey,
many,
Expand Down
11 changes: 10 additions & 1 deletion packages-next/keystone/src/admin-ui/system/createAdminMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,22 @@ export function createAdminMeta(
label: field.label ?? humanize(fieldKey),
viewsIndex: getViewId(field.views),
customViewsIndex: field.ui?.views === undefined ? null : getViewId(field.ui.views),
fieldMeta: field.getAdminMeta?.(adminMetaRoot) ?? null,
fieldMeta: null,
isOrderable: !!field.input?.orderBy,
path: fieldKey,
listKey: key,
});
}
}

// we do this seperately to the above so that fields can check other fields to validate their config or etc.
// (ofc they won't necessarily be able to see other field's fieldMeta)
for (const [key, list] of Object.entries(initialisedLists)) {
for (const fieldMetaRootVal of adminMetaRoot.listsByKey[key].fields) {
fieldMetaRootVal.fieldMeta =
list.fields[fieldMetaRootVal.path].getAdminMeta?.(adminMetaRoot) ?? null;
}
}

return adminMetaRoot;
}

1 comment on commit 253df44

@vercel
Copy link

@vercel vercel bot commented on 253df44 Jul 2, 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.