Skip to content

BugFix: Nested List Input Coercion not matching GraphQL spec #3859

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 2 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
40 changes: 36 additions & 4 deletions src/utilities/__tests__/coerceInputValue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,46 @@ describe('coerceInputValue', () => {
expectValue(result).to.deep.equal(null);
});

it('returns nested lists for nested non-list values', () => {
it('returns error for nested non-list values', () => {
const result = coerceValue([1, 2, 3], TestNestedList);
expectValue(result).to.deep.equal([[1], [2], [3]]);
expectErrors(result).to.deep.equal([
{
error: 'Expected type "[Int]" to be a list.',
path: [0],
value: 1,
},
{
error: 'Expected type "[Int]" to be a list.',
path: [1],
value: 2,
},
{
error: 'Expected type "[Int]" to be a list.',
path: [2],
value: 3,
},
]);
});

it('returns nested null for nested null values', () => {
it('returns errors for null values', () => {
const result = coerceValue([42, [null], null], TestNestedList);
expectValue(result).to.deep.equal([[42], [null], null]);
expectErrors(result).to.deep.equal([
{
error: 'Expected type "[Int]" to be a list.',
path: [0],
value: 42,
},
{
error: 'Expected type "[Int]" to be a list.',
path: [2],
value: null,
},
]);
});

it('returns nested null for nested null values', () => {
const result = coerceValue([[null], [null]], TestNestedList);
expectValue(result).to.deep.equal([[null], [null]]);
});
});

Expand Down
13 changes: 13 additions & 0 deletions src/utilities/coerceInputValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,21 @@ function coerceInputValueImpl(
if (isListType(type)) {
const itemType = type.ofType;
if (isIterableObject(inputValue)) {
const isNestedList = Boolean(
isListType(itemType) && Array.from(inputValue).length > 1,
);
return Array.from(inputValue, (itemValue, index) => {
const itemPath = addPath(path, index, undefined);
if (isNestedList && !isIterableObject(itemValue)) {
// Input values should be iterable for nested list type with multiple values
onError(
pathToArray(itemPath),
itemValue,
new GraphQLError(
`Expected type "${inspect(itemType)}" to be a list.`,
),
);
}
return coerceInputValueImpl(itemValue, itemType, onError, itemPath);
});
}
Expand Down