Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for [field].graphql.isNonNull: true #9234

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-isnonnull-true.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': breaking
---

Add support for [field].graphql.isNonNull: true
6 changes: 3 additions & 3 deletions packages/core/src/lib/core/initialise-lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
23 changes: 14 additions & 9 deletions packages/core/src/lib/typescript-schema-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ function printInterimType<L extends InitialisedList> (
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
Expand All @@ -114,15 +119,15 @@ function printInterimType<L extends InitialisedList> (
}

// soft-block `id` updates for relationship safety
if (operation === 'Update') return ` id?: undefined`
if (operation === 'update') return ` id?: undefined`
}

if (dbField.kind === 'multi') {
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 = operation === 'create' && subDbField.mode === 'required' ? '' : '?'
const optional = '?'
return ` ${subFieldKey}${optional}: ${prismaType}['${fieldKey}_${subFieldKey}']`
}),
Expand All @@ -131,8 +136,8 @@ function printInterimType<L extends InitialisedList> (
}

// 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}']`
}),
`}`,
Expand Down Expand Up @@ -209,7 +214,7 @@ export function printGeneratedTypes (
list,
listKey,
list.graphql.names.createInputName,
'Create'
'create'
)
}

Expand All @@ -219,7 +224,7 @@ export function printGeneratedTypes (
list,
listKey,
list.graphql.names.updateInputName,
'Update'
'update'
)
}
}
Expand Down
32 changes: 17 additions & 15 deletions packages/core/src/types/config/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,27 @@ export type CommonFieldConfig<ListTypeInfo extends BaseListTypeInfo> = {
}
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<ListTypeInfo>) => MaybePromise<boolean>)
isOrderable?: boolean | ((args: FilterOrderArgs<ListTypeInfo>) => MaybePromise<boolean>)
Expand Down
Loading