Skip to content

Commit f2c3e96

Browse files
Make 'parseLiteral' optional and use wrapped 'parseValue' by default (#1955)
Motivation: #500 #1827 (comment)
1 parent b17d5be commit f2c3e96

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/type/__tests__/definition-test.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
import { describe, it } from 'mocha';
1111
import { expect } from 'chai';
1212

13+
import inspect from '../../jsutils/inspect';
1314
import identityFunc from '../../jsutils/identityFunc';
14-
import { valueFromASTUntyped } from '../../utilities/valueFromASTUntyped';
15+
import { parseValue } from '../../language/parser';
1516
import {
1617
type GraphQLType,
1718
type GraphQLNullableType,
@@ -64,7 +65,23 @@ describe('Type System: Scalars', () => {
6465

6566
expect(scalar.serialize).to.equal(identityFunc);
6667
expect(scalar.parseValue).to.equal(identityFunc);
67-
expect(scalar.parseLiteral).to.equal(valueFromASTUntyped);
68+
expect(scalar.parseLiteral).to.be.a('function');
69+
});
70+
71+
it('use parseValue for parsing literals if parseLiteral omitted', () => {
72+
const scalar = new GraphQLScalarType({
73+
name: 'Foo',
74+
parseValue(value) {
75+
return 'parseValue: ' + inspect(value);
76+
},
77+
});
78+
79+
expect(scalar.parseLiteral(parseValue('null'))).to.equal(
80+
'parseValue: null',
81+
);
82+
expect(scalar.parseLiteral(parseValue('{ foo: "bar" }'))).to.equal(
83+
'parseValue: { foo: "bar" }',
84+
);
6885
});
6986

7087
it('rejects a Scalar type defining serialize with an incorrect type', () => {
@@ -80,18 +97,6 @@ describe('Type System: Scalars', () => {
8097
);
8198
});
8299

83-
it('rejects a Scalar type defining parseValue but not parseLiteral', () => {
84-
expect(
85-
() =>
86-
new GraphQLScalarType({
87-
name: 'SomeScalar',
88-
parseValue: () => null,
89-
}),
90-
).to.throw(
91-
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.',
92-
);
93-
});
94-
95100
it('rejects a Scalar type defining parseLiteral but not parseValue', () => {
96101
expect(
97102
() =>

src/type/definition.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,10 @@ export class GraphQLScalarType {
561561
this.description = config.description;
562562
this.serialize = config.serialize || identityFunc;
563563
this.parseValue = config.parseValue || identityFunc;
564-
this.parseLiteral = config.parseLiteral || valueFromASTUntyped;
564+
this.parseLiteral =
565+
config.parseLiteral ||
566+
(node => this.parseValue(valueFromASTUntyped(node)));
567+
565568
this.astNode = config.astNode;
566569
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
567570
invariant(typeof config.name === 'string', 'Must provide name.');
@@ -570,7 +573,8 @@ export class GraphQLScalarType {
570573
`${this.name} must provide "serialize" function. If this custom Scalar ` +
571574
'is also used as an input type, ensure "parseValue" and "parseLiteral" functions are also provided.',
572575
);
573-
if (config.parseValue || config.parseLiteral) {
576+
577+
if (config.parseLiteral) {
574578
invariant(
575579
typeof config.parseValue === 'function' &&
576580
typeof config.parseLiteral === 'function',

src/validation/__tests__/harness.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import { expect } from 'chai';
1111
import inspect from '../../jsutils/inspect';
12-
import { parse, print } from '../../language';
12+
import { parse } from '../../language';
1313
import { validate, validateSDL } from '../validate';
1414
import {
1515
type ValidationRule,
@@ -293,9 +293,6 @@ const ComplicatedArgs = new GraphQLObjectType({
293293

294294
const InvalidScalar = new GraphQLScalarType({
295295
name: 'Invalid',
296-
parseLiteral(valueNode) {
297-
throw new Error(`Invalid scalar is always invalid: ${print(valueNode)}`);
298-
},
299296
parseValue(value) {
300297
throw new Error(`Invalid scalar is always invalid: ${inspect(value)}`);
301298
},

0 commit comments

Comments
 (0)