Skip to content

Commit 704f975

Browse files
committed
Add null restriction to Enum value parser
1 parent 947325b commit 704f975

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

src/executor/__tests__/variables.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ var TestType = new GraphQLObjectType({
7979

8080
var schema = new GraphQLSchema({ query: TestType });
8181

82-
describe('Execute: Handles input objects', () => {
82+
describe('Execute: Handles inputs', () => {
8383

8484
describe('Handles objects and nullability', () => {
8585
describe('using inline structs', () => {
@@ -116,7 +116,7 @@ describe('Execute: Handles input objects', () => {
116116

117117
describe('using variables', () => {
118118
var doc = `
119-
query q($input:TestInputObject) {
119+
query q($input: TestInputObject) {
120120
fieldWithObjectInput(input: $input)
121121
}
122122
`;
@@ -242,21 +242,6 @@ describe('Execute: Handles input objects', () => {
242242
});
243243
});
244244

245-
it('allows nullable inputs to be set to null directly', async () => {
246-
var doc = `
247-
{
248-
fieldWithNullableStringInput(input: null)
249-
}
250-
`;
251-
var ast = parse(doc);
252-
253-
return expect(await execute(schema, null, ast)).to.deep.equal({
254-
data: {
255-
fieldWithNullableStringInput: 'null'
256-
}
257-
});
258-
});
259-
260245
it('allows nullable inputs to be set to a value in a variable', async () => {
261246
var doc = `
262247
query SetsNullable($value: String) {

src/language/__tests__/parser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ fragment MissingOn Type
104104
).to.throw('Syntax Error GraphQL (1:9) Expected Name, found }');
105105
});
106106

107+
it('does not allow null as value', async () => {
108+
expect(
109+
() => parse('{ fieldWithNullableStringInput(input: null) }')
110+
).to.throw('Syntax Error GraphQL (1:39) Unexpected Name "null"');
111+
});
112+
107113
var kitchenSink = readFileSync(
108114
join(__dirname, '/kitchen-sink.graphql'),
109115
{ encoding: 'utf8' }

src/language/parser.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -501,21 +501,22 @@ function parseValue(parser, isConst: boolean): Value {
501501
loc: loc(parser, token.start)
502502
};
503503
case TokenKind.NAME:
504-
advance(parser);
505-
switch (token.value) {
506-
case 'true':
507-
case 'false':
508-
return {
509-
kind: BOOLEAN,
510-
value: token.value === 'true',
511-
loc: loc(parser, token.start)
512-
};
504+
if (token.value === 'true' || token.value === 'false') {
505+
advance(parser);
506+
return {
507+
kind: BOOLEAN,
508+
value: token.value === 'true',
509+
loc: loc(parser, token.start)
510+
};
511+
} else if (token.value !== 'null') {
512+
advance(parser);
513+
return {
514+
kind: ENUM,
515+
value: token.value,
516+
loc: loc(parser, token.start)
517+
};
513518
}
514-
return {
515-
kind: ENUM,
516-
value: token.value,
517-
loc: loc(parser, token.start)
518-
};
519+
break;
519520
case TokenKind.DOLLAR:
520521
if (!isConst) {
521522
return parseVariable(parser);

0 commit comments

Comments
 (0)