Skip to content

Commit 3b33c3d

Browse files
committed
rename serialize and parseValue methods
1 parent 0254e5f commit 3b33c3d

File tree

16 files changed

+365
-275
lines changed

16 files changed

+365
-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
@@ -765,7 +765,7 @@ function toNodes(fieldDetailsList: FieldDetailsList): ReadonlyArray<FieldNode> {
765765
* Implements the "Executing fields" section of the spec
766766
* In particular, this function figures out the value that the field returns by
767767
* calling its resolve function, then calls completeValue to complete promises,
768-
* serialize scalars, or execute the sub-selection-set for objects.
768+
* coercing scalars, or execute the sub-selection-set for objects.
769769
*/
770770
function executeField(
771771
exeContext: ExecutionContext,
@@ -939,7 +939,7 @@ function handleFieldError(
939939
* for the inner type on each item in the list.
940940
*
941941
* If the field type is a Scalar or Enum, ensures the completed value is a legal
942-
* value of the type by calling the `serialize` method of GraphQL type
942+
* value of the type by calling the `coerceOutputValue` method of GraphQL type
943943
* definition.
944944
*
945945
* If the field is an abstract type, determine the runtime type of the value
@@ -1003,8 +1003,8 @@ function completeValue(
10031003
);
10041004
}
10051005

1006-
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
1007-
// returning null if serialization is not possible.
1006+
// If field type is a leaf type, Scalar or Enum, coerce to a valid value,
1007+
// returning null if coercion is not possible.
10081008
if (isLeafType(returnType)) {
10091009
return [completeLeafValue(returnType, result), undefined];
10101010
}
@@ -1573,14 +1573,14 @@ function completeLeafValue(
15731573
returnType: GraphQLLeafType,
15741574
result: unknown,
15751575
): unknown {
1576-
const serializedResult = returnType.serialize(result);
1577-
if (serializedResult == null) {
1576+
const coerced = returnType.coerceOutputValue(result);
1577+
if (coerced == null) {
15781578
throw new Error(
1579-
`Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` +
1580-
`return non-nullable value, returned: ${inspect(serializedResult)}`,
1579+
`Expected \`${inspect(returnType)}.coerceOutputValue(${inspect(result)})\` to ` +
1580+
`return non-nullable value, returned: ${inspect(coerced)}`,
15811581
);
15821582
}
1583-
return serializedResult;
1583+
return coerced;
15841584
}
15851585

15861586
/**

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: 32 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' },
@@ -91,6 +95,8 @@ describe('Type System: Scalars', () => {
9195

9296
expect(scalar.serialize).to.equal(identityFunc);
9397
expect(scalar.parseValue).to.equal(identityFunc);
98+
expect(scalar.coerceOutputValue).to.equal(identityFunc);
99+
expect(scalar.coerceInputValue).to.equal(identityFunc);
94100
expect(scalar.parseLiteral).to.be.a('function');
95101
/* default will be provided in v18 when parseLiteral is removed */
96102
// expect(scalar.coerceInputLiteral).to.be.a('function');
@@ -124,15 +130,15 @@ describe('Type System: Scalars', () => {
124130
);
125131
});
126132

127-
it('rejects a Scalar type defining coerceInputLiteral but not parseValue', () => {
133+
it('rejects a Scalar type defining coerceInputLiteral but not coerceInputValue', () => {
128134
expect(
129135
() =>
130136
new GraphQLScalarType({
131137
name: 'SomeScalar',
132138
coerceInputLiteral: passThroughFunc,
133139
}),
134140
).to.throw(
135-
'SomeScalar must provide both "parseValue" and "coerceInputLiteral" functions.',
141+
'SomeScalar must provide both "coerceInputValue" and "coerceInputLiteral" functions.',
136142
);
137143
});
138144
});
@@ -625,6 +631,30 @@ describe('Type System: Enums', () => {
625631
expect(someEnum.toConfig()).to.deep.equal(someEnumConfig);
626632
});
627633

634+
it('can be coerced to an output value via serialize() method', () => {
635+
const someEnum = new GraphQLEnumType({
636+
name: 'SomeEnum',
637+
values: {
638+
FOO: {
639+
value: 'foo',
640+
},
641+
},
642+
});
643+
expect(someEnum.serialize('foo')).to.equal('FOO');
644+
});
645+
646+
it('can be coerced to an input value via parseValue() method', () => {
647+
const someEnum = new GraphQLEnumType({
648+
name: 'SomeEnum',
649+
values: {
650+
FOO: {
651+
value: 'foo',
652+
},
653+
},
654+
});
655+
expect(someEnum.parseValue('FOO')).to.equal('foo');
656+
});
657+
628658
it('defines an enum type with deprecated value', () => {
629659
const EnumTypeWithDeprecatedValue = new GraphQLEnumType({
630660
name: 'EnumWithDeprecatedValue',

0 commit comments

Comments
 (0)