Skip to content

Commit

Permalink
reduced relationship error verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Feb 26, 2024
1 parent 4ad977f commit 6c869f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
7 changes: 3 additions & 4 deletions packages/core/src/fields/types/relationship/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type CardsDisplayConfig = {
/** Causes the default Card component to render as a link to navigate to the related item */
linkToItem?: boolean
/** Determines whether removing a related item in the UI will delete or unlink it */
removeMode?: 'disconnect' | 'none' // | 'delete';
removeMode?: 'disconnect' | 'none' // | 'delete'
/** Configures inline create mode for cards (alternative to opening the create modal) */
inlineCreate?: { fields: readonly string[] }
/** Configures inline edit mode for cards */
Expand Down Expand Up @@ -91,9 +91,8 @@ export const relationship =
const { many = false } = config
const [foreignListKey, foreignFieldKey] = ref.split('.')
const foreignList = lists[foreignListKey]
if (!foreignList) {
throw new Error(`${listKey}.${fieldKey} points to ${ref}, but ${ref} doesn't exist`)
}
if (!foreignList) throw new Error(`${listKey}.${fieldKey} points to ${ref}, but ${ref} doesn't exist`)

const foreignListTypes = foreignList.types

const commonConfig = {
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/lib/core/resolve-relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,25 @@ export function resolveRelationships (
if (alreadyResolvedTwoSidedRelationships.has(localRef)) {
continue
}

alreadyResolvedTwoSidedRelationships.add(foreignRef)
const foreignField = foreignUnresolvedList.fields[field.field]?.dbField
if (!foreignField) {
throw new Error(`${localRef} points to ${foreignRef}, but ${foreignRef} doesn't exist`)
}
if (!foreignField) throw new Error(`${localRef} points to ${foreignRef}, but ${foreignRef} doesn't exist`)

if (foreignField.kind !== 'relation') {
throw new Error(
`${localRef} points to ${foreignRef}, but ${foreignRef} is not a relationship field`
)
}

const actualRef = foreignField.field
? `${foreignField.list}.${foreignField.field}`
: foreignField.list
const actualRef = foreignField.field ? `${foreignField.list}.${foreignField.field}` : foreignField.list
if (actualRef !== localRef) {
throw new Error(
`${localRef} points to ${foreignRef}, ${foreignRef} points to ${actualRef}, expected ${foreignRef} to point to ${localRef}`
`${localRef} expects ${foreignRef} to be a two way relationship, but ${foreignRef} points to ${actualRef}`
)
}

let [leftRel, rightRel] = sortRelationships(
const [leftRel, rightRel] = sortRelationships(
{ listKey, fieldPath, field },
{ listKey: field.list, fieldPath: field.field, field: foreignField }
)
Expand Down
13 changes: 8 additions & 5 deletions tests/api-tests/fields/types/relationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { allowAll } from '@keystone-6/core/access'

const fieldKey = 'foo'

function getSchema (field: { ref: string, many?: boolean }) {
function getSchema(field: {
ref: string
many?: boolean
}) {
return createSystem(
initConfig(
config({
Expand Down Expand Up @@ -150,8 +153,8 @@ describe('Reference errors', () => {
Foo: list({
access: allowAll,
fields: {
bar: relationship({ ref: 'Abc.def' }),
},
bar: relationship({ ref: 'Abc.def' })
}
}),
},
error: `Foo.bar points to Abc.def, but Abc.def doesn't exist`,
Expand Down Expand Up @@ -186,7 +189,7 @@ describe('Reference errors', () => {
},
}),
},
error: `Foo.bar points to Abc.def, Abc.def points to Foo, expected Abc.def to point to Foo.bar`,
error: `Foo.bar expects Abc.def to be a two way relationship, but Abc.def points to Foo`,
},
'3-way / 2-way conflict': {
lists: {
Expand All @@ -203,7 +206,7 @@ describe('Reference errors', () => {
},
}),
},
error: `Foo.bar points to Abc.def, Abc.def points to Foo.bazzz, expected Abc.def to point to Foo.bar`,
error: `Foo.bar expects Abc.def to be a two way relationship, but Abc.def points to Foo.bazzz`,
},
'field wrong type': {
lists: {
Expand Down

0 comments on commit 6c869f0

Please sign in to comment.