You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -75,6 +79,83 @@ Now we can use the `Date` type in our classes.
75
79
creationDate: Date;
76
80
```
77
81
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
+
importGraphQLJSONfrom'graphql-type-json';
96
+
97
+
@Module({
98
+
imports: [
99
+
GraphQLModule.forRoot({
100
+
resolvers: { JSON: GraphQLJSON },
101
+
}),
102
+
],
103
+
})
104
+
exportclassAppModule {}
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.
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
+
exportclassAppModule {}
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
+
78
159
#### Schema first
79
160
80
161
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