Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Expose WS implementation options #1324

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Expose WS implementation options #1324

wants to merge 1 commit into from

Conversation

sekoyo
Copy link

@sekoyo sekoyo commented Jul 14, 2020

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:

  • Make sure all of new logic is covered by tests and passes linting
  • Update CHANGELOG.md with your change

Hi, the `subscriptions-transport-ws` package has a 4th option:

```js
constructor(
    url: string,
    options?: ClientOptions,
    webSocketImpl?: any,
    webSocketProtocols?: string | string[],
  )
```

Which is called here:

```js
  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:

```js
const wsLink = new ApolloLink((operation) => {
  const context = operation.getContext().graphqlContext

  const headers = {
    'content-type': 'application/json',
    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 much more easily than waiting and parsing a `connection_init` message which contains `connectionParams`.
@apollo-cla
Copy link

@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/

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants