Skip to content

Commit 5adeb7f

Browse files
fix(coerce-input-value): input object coercion rejects arrays (#4367)
1 parent 0a848cc commit 5adeb7f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/utilities/__tests__/coerceInputValue-test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,19 @@ describe('coerceInputValue', () => {
183183
});
184184

185185
describe('for GraphQLInputObject', () => {
186+
const DeepObject = new GraphQLInputObjectType({
187+
name: 'DeepObject',
188+
fields: {
189+
foo: { type: new GraphQLNonNull(GraphQLInt) },
190+
bar: { type: GraphQLInt },
191+
},
192+
});
186193
const TestInputObject = new GraphQLInputObjectType({
187194
name: 'TestInputObject',
188195
fields: {
189196
foo: { type: new GraphQLNonNull(GraphQLInt) },
190197
bar: { type: GraphQLInt },
198+
deepObject: { type: DeepObject },
191199
},
192200
});
193201

@@ -271,6 +279,31 @@ describe('coerceInputValue', () => {
271279
},
272280
]);
273281
});
282+
283+
it('returns an error for an array type', () => {
284+
const result = coerceValue([{ foo: 1 }, { bar: 1 }], TestInputObject);
285+
expectErrors(result).to.deep.equal([
286+
{
287+
error: 'Expected type "TestInputObject" to be an object.',
288+
path: [],
289+
value: [{ foo: 1 }, { bar: 1 }],
290+
},
291+
]);
292+
});
293+
294+
it('returns an error for an array type on a nested field', () => {
295+
const result = coerceValue(
296+
{ foo: 1, deepObject: [1, 2, 3] },
297+
TestInputObject,
298+
);
299+
expectErrors(result).to.deep.equal([
300+
{
301+
error: 'Expected type "DeepObject" to be an object.',
302+
path: ['deepObject'],
303+
value: [1, 2, 3],
304+
},
305+
]);
306+
});
274307
});
275308

276309
describe('for GraphQLInputObject that isOneOf', () => {

src/utilities/coerceInputValue.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function coerceInputValueImpl(
8686
}
8787

8888
if (isInputObjectType(type)) {
89-
if (!isObjectLike(inputValue)) {
89+
if (!isObjectLike(inputValue) || Array.isArray(inputValue)) {
9090
onError(
9191
pathToArray(path),
9292
inputValue,

0 commit comments

Comments
 (0)