@@ -100,25 +100,36 @@ describe('DateTime scalar', () => {
100
100
Integrate the scalar into a schema and run real GraphQL queries to validate end-to-end behavior.
101
101
102
102
``` js
103
- const { graphql , buildSchema } = require (' graphql' );
103
+ const { graphql , GraphQLSchema , GraphQLObjectType } = require (' graphql' );
104
+ const { DateTimeResolver as DateTime } = require (' graphql-scalars' );
105
+
106
+ const Query = new GraphQLObjectType ({
107
+ name: ' Query' ,
108
+ fields: {
109
+ now: {
110
+ type: DateTime,
111
+ resolve () {
112
+ return new Date ();
113
+ },
114
+ },
115
+ },
116
+ });
104
117
105
- const schema = buildSchema ( `
118
+ /*
106
119
scalar DateTime
107
120
108
121
type Query {
109
122
now: DateTime
110
123
}
111
- ` );
112
-
113
- const rootValue = {
114
- now : () => new Date (' 2024-01-01T00:00:00Z' ),
115
- };
124
+ */
125
+ const schema = new GraphQLSchema ({
126
+ query: Query,
127
+ });
116
128
117
129
async function testQuery () {
118
130
const response = await graphql ({
119
131
schema,
120
132
source: ' { now }' ,
121
- rootValue,
122
133
});
123
134
console .log (response);
124
135
}
@@ -181,13 +192,22 @@ If you need domain-specific behavior, you can wrap an existing scalar with custo
181
192
``` js
182
193
const { EmailAddressResolver } = require (' graphql-scalars' );
183
194
184
- const StrictEmail = new GraphQLScalarType ({
195
+ const StrictEmailAddress = new GraphQLScalarType ({
185
196
... EmailAddressResolver,
197
+ name: ' StrictEmailAddress' ,
186
198
parseValue (value ) {
187
- if (! value .endsWith (' @example.com' )) {
199
+ const email = EmailAddressResolver .parseValue (value);
200
+ if (! email .endsWith (' @example.com' )) {
201
+ throw new TypeError (' Only example.com emails are allowed.' );
202
+ }
203
+ return email;
204
+ },
205
+ parseLiteral (literal , variables ) {
206
+ const email = EmailAddressResolver .parseLiteral (literal, variables);
207
+ if (! email .endsWith (' @example.com' )) {
188
208
throw new TypeError (' Only example.com emails are allowed.' );
189
209
}
190
- return EmailAddressResolver . parseValue (value) ;
210
+ return email ;
191
211
},
192
212
});
193
213
```
0 commit comments