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

Enforces input coercion rules. #553

Merged
merged 7 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add appropriate list item behavior for missing vars
  • Loading branch information
leebyron committed Nov 3, 2016
commit 93f9a5f87a09f0b696e7ecb4dea1443d4ab16ccb
6 changes: 5 additions & 1 deletion src/utilities/__tests__/valueFromAST-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ describe('valueFromAST', () => {
});

it('asserts variables are provided as items in lists', () => {
testCaseWithVars({}, listOfBool, '[ $foo ]', undefined);
testCaseWithVars({}, listOfBool, '[ $foo ]', [ null ]);
testCaseWithVars({}, listOfNonNullBool, '[ $foo ]', undefined);
testCaseWithVars({ foo: true }, listOfNonNullBool, '[ $foo ]', [ true ]);
// Note: variables are expected to have already been coerced, so we
// do not expect the singleton wrapping behavior for variables.
testCaseWithVars({ foo: true }, listOfNonNullBool, '$foo', true);
testCaseWithVars({ foo: [ true ] }, listOfNonNullBool, '$foo', [ true ]);
});

it('omits input object fields for unprovided variables', () => {
Expand Down
27 changes: 18 additions & 9 deletions src/utilities/valueFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function valueFromAST(

if (type instanceof GraphQLNonNull) {
if (valueAST.kind === Kind.NULL) {
return; // Intentionally return no value.
return; // Invalid: intentionally return no value.
}
return valueFromAST(valueAST, type.ofType, variables);
}
Expand Down Expand Up @@ -90,24 +90,33 @@ export function valueFromAST(
const coercedValues = [];
const itemASTs = (valueAST: ListValue).values;
for (let i = 0; i < itemASTs.length; i++) {
const itemValue = valueFromAST(itemASTs[i], itemType, variables);
if (isInvalid(itemValue)) {
return; // Intentionally return no value.
if (isMissingVariable(itemASTs[i], variables)) {
// If an array contains a missing variable, it is either coerced to
// null or if the item type is non-null, it considered invalid.
if (itemType instanceof GraphQLNonNull) {
return; // Invalid: intentionally return no value.
}
coercedValues.push(null);
} else {
const itemValue = valueFromAST(itemASTs[i], itemType, variables);
if (isInvalid(itemValue)) {
return; // Invalid: intentionally return no value.
}
coercedValues.push(itemValue);
}
coercedValues.push(itemValue);
}
return coercedValues;
}
const coercedValue = valueFromAST(valueAST, itemType, variables);
if (isInvalid(coercedValue)) {
return; // Intentionally return no value.
return; // Invalid: intentionally return no value.
}
return [ coercedValue ];
}

if (type instanceof GraphQLInputObjectType) {
if (valueAST.kind !== Kind.OBJECT) {
return; // Intentionally return no value.
return; // Invalid: intentionally return no value.
}
const coercedObj = Object.create(null);
const fields = type.getFields();
Expand All @@ -124,13 +133,13 @@ export function valueFromAST(
if (!isInvalid(field.defaultValue)) {
coercedObj[fieldName] = field.defaultValue;
} else if (field.type instanceof GraphQLNonNull) {
return; // Intentionally return no value.
return; // Invalid: intentionally return no value.
}
continue;
}
const fieldValue = valueFromAST(fieldAST.value, field.type, variables);
if (isInvalid(fieldValue)) {
return; // Intentionally return no value.
return; // Invalid: intentionally return no value.
}
coercedObj[fieldName] = fieldValue;
}
Expand Down