Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add withScalar method #914

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions .changeset/three-rules-fail.md
@@ -0,0 +1,10 @@
---
'@pothos/core': minor
---

Add `withScalar` method to the schema builder to allow inference of Scalar typescript types from
`GraphQLScalarType` scalars

```typescript
const builder = new SchemaBuilder({}).withScalars({ Date: CustomDateScalar });
```
2 changes: 1 addition & 1 deletion packages/core/package.json
Expand Up @@ -52,7 +52,7 @@
"devDependencies": {
"@pothos/test-utils": "workspace:*",
"graphql": "16.8.1",
"graphql-scalars": "^1.22.2",
"graphql-scalars": "^1.22.4",
"graphql-tag": "^2.12.6"
},
"gitHead": "9dfe52f1975f41a111e01bf96a20033a914e2acc"
Expand Down
43 changes: 41 additions & 2 deletions packages/core/src/builder.ts
Expand Up @@ -591,8 +591,8 @@ export default class SchemaBuilder<Types extends SchemaTypes> {
const [options = {}] = args;
const { directives, extensions } = options;

const scalars = [GraphQLID, GraphQLInt, GraphQLFloat, GraphQLString, GraphQLBoolean];
scalars.forEach((scalar) => {
const builtInScalars = [GraphQLID, GraphQLInt, GraphQLFloat, GraphQLString, GraphQLBoolean];
builtInScalars.forEach((scalar) => {
if (!this.configStore.hasConfig(scalar.name as OutputType<Types>)) {
this.addScalarType(scalar.name as ScalarName<Types>, scalar, {});
}
Expand Down Expand Up @@ -621,4 +621,43 @@ export default class SchemaBuilder<Types extends SchemaTypes> {
? processedSchema
: lexicographicSortSchema(processedSchema);
}


withScalar<
const TName extends string,
TInternal,
TExternal,
TScalarType extends GraphQLScalarType<TInternal, TExternal>,
TTypes extends Types & {
Scalars: { [K in TName]: { Input: TInternal; Output: TExternal; }}
outputShapes: { [K in TName]: TInternal },
inputShapes: {[K in TName]: TInternal},
}
>(
name: TName,
scalar: TScalarType,
options?: Omit<
PothosSchemaTypes.ScalarTypeOptions<TTypes, InputShape<TTypes, TName>, OutputShape<TTypes, TName>>,
'serialize'
> & {
serialize?: GraphQLScalarSerializer<OutputShape<TTypes, TName>>;
},
) {
const builder = this as unknown as SchemaBuilder<TTypes>;
const config = scalar.toConfig();``
builder.scalarType<TName>(name, {
...config,
...options,
extensions: {
...config.extensions,
...options?.extensions,
},
} as PothosSchemaTypes.ScalarTypeOptions<
TTypes,
InputShape<TTypes, TName>,
ParentShape<TTypes, TName>
>);
return builder
}

}