From 9190e5ce1cc557931a15ca1f7b15586f033c8a02 Mon Sep 17 00:00:00 2001 From: Ginhing <3444080+Ginhing@users.noreply.github.com> Date: Fri, 18 Feb 2022 17:15:46 +0800 Subject: [PATCH 1/2] correct outdated documentation according #3231 returning `null` was no longer allowed. fix prettier --- src/type/definition.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/type/definition.ts b/src/type/definition.ts index 090afa367f..501111c1cc 100644 --- a/src/type/definition.ts +++ b/src/type/definition.ts @@ -562,10 +562,9 @@ export interface GraphQLScalarTypeExtensions { * Scalars (or Enums) and are defined with a name and a series of functions * used to parse input from ast or variables and to ensure validity. * - * If a type's serialize function does not return a value (i.e. it returns + * If a type's serialize function returns `null` or does not return a value (i.e. it returns * `undefined`) then an error will be raised and a `null` value will be returned - * in the response. If the serialize function returns `null`, then no error will - * be included in the response. + * in the response. * * Example: * From b157d96d56ade071a24a1b0c71334efb9af57689 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 21 Feb 2022 13:18:41 +0200 Subject: [PATCH 2/2] Review changes --- src/type/definition.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/type/definition.ts b/src/type/definition.ts index 501111c1cc..5e0c6d0472 100644 --- a/src/type/definition.ts +++ b/src/type/definition.ts @@ -562,9 +562,9 @@ export interface GraphQLScalarTypeExtensions { * Scalars (or Enums) and are defined with a name and a series of functions * used to parse input from ast or variables and to ensure validity. * - * If a type's serialize function returns `null` or does not return a value (i.e. it returns - * `undefined`) then an error will be raised and a `null` value will be returned - * in the response. + * If a type's serialize function returns `null` or does not return a value + * (i.e. it returns `undefined`) then an error will be raised and a `null` + * value will be returned in the response. It is always better to validate * * Example: * @@ -572,9 +572,16 @@ export interface GraphQLScalarTypeExtensions { * const OddType = new GraphQLScalarType({ * name: 'Odd', * serialize(value) { - * if (value % 2 === 1) { - * return value; + * if (!Number.isFinite(value)) { + * throw new Error( + * `Scalar "Odd" cannot represent "${value}" since it is not a finite number.`, + * ); * } + * + * if (value % 2 === 0) { + * throw new Error(`Scalar "Odd" cannot represent "${value}" since it is even.`); + * } + * return value; * } * }); * ```