diff --git a/src/type/__tests__/definition-test.js b/src/type/__tests__/definition-test.js index 0365e8130f..4828968a30 100644 --- a/src/type/__tests__/definition-test.js +++ b/src/type/__tests__/definition-test.js @@ -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, @@ -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', () => { @@ -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( () => diff --git a/src/type/definition.js b/src/type/definition.js index a13ac86d96..e43600a713 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -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.'); @@ -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', diff --git a/src/validation/__tests__/harness.js b/src/validation/__tests__/harness.js index b2d78d197d..cfeb35a7a5 100644 --- a/src/validation/__tests__/harness.js +++ b/src/validation/__tests__/harness.js @@ -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, @@ -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)}`); },