Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make 'parseLiteral' optional and use wrapped 'parseValue' by default #1955

Merged
merged 1 commit into from Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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