You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
I'm trying to setup subscription with react hooks and apollo-client. I'm using apollo-server-hapi in the backend to handle GraphQL with Hapi. I'm pretty confident my backend is setup correctly given I can see when a new client subscribe and furthermore I can get subscription to work with graphiql.
Client setup
index.js
import{ApolloClient}from"apollo-client";import{InMemoryCache}from"apollo-cache-inmemory";import{ApolloProvider}from"@apollo/react-hooks";import{WebSocketLink}from"apollo-link-ws";import{getMainDefinition}from"apollo-utilities";import{split}from"apollo-link";import{HttpLink}from"apollo-link-http";consthttpLink=newHttpLink({uri: "http://localhost:3000/graphql"});constwsLink=newWebSocketLink({uri: `ws://localhost:3000/graphql`,options: {reconnect: true,connectionParams: async()=>{return"hello world";}}});constlink=split(// split based on operation type({ query })=>{const{ kind, operation }=getMainDefinition(query);returnkind==="OperationDefinitions"&&operation==="subscription";},wsLink,httpLink);constclient=newApolloClient({cache: newInMemoryCache(),
link
});ReactDOM.render(<Providerstore={store}><BrowserRouter><ApolloProviderclient={client}><MyAppContent/></ApolloProvider></BrowserRouter></Provider>,document.getElementById("root"));
import{PubSub}from"graphql-subscriptions";import{ApolloServer,gql}from"apollo-server-hapi";setTimeout(()=>{console.log("pubsub trigger");pubsub.publish("somethingChanged",{somethingChanged: {id: 1,content: "Hello!"}});},20*1000);consttypeDefs=gql` type Query { dummy: String } type Subscription { somethingChanged: Result } type Result { id: String }`;constresolvers={Subscription: {somethingChanged: {subscribe: ()=>{console.log("Someone subscribed to this event");returnpubsub.asyncIterator("somethingChanged");}}}};constserverGQL=newApolloServer({
typeDefs,
resolvers,subscriptions: {onConnect: connectionParams=>console.log("CLIENT CONNECTED",connectionParams)}});// Non relevant Hapi code
More infos
I can see the webscoket connecting to my API in chrome devtools and as I said earlier when I use GraphiQL I can see the log in my server "Someone subscribed to this event".
To finish I can see in XHR that a POST request is made to my API with the subscription content (I guess it's to tell Apollo server someone want to subscribe to this subscription ?)
Chrome devtools
Actual outcome:
I can't get the frontend to subscribe to the somethingChanged subscription however we I open graphiql at http://localhost/graphql and I put the exact same content as in COMMENTS_SUBSCRIPTION I can see that everything is working as expected.
How to reproduce the issue:
I've put as much code as I can to be as thorough as possible to explain my problem.
The text was updated successfully, but these errors were encountered:
Antoine-C
changed the title
Can't subscribe to subscription but can with GraphiQL
Can't subscribe to subscription with React but can by using GraphiQL
Feb 23, 2020
Intended outcome:
I'm trying to setup subscription with react hooks and apollo-client. I'm using apollo-server-hapi in the backend to handle GraphQL with Hapi. I'm pretty confident my backend is setup correctly given I can see when a new client subscribe and furthermore I can get subscription to work with graphiql.
Client setup
index.js
In my component
Server setup
index.ts
More infos
I can see the webscoket connecting to my API in chrome devtools and as I said earlier when I use GraphiQL I can see the log in my server "Someone subscribed to this event".
To finish I can see in XHR that a POST request is made to my API with the subscription content (I guess it's to tell Apollo server someone want to subscribe to this subscription ?)
Chrome devtools
Actual outcome:
I can't get the frontend to subscribe to the somethingChanged subscription however we I open graphiql at http://localhost/graphql and I put the exact same content as in COMMENTS_SUBSCRIPTION I can see that everything is working as expected.
How to reproduce the issue:
I've put as much code as I can to be as thorough as possible to explain my problem.
Version
Infos
The text was updated successfully, but these errors were encountered: