diff --git a/src/utilities/__tests__/coerceInputValue-test.ts b/src/utilities/__tests__/coerceInputValue-test.ts index 3692ce26b7..03afca6a6b 100644 --- a/src/utilities/__tests__/coerceInputValue-test.ts +++ b/src/utilities/__tests__/coerceInputValue-test.ts @@ -183,11 +183,19 @@ describe('coerceInputValue', () => { }); describe('for GraphQLInputObject', () => { + const DeepObject = new GraphQLInputObjectType({ + name: 'DeepObject', + fields: { + foo: { type: new GraphQLNonNull(GraphQLInt) }, + bar: { type: GraphQLInt }, + }, + }); const TestInputObject = new GraphQLInputObjectType({ name: 'TestInputObject', fields: { foo: { type: new GraphQLNonNull(GraphQLInt) }, bar: { type: GraphQLInt }, + deepObject: { type: DeepObject }, }, }); @@ -271,6 +279,31 @@ describe('coerceInputValue', () => { }, ]); }); + + it('returns an error for an array type', () => { + const result = coerceValue([{ foo: 1 }, { bar: 1 }], TestInputObject); + expectErrors(result).to.deep.equal([ + { + error: 'Expected type "TestInputObject" to be an object.', + path: [], + value: [{ foo: 1 }, { bar: 1 }], + }, + ]); + }); + + it('returns an error for an array type on a nested field', () => { + const result = coerceValue( + { foo: 1, deepObject: [1, 2, 3] }, + TestInputObject, + ); + expectErrors(result).to.deep.equal([ + { + error: 'Expected type "DeepObject" to be an object.', + path: ['deepObject'], + value: [1, 2, 3], + }, + ]); + }); }); describe('for GraphQLInputObject that isOneOf', () => { diff --git a/src/utilities/coerceInputValue.ts b/src/utilities/coerceInputValue.ts index eeab0614f8..5263e925ee 100644 --- a/src/utilities/coerceInputValue.ts +++ b/src/utilities/coerceInputValue.ts @@ -86,7 +86,7 @@ function coerceInputValueImpl( } if (isInputObjectType(type)) { - if (!isObjectLike(inputValue)) { + if (!isObjectLike(inputValue) || Array.isArray(inputValue)) { onError( pathToArray(path), inputValue,