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

Commits on Jul 14, 2020

  1. Expose WS implementation options

    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`.
    sekoyo committed Jul 14, 2020
    Configuration menu
    Copy the full SHA
    b797aff View commit details
    Browse the repository at this point in the history