const {SubscriptionClient, addGraphQLSubscriptions} = require('subscriptions-transport-ws')
const {ApolloClient, createNetworkInterface} = require('apollo-client')
const ws = require('ws')
const gql = require('graphql-tag')
const networkInterface = createNetworkInterface({
uri: `http://${HOST}:${PORT}/graphql`
})
const wsClient = new SubscriptionClient(`ws://${HOST}:${PORT}/subscriptions`, {
reconnect: true
}, ws)
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
networkInterface,
wsClient
)
const client = new ApolloClient({
networkInterface: networkInterfaceWithSubscriptions
})
client.subscribeToMore({
document: gql`
subscription {
userSubscription {
id
name
}
}
`,
variables: {
token: userToken1
},
updateQuery: (prev, data) => {
console.log(data)
}
})
Error: client.subscribeToMore is not a function
change it to subscribe
client.subscribe({
document: gql`
subscription {
userSubscription {
id
name
}
}
`,
variables: {
token: userToken1
},
updateQuery: (prev, data) => {
console.log(data)
}
})
Error: Cannot read property 'kind' of undefined
Im ussing apollo client inside node
Hello
I have made a PR #266 to fix documentation. Change option document with query, it might work. If it doesn't, please let me know because it could an accumulation of different error. Thank you
Jérôme
client.subscribe({
query: gql`
subscription {
userSubscription {
id
name
}
}
`,
variables: {
token: userToken1
},
updateQuery: (prev, data) => {
console.log(data)
}
})
I currently using wsClient.subscribe but I am facing other issues...
When I was using client.subscribe, no frame for subscription was send.
Yeah, this is frustrating...
Looking at the source, it should look like this. Maybe?
wsClient.subscribe({
query: gql`
subscription {
userSubscription {
id
name
}
}
`,
variables: {
token: userToken1
}
},
(err, result) => {
console.log(err, result)
})
Edit: I tested, and yes it works.
Edit 2:
✖ __typename = 'undefined' value "UserType"
--------------------------------------------------------------------------
operator: equal
expected: undefined
actual: 'UserType'
at: equalTest (/home/edgars/hub/aizkraukle-server/node_modules/tape-schema/index.js:234:11)
stack: |-
It' s seems data recieved by subscription is missing __typename? It is inside standart querys.
Same problem, __typeName is not present and even if it's added on subscription, cache is not updated automatically.
I tried to switch to Full WebSocket Transport with client, query and mutation works fine expect subscription.
I will try to look under the hood with apolloClient and methods subscription.
Hello @mjasnikovs
I just add a pull request with 2 examples for subscribe and subscribeToMore. wsClient was great to debug my graphQL server, but it's not suitable for production application.
Those examples add __typeName and Redux cache is updated automatically if an id is provided.
@Tobino please add a note that your example won't work with apollo-client 2.X version
Most helpful comment
@Tobino please add a note that your example won't work with apollo-client 2.X version