Skip to content

Commit f2d38d9

Browse files
authored
docs: how to build or import a custom scalar
1 parent 2cca86c commit f2d38d9

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

content/graphql/scalars.md

Lines changed: 81 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,82 @@ 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") throw new Error("invalid uuid");
125+
if (!regex.test(uuid)) throw new Error("invalid uuid");
126+
return uuid;
127+
}
128+
129+
export const CustomUuidScalar = new GraphQLScalarType({
130+
name: 'UUID',
131+
description: 'A simple UUID parser',
132+
serialize: (value) => validate(value),
133+
parseValue: (value) => validate(value),
134+
parseLiteral: (ast) => validate(ast.value)
135+
})
136+
```
137+
138+
We pass a custom resolver to the `forRoot()` method:
139+
140+
```typescript
141+
@Module({
142+
imports: [
143+
GraphQLModule.forRoot({
144+
resolvers: { UUID: CustomUuidScalar },
145+
}),
146+
],
147+
})
148+
export class AppModule {}
149+
```
150+
151+
Now we can use the `UUID` type in our classes.
152+
153+
```typescript
154+
@Field((type) => CustomUuidScalar)
155+
uuid: string;
156+
```
157+
78158
#### Schema first
79159

80160
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)