pothos icon indicating copy to clipboard operation
pothos copied to clipboard

Allow scalars option for SchemaBuilder

Open Liam-Tait opened this issue 1 year ago • 7 comments

Instead of adding scalars after creating a schema builder using builder.addScalarType, I would like to be able to give SchemaBuilder an object with scalars.

This would set up like the following

const builder = new SchemaBuilder({
  scalars: {
    'ScalarName': Resolver
  }
})

This would help with this issue on using graphql-scalars and this discussion for custom scalar methods

As the resolvers are set up, methods can then be added for convenience.

This would look like the following with graphql-scalars and method generation. I have used the SimpleObjectsPlugin for simplicity

import SchemaBuilder from "@pothos/core"
import { EmailAddressResolver, DateTimeResolver } from "graphql-scalars"

const builder = new SchemaBuilder({
  plugins: [SimpleObjectsPlugin],
  scalars: {
    'DateTime': DateTimeResolver,
    'Email': EmailAddressResolver
  }
})

builder.simpleObject('Appointment', {
  fields: (t) => ({
    email: t.email()
    date: t.datetime(),
  })
})

This is useful as the types can be inferred from the resolver for Input and Output. This is also useful for not adding the extra methods as the field would still be correctly typed e.g

builder.simpleObject('Appointment', {
  fields: (t) => ({
    email: t.field({type: 'Email'})
    date: t.field({type: 'DateTime'}),
  })
})

Liam-Tait avatar May 16 '23 03:05 Liam-Tait