Skip to content

Commit

Permalink
variable tidyup for relationship field, throw earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Nov 14, 2022
1 parent 61772d0 commit 56d5d91
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions packages/core/src/fields/types/relationship/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,35 @@ export const relationship =
ref,
...config
}: RelationshipFieldConfig<ListTypeInfo>): FieldTypeFunc<ListTypeInfo> =>
meta => {
const { fieldKey } = meta;
({ fieldKey, listKey, lists }) => {
const { many = false } = config;
const [foreignListKey, foreignFieldKey] = ref.split('.');
const foreignList = lists[foreignListKey];
if (!foreignList) {
throw new Error(`Unable to resolve list '${foreignListKey}' for ${listKey}.${fieldKey}`);
}
const foreignListTypes = foreignList.types;

const commonConfig = {
...config,
views: '@keystone-6/core/fields/types/relationship/views',
getAdminMeta: (): Parameters<typeof import('./views').controller>[0]['fieldMeta'] => {
const adminMetaRoot = getAdminMetaForRelationshipField();
if (!meta.lists[foreignListKey]) {
throw new Error(
`The ref [${ref}] on relationship [${meta.listKey}.${meta.fieldKey}] is invalid`
);
const localListMeta = adminMetaRoot.listsByKey[listKey];
const foreignListMeta = adminMetaRoot.listsByKey[foreignListKey];

if (!foreignListMeta) {
throw new Error(`The ref [${ref}] on relationship [${listKey}.${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.key === meta.fieldKey
);
const currentField = localListMeta.fields.find(x => x.key === fieldKey);
if (currentField) {
const allForeignFields = new Set(
adminMetaRoot.listsByKey[foreignListKey].fields.map(x => x.key)
);
const allForeignFields = new Set(foreignListMeta.fields.map(x => x.key));
for (const [configOption, foreignFields] of [
['ui.cardFields', config.ui.cardFields],
['ui.inlineCreate.fields', config.ui.inlineCreate?.fields ?? []],
Expand All @@ -120,18 +122,17 @@ export const relationship =
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`
`The ${configOption} option on the relationship field at ${listKey}.${fieldKey} includes the "${foreignField}" field but that field does not exist on the "${foreignListKey}" list`
);
}
}
}
}
}

const foreignList = adminMetaRoot.listsByKey[foreignListKey];
const hideCreate = config.ui?.hideCreate ?? false;
const refLabelField: typeof foreignFieldKey = foreignList.labelField;
const refSearchFields: typeof foreignFieldKey[] = foreignList.fields
const refLabelField: typeof foreignFieldKey = foreignListMeta.labelField;
const refSearchFields: typeof foreignFieldKey[] = foreignListMeta.fields
.filter(x => x.search)
.map(x => x.key);

Expand Down Expand Up @@ -176,20 +177,20 @@ export const relationship =
};
}

if (!(refLabelField in foreignList.fieldsByKey)) {
if (!(refLabelField in foreignListMeta.fieldsByKey)) {
throw new Error(
`The ui.labelField option for field '${fieldKey}' uses '${refLabelField}' but that field doesn't exist.`
);
}

for (const searchFieldKey of refSearchFields) {
if (!(searchFieldKey in foreignList.fieldsByKey)) {
if (!(searchFieldKey in foreignListMeta.fieldsByKey)) {
throw new Error(
`The ui.searchFields option for relationship field '${fieldKey}' includes '${searchFieldKey}' but that field doesn't exist.`
);
}

const field = foreignList.fieldsByKey[searchFieldKey];
const field = foreignListMeta.fieldsByKey[searchFieldKey];
if (field.search) continue;

throw new Error(
Expand All @@ -208,12 +209,7 @@ export const relationship =
};
},
};
if (!meta.lists[foreignListKey]) {
throw new Error(
`Unable to resolve related list '${foreignListKey}' from ${meta.listKey}.${meta.fieldKey}`
);
}
const listTypes = meta.lists[foreignListKey].types;

if (config.many) {
return fieldType({
kind: 'relation',
Expand All @@ -225,36 +221,39 @@ export const relationship =
...commonConfig,
input: {
where: {
arg: graphql.arg({ type: listTypes.relateTo.many.where }),
arg: graphql.arg({ type: foreignListTypes.relateTo.many.where }),
resolve(value, context, resolve) {
return resolve(value);
},
},
create: listTypes.relateTo.many.create && {
arg: graphql.arg({ type: listTypes.relateTo.many.create }),
create: foreignListTypes.relateTo.many.create && {
arg: graphql.arg({ type: foreignListTypes.relateTo.many.create }),
async resolve(value, context, resolve) {
return resolve(value);
},
},
update: listTypes.relateTo.many.update && {
arg: graphql.arg({ type: listTypes.relateTo.many.update }),
update: foreignListTypes.relateTo.many.update && {
arg: graphql.arg({ type: foreignListTypes.relateTo.many.update }),
async resolve(value, context, resolve) {
return resolve(value);
},
},
},
output: graphql.field({
args: listTypes.findManyArgs,
type: graphql.list(graphql.nonNull(listTypes.output)),
args: foreignListTypes.findManyArgs,
type: graphql.list(graphql.nonNull(foreignListTypes.output)),
resolve({ value }, args) {
return value.findMany(args);
},
}),
extraOutputFields: {
[`${meta.fieldKey}Count`]: graphql.field({
[`${fieldKey}Count`]: graphql.field({
type: graphql.Int,
args: {
where: graphql.arg({ type: graphql.nonNull(listTypes.where), defaultValue: {} }),
where: graphql.arg({
type: graphql.nonNull(foreignListTypes.where),
defaultValue: {},
}),
},
resolve({ value }, args) {
return value.count({
Expand All @@ -265,6 +264,7 @@ export const relationship =
},
});
}

return fieldType({
kind: 'relation',
mode: 'one',
Expand All @@ -275,27 +275,27 @@ export const relationship =
...commonConfig,
input: {
where: {
arg: graphql.arg({ type: listTypes.where }),
arg: graphql.arg({ type: foreignListTypes.where }),
resolve(value, context, resolve) {
return resolve(value);
},
},
create: listTypes.relateTo.one.create && {
arg: graphql.arg({ type: listTypes.relateTo.one.create }),
create: foreignListTypes.relateTo.one.create && {
arg: graphql.arg({ type: foreignListTypes.relateTo.one.create }),
async resolve(value, context, resolve) {
return resolve(value);
},
},

update: listTypes.relateTo.one.update && {
arg: graphql.arg({ type: listTypes.relateTo.one.update }),
update: foreignListTypes.relateTo.one.update && {
arg: graphql.arg({ type: foreignListTypes.relateTo.one.update }),
async resolve(value, context, resolve) {
return resolve(value);
},
},
},
output: graphql.field({
type: listTypes.output,
type: foreignListTypes.output,
resolve({ value }) {
return value();
},
Expand Down

0 comments on commit 56d5d91

Please sign in to comment.