Skip to content

Commit 44e5d9a

Browse files
authored
rename serialize and parseValue methods (#4241)
finish addressing #2357, started in #4218
1 parent 858aa32 commit 44e5d9a

16 files changed

+371
-275
lines changed

integrationTests/ts/kitchenSink-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { Kind } from 'graphql/language';
66
// Test subset of public APIs with "exactOptionalPropertyTypes" flag enabled
77
new GraphQLScalarType({
88
name: 'SomeScalar',
9-
serialize: undefined,
10-
parseValue: undefined,
9+
coerceOutputValue: undefined,
10+
coerceInputValue: undefined,
1111
coerceInputLiteral: undefined,
1212
});
1313

src/execution/__tests__/executor-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,10 +1258,10 @@ describe('Execute: Handles basic execution tasks', () => {
12581258
expect(asyncResult).to.deep.equal(result);
12591259
});
12601260

1261-
it('fails when serialize of custom scalar does not return a value', () => {
1261+
it('fails when coerceOutputValue of custom scalar does not return a value', () => {
12621262
const customScalar = new GraphQLScalarType({
12631263
name: 'CustomScalar',
1264-
serialize() {
1264+
coerceOutputValue() {
12651265
/* returns nothing */
12661266
},
12671267
});
@@ -1283,7 +1283,7 @@ describe('Execute: Handles basic execution tasks', () => {
12831283
errors: [
12841284
{
12851285
message:
1286-
'Expected `CustomScalar.serialize("CUSTOM_VALUE")` to return non-nullable value, returned: undefined',
1286+
'Expected `CustomScalar.coerceOutputValue("CUSTOM_VALUE")` to return non-nullable value, returned: undefined',
12871287
locations: [{ line: 1, column: 3 }],
12881288
path: ['customScalar'],
12891289
},

src/execution/__tests__/variables-test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const TestFaultyScalarGraphQLError = new GraphQLError(
4444

4545
const TestFaultyScalar = new GraphQLScalarType({
4646
name: 'FaultyScalar',
47-
parseValue() {
47+
coerceInputValue() {
4848
throw TestFaultyScalarGraphQLError;
4949
},
5050
coerceInputLiteral() {
@@ -54,13 +54,13 @@ const TestFaultyScalar = new GraphQLScalarType({
5454

5555
const TestComplexScalar = new GraphQLScalarType({
5656
name: 'ComplexScalar',
57-
parseValue(value) {
58-
expect(value).to.equal('SerializedValue');
59-
return 'DeserializedValue';
57+
coerceInputValue(value) {
58+
expect(value).to.equal('ExternalValue');
59+
return 'InternalValue';
6060
},
6161
coerceInputLiteral(ast) {
62-
expect(ast).to.include({ kind: 'StringValue', value: 'SerializedValue' });
63-
return 'DeserializedValue';
62+
expect(ast).to.include({ kind: 'StringValue', value: 'ExternalValue' });
63+
return 'InternalValue';
6464
},
6565
});
6666

@@ -284,13 +284,13 @@ describe('Execute: Handles inputs', () => {
284284
it('properly runs coerceInputLiteral on complex scalar types', () => {
285285
const result = executeQuery(`
286286
{
287-
fieldWithObjectInput(input: {c: "foo", d: "SerializedValue"})
287+
fieldWithObjectInput(input: {c: "foo", d: "ExternalValue"})
288288
}
289289
`);
290290

291291
expect(result).to.deep.equal({
292292
data: {
293-
fieldWithObjectInput: '{ c: "foo", d: "DeserializedValue" }',
293+
fieldWithObjectInput: '{ c: "foo", d: "InternalValue" }',
294294
},
295295
});
296296
});
@@ -447,25 +447,25 @@ describe('Execute: Handles inputs', () => {
447447
});
448448

449449
it('executes with complex scalar input', () => {
450-
const params = { input: { c: 'foo', d: 'SerializedValue' } };
450+
const params = { input: { c: 'foo', d: 'ExternalValue' } };
451451
const result = executeQuery(doc, params);
452452

453453
expect(result).to.deep.equal({
454454
data: {
455-
fieldWithObjectInput: '{ c: "foo", d: "DeserializedValue" }',
455+
fieldWithObjectInput: '{ c: "foo", d: "InternalValue" }',
456456
},
457457
});
458458
});
459459

460460
it('errors on faulty scalar type input', () => {
461-
const params = { input: { c: 'foo', e: 'SerializedValue' } };
461+
const params = { input: { c: 'foo', e: 'ExternalValue' } };
462462
const result = executeQuery(doc, params);
463463

464464
expectJSON(result).toDeepEqual({
465465
errors: [
466466
{
467467
message:
468-
'Variable "$input" got invalid value "SerializedValue" at "input.e"; FaultyScalarErrorMessage',
468+
'Variable "$input" got invalid value "ExternalValue" at "input.e"; FaultyScalarErrorMessage',
469469
locations: [{ line: 2, column: 16 }],
470470
extensions: { code: 'FaultyScalarErrorExtensionCode' },
471471
},

src/execution/execute.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ function toNodes(fieldDetailsList: FieldDetailsList): ReadonlyArray<FieldNode> {
763763
* Implements the "Executing fields" section of the spec
764764
* In particular, this function figures out the value that the field returns by
765765
* calling its resolve function, then calls completeValue to complete promises,
766-
* serialize scalars, or execute the sub-selection-set for objects.
766+
* coercing scalars, or execute the sub-selection-set for objects.
767767
*/
768768
function executeField(
769769
exeContext: ExecutionContext,
@@ -937,7 +937,7 @@ function handleFieldError(
937937
* for the inner type on each item in the list.
938938
*
939939
* If the field type is a Scalar or Enum, ensures the completed value is a legal
940-
* value of the type by calling the `serialize` method of GraphQL type
940+
* value of the type by calling the `coerceOutputValue` method of GraphQL type
941941
* definition.
942942
*
943943
* If the field is an abstract type, determine the runtime type of the value
@@ -1001,8 +1001,8 @@ function completeValue(
10011001
);
10021002
}
10031003

1004-
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
1005-
// returning null if serialization is not possible.
1004+
// If field type is a leaf type, Scalar or Enum, coerce to a valid value,
1005+
// returning null if coercion is not possible.
10061006
if (isLeafType(returnType)) {
10071007
return [completeLeafValue(returnType, result), undefined];
10081008
}
@@ -1571,14 +1571,14 @@ function completeLeafValue(
15711571
returnType: GraphQLLeafType,
15721572
result: unknown,
15731573
): unknown {
1574-
const serializedResult = returnType.serialize(result);
1575-
if (serializedResult == null) {
1574+
const coerced = returnType.coerceOutputValue(result);
1575+
if (coerced == null) {
15761576
throw new Error(
1577-
`Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` +
1578-
`return non-nullable value, returned: ${inspect(serializedResult)}`,
1577+
`Expected \`${inspect(returnType)}.coerceOutputValue(${inspect(result)})\` to ` +
1578+
`return non-nullable value, returned: ${inspect(coerced)}`,
15791579
);
15801580
}
1581-
return serializedResult;
1581+
return coerced;
15821582
}
15831583

15841584
/**

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ export type {
198198
GraphQLUnionTypeExtensions,
199199
GraphQLScalarSerializer,
200200
GraphQLScalarValueParser,
201-
/* @deprecated in favor of GraphQLScalarInputLiteralCoercer, will be removed in v18 */
202201
GraphQLScalarLiteralParser,
202+
GraphQLScalarOutputValueCoercer,
203+
GraphQLScalarInputValueCoercer,
203204
GraphQLScalarInputLiteralCoercer,
204205
GraphQLDefaultValueUsage,
205206
} from './type/index.js';

src/type/__tests__/definition-test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ describe('Type System: Scalars', () => {
6060
serialize: someScalar.serialize,
6161
parseValue: someScalar.parseValue,
6262
parseLiteral: someScalar.parseLiteral,
63+
coerceOutputValue: someScalar.coerceOutputValue,
64+
coerceInputValue: someScalar.coerceInputValue,
6365
coerceInputLiteral: undefined,
6466
valueToLiteral: undefined,
6567
extensions: {},
@@ -76,6 +78,8 @@ describe('Type System: Scalars', () => {
7678
serialize: passThroughFunc,
7779
parseValue: passThroughFunc,
7880
parseLiteral: passThroughFunc,
81+
coerceOutputValue: passThroughFunc,
82+
coerceInputValue: passThroughFunc,
7983
coerceInputLiteral: passThroughFunc,
8084
valueToLiteral: passThroughFunc,
8185
extensions: { someExtension: 'extension' },
@@ -95,6 +99,8 @@ describe('Type System: Scalars', () => {
9599
serialize: passThroughFunc,
96100
parseValue: passThroughFunc,
97101
parseLiteral: passThroughFunc,
102+
coerceOutputValue: passThroughFunc,
103+
coerceInputValue: passThroughFunc,
98104
coerceInputLiteral: passThroughFunc,
99105
valueToLiteral: passThroughFunc,
100106
extensions: { [test]: 'extension' },
@@ -110,6 +116,8 @@ describe('Type System: Scalars', () => {
110116

111117
expect(scalar.serialize).to.equal(identityFunc);
112118
expect(scalar.parseValue).to.equal(identityFunc);
119+
expect(scalar.coerceOutputValue).to.equal(identityFunc);
120+
expect(scalar.coerceInputValue).to.equal(identityFunc);
113121
expect(scalar.parseLiteral).to.be.a('function');
114122
/* default will be provided in v18 when parseLiteral is removed */
115123
// expect(scalar.coerceInputLiteral).to.be.a('function');
@@ -143,15 +151,15 @@ describe('Type System: Scalars', () => {
143151
);
144152
});
145153

146-
it('rejects a Scalar type defining coerceInputLiteral but not parseValue', () => {
154+
it('rejects a Scalar type defining coerceInputLiteral but not coerceInputValue', () => {
147155
expect(
148156
() =>
149157
new GraphQLScalarType({
150158
name: 'SomeScalar',
151159
coerceInputLiteral: passThroughFunc,
152160
}),
153161
).to.throw(
154-
'SomeScalar must provide both "parseValue" and "coerceInputLiteral" functions.',
162+
'SomeScalar must provide both "coerceInputValue" and "coerceInputLiteral" functions.',
155163
);
156164
});
157165
});
@@ -644,6 +652,30 @@ describe('Type System: Enums', () => {
644652
expect(someEnum.toConfig()).to.deep.equal(someEnumConfig);
645653
});
646654

655+
it('can be coerced to an output value via serialize() method', () => {
656+
const someEnum = new GraphQLEnumType({
657+
name: 'SomeEnum',
658+
values: {
659+
FOO: {
660+
value: 'foo',
661+
},
662+
},
663+
});
664+
expect(someEnum.serialize('foo')).to.equal('FOO');
665+
});
666+
667+
it('can be coerced to an input value via parseValue() method', () => {
668+
const someEnum = new GraphQLEnumType({
669+
name: 'SomeEnum',
670+
values: {
671+
FOO: {
672+
value: 'foo',
673+
},
674+
},
675+
});
676+
expect(someEnum.parseValue('FOO')).to.equal('foo');
677+
});
678+
647679
it('defines an enum type with deprecated value', () => {
648680
const EnumTypeWithDeprecatedValue = new GraphQLEnumType({
649681
name: 'EnumWithDeprecatedValue',

0 commit comments

Comments
 (0)