Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 34a96aa

Browse files
edusigacao
authored andcommitted
Adds condition to only create a ws client when executing a subscription operation
1 parent 26e3805 commit 34a96aa

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/client/network-helper.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,30 @@ export class NetworkHelper {
3939
endpoint: Endpoint
4040
updateCallback: (data: string, operation: string) => void
4141
}) {
42-
const wsEndpointURL = endpoint.url.replace(/^http/, "ws")
43-
const wsClient = createWSClient({
44-
url: wsEndpointURL,
45-
connectionAckWaitTimeout: 3000,
46-
webSocketImpl: ws,
47-
})
48-
4942
const { rejectUnauthorized } = workspace.getConfiguration("vscode-graphql")
5043
// this is a node specific setting that can allow requests against servers using self-signed certificates
5144
// it is similar to passing the nodejs env variable flag, except configured on a per-request basis here
5245
const agent = new Agent({ rejectUnauthorized })
5346

47+
const exchanges = [...defaultExchanges];
48+
if (operation === "subscription") {
49+
const wsEndpointURL = endpoint.url.replace(/^http/, "ws")
50+
const wsClient = createWSClient({
51+
url: wsEndpointURL,
52+
connectionAckWaitTimeout: 3000,
53+
webSocketImpl: ws,
54+
})
55+
exchanges.push(
56+
subscriptionExchange({
57+
forwardSubscription: operation => ({
58+
subscribe: sink => ({
59+
unsubscribe: wsClient.subscribe(operation, sink),
60+
}),
61+
}),
62+
}),
63+
)
64+
}
65+
5466
return createClient({
5567
url: endpoint.url,
5668
fetch,
@@ -60,16 +72,7 @@ export class NetworkHelper {
6072
// @ts-expect-error
6173
agent,
6274
},
63-
exchanges: [
64-
...defaultExchanges,
65-
subscriptionExchange({
66-
forwardSubscription: operation => ({
67-
subscribe: sink => ({
68-
unsubscribe: wsClient.subscribe(operation, sink),
69-
}),
70-
}),
71-
}),
72-
],
75+
exchanges,
7376
})
7477
}
7578

0 commit comments

Comments
 (0)