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 159 forks source link

Support for default value on fields #789

Open mmahalwy opened 1 year ago

mmahalwy commented 1 year ago

I dont think its part of the graphql spec, so this would be mostly a helper but would be great to have a defaultValue option on fields instead of having to write a resolver to handle.

For example:

someField: t.field({
      type: 'String',
      nullable: true,
      resolve: (parent) => parent.someField || 'default value',
    }),

can become:

someField: t.exposeString('someField', {
      nullable: true,
      defaultValue: 'default value',
    }),

It could be a plugin where we have

someField: t.exposeStringWithDefault('someField', {
      nullable: true,
      defaultValue: 'default value',
    }),
hayes commented 1 year ago

unless this makes it into Graphql itself it probably doesn't make sense for core, but as a plugin this could be cool! I probably wouldn't have time to prioritize building it myself of a while, but definitely open to a PR (and would be more than happy to help out on figuring out how to implement it).