hayes / pothos

Pothos GraphQL is library for creating GraphQL schemas in typescript using a strongly typed code first approach
https://pothos-graphql.dev
ISC License
2.33k stars 158 forks source link

[Prisma plugin] Required field should be non-nullable automatically unless we override it #1234

Open vimutti77 opened 3 months ago

vimutti77 commented 3 months ago

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
  }),
})
hayes commented 3 months ago

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

vimutti77 commented 3 months ago

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!