-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Comments
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. |
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. |
Works like a charm. Would you like me to create a pull-request for an example in the readme? |
that would be great! |
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) |
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 likecreateWsClient
. But it does not seem to exist.So my questions are:
The text was updated successfully, but these errors were encountered: