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

[executors/http] wrap upstream graphql errors in a GraphQLError instance #6299

Merged
merged 7 commits into from
Jul 5, 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/dry-rice-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-tools/executor-http": patch
---

When proxying the requests to the HTTP executor, it should return \`GraphQLError\` instances in \`errors\` array
21 changes: 21 additions & 0 deletions packages/executors/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,27 @@ export function buildHTTPExecutor(
],
};
}
if (Array.isArray(parsedResult.errors)) {
return {
...parsedResult,
errors: parsedResult.errors.map(
({
message,
...options
}: {
message: string;
extensions: Record<string, unknown>;
}) =>
createGraphQLError(message, {
...options,
extensions: {
code: 'DOWNSTREAM_SERVICE_ERROR',
...(options.extensions || {}),
},
}),
),
};
}
return parsedResult;
} catch (e: any) {
return {
Expand Down
29 changes: 27 additions & 2 deletions packages/executors/http/tests/buildHTTPExecutor.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createServer, Server } from 'http';
import { parse } from 'graphql';
import { createGraphQLError, ExecutionResult } from '@graphql-tools/utils';
import { GraphQLError, parse } from 'graphql';
import { createGraphQLError, ExecutionResult, isAsyncIterable } from '@graphql-tools/utils';
import { ReadableStream, Request, Response } from '@whatwg-node/fetch';
import { assertAsyncIterable } from '../../../loaders/url/tests/test-utils.js';
import { buildHTTPExecutor } from '../src/index.js';
Expand Down Expand Up @@ -255,4 +255,29 @@ describe('buildHTTPExecutor', () => {
}),
).toThrow('Executor was disposed. Aborting execution');
});
it('should return return GraphqlError instances', async () => {
const executor = buildHTTPExecutor({
useGETForQueries: true,
fetch() {
return Response.json({ errors: [{ message: 'test error' }] });
},
});

const result = await executor({
document: parse(/* GraphQL */ `
query {
hello
}
`),
});

if (isAsyncIterable(result)) {
throw new Error('Expected result to be an ExecutionResult');
}

expect(result.errors?.[0]).toBeInstanceOf(GraphQLError);
expect(result.errors?.[0]?.extensions).toMatchObject({
code: 'DOWNSTREAM_SERVICE_ERROR',
});
});
});
Loading