Skip to content

Commit 7c92987

Browse files
committed
rename serialize and parseValue methods (graphql#4241)
finish addressing graphql#2357, started in graphql#4218
1 parent 88c1c57 commit 7c92987

File tree

16 files changed

+371
-275
lines changed

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
@@ -1221,10 +1221,10 @@ describe('Execute: Handles basic execution tasks', () => {
12211221
expect(asyncResult).to.deep.equal(result);
12221222
});
12231223

1224-
it('fails when serialize of custom scalar does not return a value', () => {
1224+
it('fails when coerceOutputValue of custom scalar does not return a value', () => {
12251225
const customScalar = new GraphQLScalarType({
12261226
name: 'CustomScalar',
1227-
serialize() {
1227+
coerceOutputValue() {
12281228
/* returns nothing */
12291229
},
12301230
});
@@ -1246,7 +1246,7 @@ describe('Execute: Handles basic execution tasks', () => {
12461246
errors: [
12471247
{
12481248
message:
1249-
'Expected `CustomScalar.serialize("CUSTOM_VALUE")` to return non-nullable value, returned: undefined',
1249+
'Expected `CustomScalar.coerceOutputValue("CUSTOM_VALUE")` to return non-nullable value, returned: undefined',
12501250
locations: [{ line: 1, column: 3 }],
12511251
path: ['customScalar'],
12521252
},

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: 'FaultyScalarErrorMessageExtensionCode' },
471471
},

src/execution/execute.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ function executeFields(
553553
* Implements the "Executing fields" section of the spec
554554
* In particular, this function figures out the value that the field returns by
555555
* calling its resolve function, then calls completeValue to complete promises,
556-
* serialize scalars, or execute the sub-selection-set for objects.
556+
* coercing scalars, or execute the sub-selection-set for objects.
557557
*/
558558
function executeField(
559559
exeContext: ExecutionContext,
@@ -712,7 +712,7 @@ function handleFieldError(
712712
* for the inner type on each item in the list.
713713
*
714714
* If the field type is a Scalar or Enum, ensures the completed value is a legal
715-
* value of the type by calling the `serialize` method of GraphQL type
715+
* value of the type by calling the `coerceOutputValue` method of GraphQL type
716716
* definition.
717717
*
718718
* If the field is an abstract type, determine the runtime type of the value
@@ -770,8 +770,8 @@ function completeValue(
770770
);
771771
}
772772

773-
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
774-
// returning null if serialization is not possible.
773+
// If field type is a leaf type, Scalar or Enum, coerce to a valid value,
774+
// returning null if coercion is not possible.
775775
if (isLeafType(returnType)) {
776776
return completeLeafValue(returnType, result);
777777
}
@@ -1117,14 +1117,14 @@ function completeLeafValue(
11171117
returnType: GraphQLLeafType,
11181118
result: unknown,
11191119
): unknown {
1120-
const serializedResult = returnType.serialize(result);
1121-
if (serializedResult == null) {
1120+
const coerced = returnType.coerceOutputValue(result);
1121+
if (coerced == null) {
11221122
throw new Error(
1123-
`Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` +
1124-
`return non-nullable value, returned: ${inspect(serializedResult)}`,
1123+
`Expected \`${inspect(returnType)}.coerceOutputValue(${inspect(result)})\` to ` +
1124+
`return non-nullable value, returned: ${inspect(coerced)}`,
11251125
);
11261126
}
1127-
return serializedResult;
1127+
return coerced;
11281128
}
11291129

11301130
/**

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ export type {
196196
GraphQLUnionTypeExtensions,
197197
GraphQLScalarSerializer,
198198
GraphQLScalarValueParser,
199-
/* @deprecated in favor of GraphQLScalarInputLiteralCoercer, will be removed in v18 */
200199
GraphQLScalarLiteralParser,
200+
GraphQLScalarOutputValueCoercer,
201+
GraphQLScalarInputValueCoercer,
201202
GraphQLScalarInputLiteralCoercer,
202203
GraphQLDefaultValueUsage,
203204
} 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)