diff --git a/.changeset/add-isnonnull-true.md b/.changeset/add-isnonnull-true.md new file mode 100644 index 00000000000..9cb30d431b1 --- /dev/null +++ b/.changeset/add-isnonnull-true.md @@ -0,0 +1,5 @@ +--- +'@keystone-6/core': breaking +--- + +Add support for [field].graphql.isNonNull: true diff --git a/packages/core/src/lib/core/initialise-lists.ts b/packages/core/src/lib/core/initialise-lists.ts index c8e91675393..c26ca6cd8c3 100644 --- a/packages/core/src/lib/core/initialise-lists.ts +++ b/packages/core/src/lib/core/initialise-lists.ts @@ -407,9 +407,9 @@ function getListsWithInitialisedFields ( cacheHint: f.graphql?.cacheHint, isEnabled: isEnabledField, isNonNull: { - read: f.graphql?.isNonNull?.read ?? false, - create: f.graphql?.isNonNull?.create ?? false, - update: f.graphql?.isNonNull?.update ?? false, + read: typeof f.graphql?.isNonNull === 'boolean' ? f.graphql.isNonNull : f.graphql?.isNonNull?.read ?? false, + create: typeof f.graphql?.isNonNull === 'boolean' ? f.graphql.isNonNull : f.graphql?.isNonNull?.create ?? false, + update: typeof f.graphql?.isNonNull === 'boolean' ? f.graphql.isNonNull : f.graphql?.isNonNull?.update ?? false, }, }, ui: { diff --git a/packages/core/src/lib/typescript-schema-printer.ts b/packages/core/src/lib/typescript-schema-printer.ts index 744c8cbf2b0..ff5cfacb88d 100644 --- a/packages/core/src/lib/typescript-schema-printer.ts +++ b/packages/core/src/lib/typescript-schema-printer.ts @@ -95,13 +95,18 @@ function printInterimType ( list: L, listKey: string, typename: string, - operation: 'Create' | 'Update' + operation: 'create' | 'update' ) { - const prismaType = `import('${prismaClientPath}').Prisma.${listKey}${operation}Input` + const operationName = { + 'create': 'Create', + 'update': 'Update' + }[operation] + const prismaType = `import('${prismaClientPath}').Prisma.${listKey}${operationName}Input` return [ `type Resolved${typename} = {`, - ...Object.entries(list.fields).map(([fieldKey, { dbField }]) => { + ...Object.entries(list.fields).map(([fieldKey, field]) => { + const { dbField } = field if (dbField.kind === 'none') return ` ${fieldKey}?: undefined` // TODO: this could be elsewhere, maybe id-field.ts @@ -114,7 +119,7 @@ function printInterimType ( } // soft-block `id` updates for relationship safety - if (operation === 'Update') return ` id?: undefined` + if (operation === 'update') return ` id?: undefined` } if (dbField.kind === 'multi') { @@ -122,7 +127,7 @@ function printInterimType ( ` ${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 = operation === 'create' && subDbField.mode === 'required' ? '' : '?' const optional = '?' return ` ${subFieldKey}${optional}: ${prismaType}['${fieldKey}_${subFieldKey}']` }), @@ -131,8 +136,8 @@ function printInterimType ( } // TODO: untrue if a db defaultValue is set - // const optional = operation === 'Create' && dbField.mode === 'required' ? '' : '?' - const optional = '?' + // const optional = operation === 'create' && dbField.mode === 'required' ? '' : '?' + const optional = field.graphql.isNonNull[operation] ? '' : '?' return ` ${fieldKey}${optional}: ${prismaType}['${fieldKey}']` }), `}`, @@ -209,7 +214,7 @@ export function printGeneratedTypes ( list, listKey, list.graphql.names.createInputName, - 'Create' + 'create' ) } @@ -219,7 +224,7 @@ export function printGeneratedTypes ( list, listKey, list.graphql.names.updateInputName, - 'Update' + 'update' ) } } diff --git a/packages/core/src/types/config/fields.ts b/packages/core/src/types/config/fields.ts index f519bf6a632..2960a84008a 100644 --- a/packages/core/src/types/config/fields.ts +++ b/packages/core/src/types/config/fields.ts @@ -33,25 +33,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?: boolean | ((args: FilterOrderArgs) => MaybePromise) isOrderable?: boolean | ((args: FilterOrderArgs) => MaybePromise)