hayes / hayes/pothos

Allow scalars option for SchemaBuilder

Open
#910 7 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
2.6k
Forks
184
Avg merge
4h 25m
Merged PRs (30d)
137

Description

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
```ts
const builder = new SchemaBuilder({
scalars: {
'ScalarName': Resolver
}
})
```

This would help with [this issue on using graphql-scalars](https://github.com/hayes/pothos/issues/726) and [this discussion for custom scalar methods](https://github.com/hayes/pothos/discussions/568)

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

This would look like the following with [graphql-scalars](http://graphql-scalars.dev/) and method generation. I have used the SimpleObjectsPlugin for simplicity

```ts
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
```ts
builder.simpleObject('Appointment', {
fields: (t) => ({
email: t.field({type: 'Email'})
date: t.field({type: 'DateTime'}),
})
})
```

Contributor guide

Open the contributing guide

Research direction

Start at SchemaBuilder and the existing builder.addScalarType flow; the issue names no implementation files or tests. Check how constructor options and scalar resolver types support graphql-scalars, then verify that the requested scalars object preserves Input/Output inference and supports the shown field definitions.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.