[Prisma plugin] Required field should be non-nullable automatically unless we override it
It is quite common for the nullability of an object in GraphQL to match that of the corresponding object in the Prisma schema.
However, with the current approach, I need to manually specify nullable: true/false in many places, and unfortunately, I sometimes make errors. It would be beneficial if the Prisma plugin could automatically handle this by default.”
In example, if we have a prisma model like this.
model User {
id String @id
name String
tel String?
}
And if we expose those fields. the result should be like this
builder.prismaObject('User', {
fields: (t) => ({
id: t.exposeID('id'), // should be non-nullable
name: t.exposeString('name'), // should be non-nullable
tel: t.exposeString('tel'), // should be nullable
nullableName: t.exposeString('name', { nullable: true }), // nullable because we override the default
}),
})
We might be able to add that as a setting., but this is intentional.
Pothos v4 made changes so that you will now see type-errors if you try to make a nullable field in your db non-nullable in the GraphQL.
Pothos is designed to never rely on the shape of your data to determine the shape of the GraphQL schema. This means that a change in your DB (or other data) should never automatically change the GraphQL schema. This abstraction is critical for making it manageable to maintain large APIs and has worked out really well in the projects I've worked on. It does require adding some extra definitions for which fields are nullable, but to me this is a worthwhile tradeoff.
But, if it seems worth it to you, we could add a setting to the builder that enables mirroring the Schemas nullability
The setting to enable mirroring the schema’s nullability would be perfectly helpful for me. I’m eager to see this feature come to fruition, and I’m available to assist if needed!
Hey guys,
i know this is an old issue but would ask if there are plans to integrate it. We are currently running into the same problem as vimutti77.
My current workaround is something like this 🙈:
// @ts-expect-error currently, pothos doesn't support `nullable:false` on nullable fields
// which is, i think, the correct behaviour, but would be cool to override this anyway
// GH: https://github.com/hayes/pothos/issues/1234
relationName: t.relation("relation", { nullable: false }),
@noxify I think your case is a little different, you want the opposite of the original request, which was to automatically match the nullability of the schema.
What you are looking for should work fine if you do this:
relationName: t.relation("relation", { nullable: false, onNull: 'error' }),
Hey @hayes
thanks for the quick reply and sorry for the noise - seems I didn't read the initial request correctly ( just searched for nullable 🙈 ).
For the relation, this works perfectly. Now I have to find a solution for expose methods.
- t.exposeString('foo', { nullable: false })
+ t.string({ nullable: false, resolve: (parent) => parent.foo! })