From 45e9ebed191cc53abafefd1dc3a437f50a27dcae Mon Sep 17 00:00:00 2001 From: Abhinand C Date: Fri, 3 Mar 2023 23:48:35 +0530 Subject: [PATCH 1/2] fix(List Input Coercion): Adds validation code for nested list with multiple values --- src/utilities/coerceInputValue.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/utilities/coerceInputValue.ts b/src/utilities/coerceInputValue.ts index d1decf86a1..8fb010d1e1 100644 --- a/src/utilities/coerceInputValue.ts +++ b/src/utilities/coerceInputValue.ts @@ -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); }); } From 2cd48780c604c8365f3c5a1c7bf790a99546556b Mon Sep 17 00:00:00 2001 From: Abhinand C Date: Fri, 3 Mar 2023 23:53:52 +0530 Subject: [PATCH 2/2] feat(Input Coercion Tests): Updates test to support proper nested input coercion --- .../__tests__/coerceInputValue-test.ts | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/utilities/__tests__/coerceInputValue-test.ts b/src/utilities/__tests__/coerceInputValue-test.ts index 5e0199fead..917b920647 100644 --- a/src/utilities/__tests__/coerceInputValue-test.ts +++ b/src/utilities/__tests__/coerceInputValue-test.ts @@ -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]]); }); });