@@ -100,25 +100,36 @@ describe('DateTime scalar', () => {
100100Integrate the scalar into a schema and run real GraphQL queries to validate end-to-end behavior.
101101
102102``` 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+ });
104117
105- const schema = buildSchema ( `
118+ /*
106119 scalar DateTime
107120
108121 type Query {
109122 now: DateTime
110123 }
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+ });
116128
117129async function testQuery () {
118130 const response = await graphql ({
119131 schema,
120132 source: ' { now }' ,
121- rootValue,
122133 });
123134 console .log (response);
124135}
@@ -181,13 +192,22 @@ If you need domain-specific behavior, you can wrap an existing scalar with custo
181192``` js
182193const { EmailAddressResolver } = require (' graphql-scalars' );
183194
184- const StrictEmail = new GraphQLScalarType ({
195+ const StrictEmailAddress = new GraphQLScalarType ({
185196 ... EmailAddressResolver,
197+ name: ' StrictEmailAddress' ,
186198 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' )) {
188208 throw new TypeError (' Only example.com emails are allowed.' );
189209 }
190- return EmailAddressResolver . parseValue (value) ;
210+ return email ;
191211 },
192212});
193213```
0 commit comments