Skip to content
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

HTTP executor should error when both data and errors fields are empty #5902

Merged
merged 2 commits into from
Feb 15, 2024
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
5 changes: 5 additions & 0 deletions .changeset/rare-bulldogs-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-tools/executor-http": patch
---

Error when both data and errors fields are empty
21 changes: 20 additions & 1 deletion packages/executors/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,26 @@ export function buildHTTPExecutor(
if (typeof result === 'string') {
if (result) {
try {
return JSON.parse(result);
const parsedResult = JSON.parse(result);
if (
parsedResult.data == null &&
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to check for undefined as well. E.g. the response is {notGraphql: true}

Copy link
Owner

@ardatan ardatan Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== null (not ===) should check for both undefined and null.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 woops
I'm too used to strict typescript rules. No == for me :)

(parsedResult.errors == null || parsedResult.errors.length === 0)
) {
return {
errors: [
createGraphQLError('Unexpected empty "data" and "errors" fields', {
extensions: {
requestBody: {
query,
operationName: request.operationName,
},
responseDetails: responseDetailsForError,
},
}),
],
};
}
return parsedResult;
} catch (e: any) {
return {
errors: [
Expand Down
31 changes: 31 additions & 0 deletions packages/executors/http/tests/buildHTTPExecutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ describe('buildHTTPExecutor', () => {
],
});
});
it.each([
JSON.stringify({ data: null, errors: null }),
JSON.stringify({ data: null }),
JSON.stringify({ data: null, errors: [] }),
JSON.stringify({ errors: null }),
JSON.stringify({ errors: [] }),
])('should error when both data and errors fields are empty %s', async body => {
const executor = buildHTTPExecutor({
fetch: () =>
new Response(body, {
status: 200,
headers: {
'content-type': 'application/json',
},
}),
});
const result = await executor({
document: parse(/* GraphQL */ `
query {
hello
}
`),
});
expect(result).toMatchObject({
errors: [
{
message: 'Unexpected empty "data" and "errors" fields',
},
],
});
});
it('should use GET for subscriptions by default', async () => {
let method: string = '';
const executor = buildHTTPExecutor({
Expand Down
Loading