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

GraphQL replication: WebSocket client headers support #4113

Closed
nirvdrum opened this issue Nov 6, 2022 · 5 comments
Closed

GraphQL replication: WebSocket client headers support #4113

nirvdrum opened this issue Nov 6, 2022 · 5 comments

Comments

@nirvdrum
Copy link
Contributor

nirvdrum commented Nov 6, 2022

As part of my RxDB 11 -> 13 upgrade, I've tried to move away from the old style of GraphQL subscriptions via custom WebSockets to RxDB's built-in support. I was confused by why the headers were passed to the streamQueryBuilder at first and thought maybe that was just a way to pass state through the system. But, it looks like RxDB is expecting the headers to be part of the GraphQL subscription query. Unfortunately, that's not how all GraphQL servers work.

In my case, I'm using Hasura. It expects the header to be a true HTTP header and to the best of my knowledge, there's no option to pass the token as part of the subscription itself. Instead, the header should be passed as part of the createClient call. My old code for doing so looks like:

export function createWebSocketClient(
  tokenFetcher: Auth0ContextInterface['getAccessTokenSilently']
): Client {
  return createClient({
    url: GRAPHQL_WS_URL,
    connectionParams: async () => {
      const token = await tokenFetcher();

      return {
        headers: { authorization: `Bearer ${token}` }
      };
    }
  });
}

Obviously, we wouldn't need any of the Auth0 stuff. RxDB already has the headers in mutableClientState.headers. I think it just needs an option to pass the header through in getGraphQLWebSocket.

@pubkey
Copy link
Owner

pubkey commented Nov 6, 2022

Yes sounds good. You can make a PR if you need this.

@pubkey pubkey closed this as completed Nov 6, 2022
@L-U-C-K-Y
Copy link
Contributor

As part of my RxDB 11 -> 13 upgrade, I've tried to move away from the old style of GraphQL subscriptions via custom WebSockets to RxDB's built-in support. I was confused by why the headers were passed to the streamQueryBuilder at first and thought maybe that was just a way to pass state through the system. But, it looks like RxDB is expecting the headers to be part of the GraphQL subscription query. Unfortunately, that's not how all GraphQL servers work.

In my case, I'm using Hasura. It expects the header to be a true HTTP header and to the best of my knowledge, there's no option to pass the token as part of the subscription itself. Instead, the header should be passed as part of the createClient call. My old code for doing so looks like:

export function createWebSocketClient(

  tokenFetcher: Auth0ContextInterface['getAccessTokenSilently']

): Client {

  return createClient({

    url: GRAPHQL_WS_URL,

    connectionParams: async () => {

      const token = await tokenFetcher();



      return {

        headers: { authorization: `Bearer ${token}` }

      };

    }

  });

}

Obviously, we wouldn't need any of the Auth0 stuff. RxDB already has the headers in mutableClientState.headers. I think it just needs an option to pass the header through in getGraphQLWebSocket.

@nirvdrum are you using native hasura queries/mutation/subscription/streams to implement the replication or do you have a remote schema integrated that handled the logic?

@nirvdrum
Copy link
Contributor Author

@L-U-C-K-Y I'm using a pretty stock Hasura configuration. No remote schemas, just the generated GraphQL API for my Postgres database.

@idesignpixels
Copy link

@nirvdrum Any luck with this?, I have the same issue.

@nirvdrum
Copy link
Contributor Author

Unfortunately, no. I just removed subscriptions for the time being. I think I'm going to go back to managing the WebSocket and replication state manually.

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

No branches or pull requests

4 participants