Reputation: 14450
I'm trying to get websocket subscriptions working with Nuxt Apollo. For my server (8base.com) I need to send along a connectionParams
object with the subscription request.
It seems Nuxt Apollo has a httpLinkOptions
but what I really need is a wssLinkOptions
. Anyone know of a way to do this with Nuxt? Ideally I don't have to replace Nuxt Apollo, as I'm using it all throughout the app.
Upvotes: 4
Views: 2040
Reputation: 686
this is how i configure inside nuxt.config straight forward
modules: [
'@nuxtjs/apollo',
],
apollo: {
clientConfigs: {
default: {
httpEndpoint: 'your_graphql_url'
}
}
},
env: {
WS_URL: 'ws://you_url/ws',
}
Upvotes: -1
Reputation: 14450
So after trying a few different ways, this seems like the best way. Simply overwrite the wsClient
using a plugin.
So in nuxt.config.js
:
plugins: [
{ src: "~/plugins/apollo-ws-client.js", mode: "client" }
],
apollo: {
clientConfigs: {
default: "~/plugins/apollo-config-default.js"
}
},
In plugins/apollo-config-default.js
:
export default function() {
return {
httpEndpoint: "https://api.8base.com/123456",
wsEndpoint: "wss://ws.8base.com"
}
}
Then in plugins/apollo-ws-client.js
export default ({ app }) => {
const client = app.apolloProvider.defaultClient
const token = app.$apolloHelpers.getToken()
if (token) {
client.wsClient.lazy = true
client.wsClient.reconnect = true
client.wsClient.connectionParams = () => {
return {
workspaceId: "123456",
token: token
}
}
}
}
Works great.
Upvotes: 3
Reputation: 872
According to the docs, you can setup a subscription as a WebSocketLink.
https://www.npmjs.com/package/@nuxtjs/apollo/v/3.0.4#example-with-subscription-graphcool-as-example
// Set up subscription
const wsLink = new WebSocketLink({
uri: `wss://subscriptions.graph.cool/v1/${process.env.GRAPHQL_ALIAS}`,
options: {
reconnect: true,
connectionParams: () => {
const token = process.server ? ctx.req.session : window.__NUXT__.state.session
return {
Authorization: token ? `Bearer ${token}` : null
}
}
}
})
And here's the full example:
Nuxt Config:
// nuxt.config.js
apollo:{
clientConfigs:{
default: '~/apollo/client-configs/default.js'
}
}
Default Client Config:
// apollo/client-configs/default.js
import { ApolloLink, concat, split } from 'apollo-link'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { WebSocketLink } from 'apollo-link-ws'
import { getMainDefinition } from 'apollo-utilities'
import 'subscriptions-transport-ws' // this is the default of apollo-link-ws
export default (ctx) => {
const httpLink = new HttpLink({uri: 'https://api.graph.cool/simple/v1/' + process.env.GRAPHQL_ALIAS})
const authMiddleware = new ApolloLink((operation, forward) => {
const token = process.server ? ctx.req.session : window.__NUXT__.state.session
operation.setContext({
headers: {
Authorization: token ? `Bearer ${token}` : null
}
})
return forward(operation)
})
// Set up subscription
const wsLink = new WebSocketLink({
uri: `wss://subscriptions.graph.cool/v1/${process.env.GRAPHQL_ALIAS}`,
options: {
reconnect: true,
connectionParams: () => {
const token = process.server ? ctx.req.session : window.__NUXT__.state.session
return {
Authorization: token ? `Bearer ${token}` : null
}
}
}
})
const link = split(
({query}) => {
const {kind, operation} = getMainDefinition(query)
return kind === 'OperationDefinition' && operation === 'subscription'
},
wsLink,
httpLink
)
return {
link: concat(authMiddleware, link),
cache: new InMemoryCache()
}
}
The Client Config subscription should work using the Vue Apollo model: https://apollo.vuejs.org/guide/apollo/subscriptions.html#setup
If you just need the basics, you may also be able to just specify your HTTP and WS endpints:
apollo:{
clientConfigs:{
default:{
httpEndpoint: YOUR_ENDPOINT,
wsEndpoint: YOUR_WS_ENDPOINT
}
}
}
Upvotes: 5