Skip to content

Commit e4c0fe4

Browse files
committed
Explain custom scalar example (graphql/graphql.github.io#163)
1 parent c24fefd commit e4c0fe4

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

website/pages/type.mdx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,20 @@ functions used to ensure validity.
214214
```js
215215
const OddType = new GraphQLScalarType({
216216
name: 'Odd',
217-
serialize: oddValue,
218-
parseValue: oddValue,
217+
description:
218+
'This custom scalar will only return a value if the passed in value is an odd integer, when it's not it will return null.'
219+
serialize: (outputValue) => {
220+
// This function gets called for response-data, the application returns data
221+
// for a property and in the schema we see that this value has the "Odd" type.
222+
return typeof outputValue === 'number' && outputValue % 2 === 1 ? value : null;
223+
},
224+
parseValue: (inputValue) => {
225+
// This function gets called for input-data, i.e. variables being passed in
226+
return typeof inputValue === 'number' && outputValue % 2 === 1 ? value : null;
227+
},
219228
parseLiteral(ast) {
229+
// This function gets called when the value is passed in as a literal on the
230+
// Executable GraphQL Document
220231
if (ast.kind === Kind.INT) {
221232
return oddValue(parseInt(ast.value, 10));
222233
}

0 commit comments

Comments
 (0)