Skip to content

Commit 387e009

Browse files
Merge pull request #2293 from jfairley/patch-1
docs: how to build or import a custom scalar
2 parents b7d153f + 05e0d19 commit 387e009

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

content/graphql/scalars.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ GraphQLModule.forRoot({
3232
}),
3333
```
3434

35-
In addition, you can create custom scalars. For example, to create a `Date` scalar, simply create a new class.
35+
In addition, you can create custom scalars.
36+
37+
#### Override a default scalar
38+
39+
To create a custom implementation for the `Date` scalar, simply create a new class.
3640

3741
```typescript
3842
import { Scalar, CustomScalar } from '@nestjs/graphql';
@@ -75,6 +79,83 @@ Now we can use the `Date` type in our classes.
7579
creationDate: Date;
7680
```
7781

82+
#### Import a custom scalar
83+
84+
To use a custom scalar, import and register it as a resolver. We’ll use the `graphql-type-json` package for demonstration purposes. This npm package defines a `JSON` GraphQL scalar type.
85+
86+
Start by installing the package:
87+
88+
```bash
89+
$ npm i --save graphql-type-json
90+
```
91+
92+
Once the package is installed, we pass a custom resolver to the `forRoot()` method:
93+
94+
```typescript
95+
import GraphQLJSON from 'graphql-type-json';
96+
97+
@Module({
98+
imports: [
99+
GraphQLModule.forRoot({
100+
resolvers: { JSON: GraphQLJSON },
101+
}),
102+
],
103+
})
104+
export class AppModule {}
105+
```
106+
107+
Now we can use the `JSON` type in our classes.
108+
109+
```typescript
110+
@Field((type) => GraphQLJSON)
111+
info: JSON;
112+
```
113+
114+
For a suite of useful scalars, take a look at the [graphql-scalars](https://www.npmjs.com/package/graphql-scalars) package.
115+
116+
#### Create a custom scalar
117+
118+
To define a custom scalar, create a new `GraphQLScalarType` instance. We'll create a custom `UUID` scalar.
119+
120+
```typescript
121+
const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
122+
123+
function validate(uuid: unknown): string | never {
124+
if (typeof uuid !== "string" || !regex.test(uuid)) {
125+
throw new Error("invalid uuid");
126+
}
127+
return uuid;
128+
}
129+
130+
export const CustomUuidScalar = new GraphQLScalarType({
131+
name: 'UUID',
132+
description: 'A simple UUID parser',
133+
serialize: (value) => validate(value),
134+
parseValue: (value) => validate(value),
135+
parseLiteral: (ast) => validate(ast.value)
136+
})
137+
```
138+
139+
We pass a custom resolver to the `forRoot()` method:
140+
141+
```typescript
142+
@Module({
143+
imports: [
144+
GraphQLModule.forRoot({
145+
resolvers: { UUID: CustomUuidScalar },
146+
}),
147+
],
148+
})
149+
export class AppModule {}
150+
```
151+
152+
Now we can use the `UUID` type in our classes.
153+
154+
```typescript
155+
@Field((type) => CustomUuidScalar)
156+
uuid: string;
157+
```
158+
78159
#### Schema first
79160

80161
To define a custom scalar (read more about scalars [here](https://www.apollographql.com/docs/graphql-tools/scalars.html)), create a type definition and a dedicated resolver. Here (as in the official documentation), we’ll use the `graphql-type-json` package for demonstration purposes. This npm package defines a `JSON` GraphQL scalar type.

0 commit comments

Comments
 (0)