Skip to content

Fix parsing of default values in buildASTSchema, extendSchema and buildClientSchema #903

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import invariant from '../jsutils/invariant';
import isNullish from '../jsutils/isNullish';
import * as Kind from '../language/kinds';
import { assertValidName } from '../utilities/assertValidName';
import { scalarValueFromAST } from '../utilities/scalarValueFromAST';
import type {
ScalarTypeDefinitionNode,
ObjectTypeDefinitionNode,
Expand Down Expand Up @@ -345,7 +346,10 @@ export class GraphQLScalarType {
// Parses an externally provided value to use as an input.
parseValue(value: mixed): mixed {
const parser = this._scalarConfig.parseValue;
return parser && !isNullish(value) ? parser(value) : undefined;
if (isNullish(value)) {
return undefined;
}
return parser ? parser(value) : value;
}

// Determines if an internal value is valid for this type.
Expand All @@ -357,7 +361,7 @@ export class GraphQLScalarType {
// Parses an externally provided literal value to use as an input.
parseLiteral(valueNode: ValueNode): mixed {
const parser = this._scalarConfig.parseLiteral;
return parser ? parser(valueNode) : undefined;
return (parser || scalarValueFromAST)(valueNode);
}

toString(): string {
Expand Down
16 changes: 16 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,22 @@ describe('Schema Builder', () => {
expect(output).to.equal(body);
});

it('Custom scalar argument field with default', () => {
const body = dedent`
schema {
query: Hello
}

scalar CustomScalar

type Hello {
str(int: CustomScalar = 2): String
}
`;
const output = cycleOutput(body);
expect(output).to.equal(body);
});

it('Simple type with mutation', () => {
const body = dedent`
schema {
Expand Down
24 changes: 24 additions & 0 deletions src/utilities/__tests__/buildClientSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,30 @@ describe('Type System: build schema from introspection', () => {
await testSchema(schema);
});

it('builds a schema with default value on custom scalar field', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'ArgFields',
fields: {
testField: {
type: GraphQLString,
args: {
testArg: {
type: new GraphQLScalarType({
name: 'CustomScalar',
serialize: value => value
}),
defaultValue: 'default'
}
}
}
}
})
});

await testSchema(schema);
});

it('builds a schema with an enum', async () => {
const foodEnum = new GraphQLEnumType({
name: 'Food',
Expand Down
77 changes: 77 additions & 0 deletions src/utilities/__tests__/scalarValueFromAST-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2017, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import { describe, it } from 'mocha';
import { expect } from 'chai';
import { scalarValueFromAST } from '../scalarValueFromAST';
import { parseValue } from '../../language';

describe('scalarValueFromAST', () => {

function testCase(valueText, expected) {
expect(
scalarValueFromAST(parseValue(valueText))
).to.deep.equal(expected);
}

function testNegativeCase(valueText, errorMsg) {
expect(
() => scalarValueFromAST(parseValue(valueText))
).to.throw(errorMsg);
}

it('parses simple values', () => {
testCase('null', null);
testCase('true', true);
testCase('false', false);
testCase('123', 123);
testCase('123.456', 123.456);
testCase('"abc123"', 'abc123');
});

it('parses lists of values', () => {
testCase('[true, false]', [ true, false ]);
testCase('[true, 123.45]', [ true, 123.45 ]);
testCase('[true, null]', [ true, null ]);
testCase('[true, ["foo", 1.2]]', [ true, [ 'foo', 1.2 ] ]);
});

it('parses input objects', () => {
testCase(
'{ int: 123, requiredBool: false }',
{ int: 123, requiredBool: false }
);
testCase(
'{ foo: [{ bar: "baz"}]}',
{ foo: [ { bar: 'baz'} ] }
);
});

it('rejects enum values and query variables', () => {
testNegativeCase('TEST_ENUM_VALUE', 'Scalar value can not contain Enum.');
testNegativeCase(
'$test_variable',
'Scalar value can not contain Query variable.'
);
testNegativeCase('[TEST_ENUM_VALUE]', 'Scalar value can not contain Enum.');
testNegativeCase(
'[$test_variable]',
'Scalar value can not contain Query variable.'
);
testNegativeCase(
'{foo: TEST_ENUM_VALUE}',
'Scalar value can not contain Enum.'
);
testNegativeCase(
'{bar: $test_variable}',
'Scalar value can not contain Query variable.'
);
});

});
8 changes: 1 addition & 7 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
name: def.name.value,
description: getDescription(def),
astNode: def,
serialize: () => null,
// Note: validation calls the parse functions to determine if a
// literal value is correct. Returning null would cause use of custom
// scalars to always fail validation. Returning false causes them to
// always pass validation.
parseValue: () => false,
parseLiteral: () => false,
serialize: value => value,
});
}

Expand Down
6 changes: 0 additions & 6 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,6 @@ export function buildClientSchema(
name: scalarIntrospection.name,
description: scalarIntrospection.description,
serialize: id => id,
// Note: validation calls the parse functions to determine if a
// literal value is correct. Returning null would cause use of custom
// scalars to always fail validation. Returning false causes them to
// always pass validation.
parseValue: () => false,
parseLiteral: () => false,
});
}

Expand Down
6 changes: 0 additions & 6 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,6 @@ export function extendSchema(
description: getDescription(typeNode),
astNode: typeNode,
serialize: id => id,
// Note: validation calls the parse functions to determine if a
// literal value is correct. Returning null would cause use of custom
// scalars to always fail validation. Returning false causes them to
// always pass validation.
parseValue: () => false,
parseLiteral: () => false,
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export { valueFromAST } from './valueFromAST';
// Create a GraphQL language AST from a JavaScript value.
export { astFromValue } from './astFromValue';

// Create a JavaScript value from a GraphQL language AST representation
// of Scalar.
export { scalarValueFromAST } from './scalarValueFromAST';

// A helper to use within recursive-descent visitors which need to be aware of
// the GraphQL type system.
export { TypeInfo } from './TypeInfo';
Expand Down
43 changes: 43 additions & 0 deletions src/utilities/scalarValueFromAST.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* @flow */
/**
* Copyright (c) 2017, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import keyValMap from '../jsutils/keyValMap';
import * as Kind from '../language/kinds';
import type { ValueNode } from '../language/ast';

/**
* Create a JavaScript value from a GraphQL language AST representation
* of Scalar.
*/
export function scalarValueFromAST(astValue: ValueNode): mixed {
switch (astValue.kind) {
case Kind.NULL:
return null;
case Kind.INT:
return parseInt(astValue.value, 10);
case Kind.FLOAT:
return parseFloat(astValue.value);
case Kind.STRING:
case Kind.BOOLEAN:
return astValue.value;
case Kind.LIST:
return astValue.values.map(scalarValueFromAST);
case Kind.OBJECT:
return keyValMap(
astValue.fields,
field => field.name.value,
field => scalarValueFromAST(field.value),
);
case Kind.ENUM:
throw new Error('Scalar value can not contain Enum.');
case Kind.VARIABLE:
throw new Error('Scalar value can not contain Query variable.');
}
}