diff --git a/.changeset/add-isnonnull-true.md b/.changeset/add-isnonnull-true.md new file mode 100644 index 00000000000..5a2319cfb1a --- /dev/null +++ b/.changeset/add-isnonnull-true.md @@ -0,0 +1,5 @@ +--- +'@keystone-6/core': major +--- + +Add support for [field].graphql.isNonNull: true diff --git a/packages/core/src/fields/non-null-graphql.ts b/packages/core/src/fields/non-null-graphql.ts index 7886569c836..8e89d6aa3eb 100644 --- a/packages/core/src/fields/non-null-graphql.ts +++ b/packages/core/src/fields/non-null-graphql.ts @@ -25,7 +25,7 @@ export function makeValidateHook ( isNullable?: boolean } graphql?: { - isNonNull?: { + isNonNull?: boolean | { read?: boolean } } @@ -75,7 +75,7 @@ export function assertReadIsNonNullAllowed ( const prismaType = `import('${prismaClientPath}').Prisma.${name}` return [ `type Resolved${name} = {`, - ...Object.entries(list.fields).map(([fieldKey, { dbField }]) => { + ...Object.entries(list.fields).map(([fieldKey, { dbField, graphql }]) => { if (dbField.kind === 'none') return ` ${fieldKey}?: undefined` // TODO: this could be elsewhere, maybe id-field.ts @@ -78,18 +78,14 @@ function printInterimType ( return [ ` ${fieldKey}: {`, ...Object.entries(dbField.fields).map(([subFieldKey, subDbField]) => { - // TODO: untrue if a db defaultValue is set - // const optional = operation === 'Create' && subDbField.mode === 'required' ? '' : '?' - const optional = '?' + const optional = operation === 'create' && subDbField.mode === 'required' && !subDbField.default ? '' : '?' return ` ${subFieldKey}${optional}: ${prismaType}['${fieldKey}_${subFieldKey}']` }), ` }`, ].join('\n') } - // TODO: untrue if a db defaultValue is set - // const optional = operation === 'Create' && dbField.mode === 'required' ? '' : '?' - const optional = '?' + const optional = (operation === 'create' && dbField.mode === 'required' && !dbField.default) || graphql.isNonNull[operation] ? '' : '?' return ` ${fieldKey}${optional}: ${prismaType}['${fieldKey}']` }), `}`, diff --git a/packages/core/src/types/config/fields.ts b/packages/core/src/types/config/fields.ts index bc0b8c6d2c3..e668e1bc60f 100644 --- a/packages/core/src/types/config/fields.ts +++ b/packages/core/src/types/config/fields.ts @@ -37,25 +37,27 @@ export type CommonFieldConfig = { } graphql?: { cacheHint?: CacheHint - isNonNull?: { - // should this field be non-nullable on the {List} GraphQL type? - read?: boolean - // should this field be non-nullable on the {List}CreateInput GraphQL type? - create?: boolean - // should this field be non-nullable on the {List}UpdateInput GraphQL type? - update?: boolean - } + isNonNull?: + | boolean + | { + // whether this field is non-nullable on the {List} GraphQL type + read?: boolean + // whether this field is non-nullable on the {List}CreateInput GraphQL type + create?: boolean + // whether this field is non-nullable on the {List}UpdateInput GraphQL type + update?: boolean + } omit?: | boolean | { - // should this field be omitted from the {List} GraphQL type? - read?: boolean - // should this field be omitted from the {List}CreateInput GraphQL type? - create?: boolean - // should this field be omitted from the {List}UpdateInput GraphQL type? - update?: boolean - } + // whether this field is omitted from the {List} GraphQL type + read?: boolean + // whether this field is omitted from the {List}CreateInput GraphQL type + create?: boolean + // whether this field is omitted from the {List}UpdateInput GraphQL type + update?: boolean + } } isFilterable?: MaybeFieldFunction isOrderable?: MaybeFieldFunction