apollo-link
apollo-link copied to clipboard
Expose WS implementation options
Hi, the subscriptions-transport-ws
package has a 4th option:
constructor(
url: string,
options?: ClientOptions,
webSocketImpl?: any,
webSocketProtocols?: string | string[],
)
Which is called here:
private connect() {
this.client = new this.wsImpl(this.url, this.wsProtocols);
This is an important option for using Apollo on Node since we typically use the ws
package where the second parameter to ws
is a bunch of important options.
One of these options is the ability to pass HTTP headers for example, which are not limited on the server. This makes it a lot simpler to implement secure subscriptions to other micro-services if the Apollo server is a gateway for example:
const wsLink = new ApolloLink((operation) => {
const context = operation.getContext().graphqlContext
const headers = {
authorization: context.authToken,
}
// Create a new websocket link per request
return new WebSocketLink({
uri: subscriptionsUri,
options: {
reconnect: true,
connectionParams: {
headers,
},
},
webSocketImpl: ws,
webSocketImplOptions: { headers },
}).request(operation)
})
Now we can forward secure subscriptions to other mircro-services as the upgrade request has an authorization
header much more easily than waiting and parsing a connection_init
message which contains connectionParams
.
TODO:
- [x] Make sure all of new logic is covered by tests and passes linting
- [ ] Update CHANGELOG.md with your change
@DominicTobias: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/