Skip to content

Commit

Permalink
Make 'parseLiteral' optional and use wrapped 'parseValue' by default (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 7, 2019
1 parent b17d5be commit f2c3e96
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
33 changes: 19 additions & 14 deletions src/type/__tests__/definition-test.js
Expand Up @@ -10,8 +10,9 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';

import inspect from '../../jsutils/inspect';
import identityFunc from '../../jsutils/identityFunc';
import { valueFromASTUntyped } from '../../utilities/valueFromASTUntyped';
import { parseValue } from '../../language/parser';
import {
type GraphQLType,
type GraphQLNullableType,
Expand Down Expand Up @@ -64,7 +65,23 @@ describe('Type System: Scalars', () => {

expect(scalar.serialize).to.equal(identityFunc);
expect(scalar.parseValue).to.equal(identityFunc);
expect(scalar.parseLiteral).to.equal(valueFromASTUntyped);
expect(scalar.parseLiteral).to.be.a('function');
});

it('use parseValue for parsing literals if parseLiteral omitted', () => {
const scalar = new GraphQLScalarType({
name: 'Foo',
parseValue(value) {
return 'parseValue: ' + inspect(value);
},
});

expect(scalar.parseLiteral(parseValue('null'))).to.equal(
'parseValue: null',
);
expect(scalar.parseLiteral(parseValue('{ foo: "bar" }'))).to.equal(
'parseValue: { foo: "bar" }',
);
});

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

it('rejects a Scalar type defining parseValue but not parseLiteral', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
parseValue: () => null,
}),
).to.throw(
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.',
);
});

it('rejects a Scalar type defining parseLiteral but not parseValue', () => {
expect(
() =>
Expand Down
8 changes: 6 additions & 2 deletions src/type/definition.js
Expand Up @@ -561,7 +561,10 @@ export class GraphQLScalarType {
this.description = config.description;
this.serialize = config.serialize || identityFunc;
this.parseValue = config.parseValue || identityFunc;
this.parseLiteral = config.parseLiteral || valueFromASTUntyped;
this.parseLiteral =
config.parseLiteral ||
(node => this.parseValue(valueFromASTUntyped(node)));

this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
invariant(typeof config.name === 'string', 'Must provide name.');
Expand All @@ -570,7 +573,8 @@ export class GraphQLScalarType {
`${this.name} must provide "serialize" function. If this custom Scalar ` +
'is also used as an input type, ensure "parseValue" and "parseLiteral" functions are also provided.',
);
if (config.parseValue || config.parseLiteral) {

if (config.parseLiteral) {
invariant(
typeof config.parseValue === 'function' &&
typeof config.parseLiteral === 'function',
Expand Down
5 changes: 1 addition & 4 deletions src/validation/__tests__/harness.js
Expand Up @@ -9,7 +9,7 @@

import { expect } from 'chai';
import inspect from '../../jsutils/inspect';
import { parse, print } from '../../language';
import { parse } from '../../language';
import { validate, validateSDL } from '../validate';
import {
type ValidationRule,
Expand Down Expand Up @@ -293,9 +293,6 @@ const ComplicatedArgs = new GraphQLObjectType({

const InvalidScalar = new GraphQLScalarType({
name: 'Invalid',
parseLiteral(valueNode) {
throw new Error(`Invalid scalar is always invalid: ${print(valueNode)}`);
},
parseValue(value) {
throw new Error(`Invalid scalar is always invalid: ${inspect(value)}`);
},
Expand Down

0 comments on commit f2c3e96

Please sign in to comment.