Skip to content

Commit

Permalink
valueFromAST: fixed coercing of null variables on non-nullable… (#2348)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jan 14, 2020
1 parent f462347 commit 53afba1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/utilities/__tests__/valueFromAST-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('valueFromAST', () => {
expectValueFrom('"BLUE"', testEnum).to.equal(undefined);
expectValueFrom('null', testEnum).to.equal(null);
expectValueFrom('NULL', testEnum).to.equal(null);
expectValueFrom('NULL', new GraphQLNonNull(testEnum)).to.equal(null);
expectValueFrom('NAN', testEnum).to.deep.equal(NaN);
expectValueFrom('NO_CUSTOM_VALUE', testEnum).to.equal('NO_CUSTOM_VALUE');
});
Expand Down Expand Up @@ -184,6 +185,7 @@ describe('valueFromAST', () => {
expectValueFrom('$var', GraphQLBoolean, {}).to.equal(undefined);
expectValueFrom('$var', GraphQLBoolean, { var: true }).to.equal(true);
expectValueFrom('$var', GraphQLBoolean, { var: null }).to.equal(null);
expectValueFrom('$var', nonNullBool, { var: null }).to.equal(undefined);
});

it('asserts variables are provided as items in lists', () => {
Expand Down
24 changes: 12 additions & 12 deletions src/utilities/valueFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ export function valueFromAST(
return;
}

if (isNonNullType(type)) {
if (valueNode.kind === Kind.NULL) {
return; // Invalid: intentionally return no value.
}
return valueFromAST(valueNode, type.ofType, variables);
}

if (valueNode.kind === Kind.NULL) {
// This is explicitly returning the value null.
return null;
}

if (valueNode.kind === Kind.VARIABLE) {
const variableName = valueNode.name.value;
if (!variables || isInvalid(variables[variableName])) {
Expand All @@ -78,6 +66,18 @@ export function valueFromAST(
return variableValue;
}

if (isNonNullType(type)) {
if (valueNode.kind === Kind.NULL) {
return; // Invalid: intentionally return no value.
}
return valueFromAST(valueNode, type.ofType, variables);
}

if (valueNode.kind === Kind.NULL) {
// This is explicitly returning the value null.
return null;
}

if (isListType(type)) {
const itemType = type.ofType;
if (valueNode.kind === Kind.LIST) {
Expand Down

0 comments on commit 53afba1

Please sign in to comment.