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/ws] Allow to configure headers #6540

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/cuddly-ants-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/executor-graphql-ws': minor
---

Allow to configure headers of the WebSocket sent with the connection request.
EmrysMyrddin marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 16 additions & 1 deletion packages/executors/graphql-ws/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const defaultPrintFn = memoize1(print);
interface GraphQLWSExecutorOptions extends ClientOptions {
onClient?: (client: Client) => void;
print?: typeof print;
/**
* Additional headers to include when querying the original schema
EmrysMyrddin marked this conversation as resolved.
Show resolved Hide resolved
*/
headers?: Record<string, string>;
}

function isClient(client: Client | GraphQLWSExecutorOptions): client is Client {
Expand All @@ -31,8 +35,19 @@ export function buildGraphQLWSExecutor(
if (clientOptionsOrClient.print) {
printFn = clientOptionsOrClient.print;
}

const webSocketImpl = clientOptionsOrClient.headers
? class WebSocketWithHeaders extends WebSocket {
constructor(url: string, protocol: string) {
super(url, protocol, {
headers: (clientOptionsOrClient as GraphQLWSExecutorOptions).headers,
});
}
}
: WebSocket;

graphqlWSClient = createClient({
webSocketImpl: WebSocket,
webSocketImpl,
lazy: true,
...clientOptionsOrClient,
connectionParams: () => {
Expand Down
Loading