Skip to content

Commit 02179ff

Browse files
committed
Light editorial on #4380
1 parent e5ab7f7 commit 02179ff

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

website/pages/docs/advanced-custom-scalars.mdx

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,36 @@ describe('DateTime scalar', () => {
100100
Integrate 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

117129
async 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
182193
const { 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
```

website/pages/docs/custom-scalars.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ providing a name, description, and three functions:
8080
- `serialize`: How the server sends internal values to clients.
8181
- `parseValue`: How the server parses incoming variable values.
8282
- `parseLiteral`: How the server parses inline values in queries.
83+
- `specifiedByURL` (optional): A URL specifying the behavior of your scalar;
84+
this can be used by clients and tooling to recognize and handle common scalars
85+
such as [date-time](https://scalars.graphql.org/andimarek/date-time.html)
86+
independent of their name.
8387

8488
The following example is a custom `DateTime` scalar that handles ISO-8601 encoded
8589
date strings:
@@ -90,6 +94,7 @@ const { GraphQLScalarType, Kind } = require('graphql');
9094
const DateTime = new GraphQLScalarType({
9195
name: 'DateTime',
9296
description: 'An ISO-8601 encoded UTC date string.',
97+
specifiedByURL: 'https://scalars.graphql.org/andimarek/date-time.html',
9398

9499
serialize(value) {
95100
if (!(value instanceof Date)) {

0 commit comments

Comments
 (0)