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

valueFromAST: fixed coercing of null variables on non-nullable type #2348

Merged
merged 1 commit into from
Jan 14, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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