Skip to content

fix(coerce-input-value): input object coercion rejects arrays #4367

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

Merged
merged 3 commits into from
Apr 5, 2025
Merged
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
33 changes: 33 additions & 0 deletions src/utilities/__tests__/coerceInputValue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
});

Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/coerceInputValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function coerceInputValueImpl(
}

if (isInputObjectType(type)) {
if (!isObjectLike(inputValue)) {
if (!isObjectLike(inputValue) || Array.isArray(inputValue)) {
onError(
pathToArray(path),
inputValue,
Expand Down