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

how to enable subscriptions #247

Closed
barbalex opened this issue Jun 10, 2020 · 5 comments · Fixed by #250
Closed

how to enable subscriptions #247

barbalex opened this issue Jun 10, 2020 · 5 comments · Fixed by #250

Comments

@barbalex
Copy link
Contributor

barbalex commented Jun 10, 2020

I have read:

So basically I do not know how this should work as it is not supported by graphql-request. Yet "The gqlWsClient needs to be set during the store creation to make this possible" seems to imply that it is possible.

I have also searched for an analog method to createHttpClient, something like createWsClient. But it does not seem to exist.

So my questions are:

  • can it really be done?
  • if yes: how?
@chrisdrackett
Copy link
Collaborator

I think example 1 supports subscriptions, but I'm not 100% sure. We don't use them in our app so I have 0 experience with them myself.

@barbalex
Copy link
Contributor Author

O.k., seems that https://github.com/mobxjs/mst-gql/blob/master/src/MSTGQLStore.ts gives some important clues:

Need to

import { SubscriptionClient } from 'subscriptions-transport-ws'

build a ws client:

// see: https://www.npmjs.com/package/subscriptions-transport-ws#hybrid-websocket-transport
const gqlWsClient = new SubscriptionClient(constants.graphQlWsUri, {
  reconnect: true,
  connectionParams: {
    headers: { authorization: `Bearer ${tokenWithRoles}` },
  },
})

add the ws client when creating the store:

// see: https://github.com/mobxjs/mst-gql/blob/master/src/MSTGQLStore.ts#L42-L43
const store = RootStore.create(undefined, {
  gqlHttpClient,
  gqlWsClient,
})

Works for me so far without yet actually using websockets. Will see if I can get that to work too.

@barbalex
Copy link
Contributor Author

Works like a charm. Would you like me to create a pull-request for an example in the readme?

@chrisdrackett
Copy link
Collaborator

that would be great!

@barbalex
Copy link
Contributor Author

Additional caveat:

When using server side rendered tools like gatsby/next/nuxt it is necessary to prevent using subscriptions server side. Otherwise there will be an error because the server is missing a websocket implementation.

This is how I do it in gatsby:

const gqlHttpClient = createHttpClient(constants.graphQlUri)
const tokenWithRoles =
  typeof window !== 'undefined'
    ? window.localStorage.getItem('token') || 'none'
    : 'none'
gqlHttpClient.setHeaders({ authorization: `Bearer ${tokenWithRoles}` })

// ws client only works in the browser
// need to prevent gatsby from executing it server side
// see: https://github.com/apollographql/subscriptions-transport-ws/issues/333#issuecomment-359261024
let gqlWsClient
let storeOptions = {
  gqlHttpClient,
}
if (typeof window !== 'undefined') {
  // https://www.npmjs.com/package/subscriptions-transport-ws#hybrid-websocket-transport
  gqlWsClient = new SubscriptionClient(constants.graphQlWsUri, {
    reconnect: true,
    connectionParams: {
      headers: { authorization: `Bearer ${tokenWithRoles}` },
    },
  })
  // https://github.com/mobxjs/mst-gql/blob/master/src/MSTGQLStore.ts#L42-L43
  storeOptions = {
    gqlHttpClient,
    gqlWsClient,
  }
}

const store = RootStore.create(undefined, storeOptions)

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