Issue: fields with `@default` should appear as required `!` in generated GraphQL schema base type (1.0.0-rc.4)
StephanDecker opened this issue · 5 comments
Given the following model
model Priority {
id String @id @default(uuid())
name String @unique
}
The generated graphql type is
type Priority {
id: String
name: String!
}
ID isn't marked as mandatory - in 1.0.0-rc.3 it was still correct. Thanks for your support!
That might be intentional since you're assigning a default value in case it's not filled out? What happens if you remove the @default
directive?
Thanks for your comment: Indeed - if I remove the @default(uuid())
directive the id is mandatory again.
However, I don't think it's intentional because if you look at the getPriority
resp. listPriorities
query the return type is Priority
. And we know there is an ID in the Priority type.
Also prisma states that the ID cannot be optional: https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#id
# Find a single Priority record by unique identifier.
getPriority(where: PriorityWhereUniqueInput!): Priority
# Find many Priority records (optional query filters).
listPriorities(
where: PriorityWhereInput
orderBy: [PriorityOrderByInput]
skip: Int
take: Int
): [Priority]
Does that make sense?
That's because when you use @default
, the ID field is autofilled if it's empty on creation... So the GraphQL schema follows along with that behavior. If you try using the generated Prisma client with your Priority
model and the @default
directive, you'll see that Prisma lets you create a model without specifying your ID field, too.
I'm not sure what is your issue here?
For the creation the Priority
type isn't used as an input but the input type PriorityCreateInput
.
That has also changed from (1.0.0-rc.3)
input PriorityCreateInput {
name: String!
}
to (1.0.0-rc.4)
input PriorityCreateInput {
id: String
name: String!
}
But for that both is fine for me.
Coming back to your question: The issue is that I use the generated graphql schema to generate TS types out of it, see: https://the-guild.dev/graphql/codegen
Now the autogenerated Priority
TS type has an optional id
so that the TS compiler complains which I can understand ;-) because every Priority
record has an id
by definition and isn't undefined
.
That are only types so I can overwrite that I just wanted to let you know.
Fix will be released as part of 1.0.0-rc.5
.