I have a GraphQL server running with Type-GraphQL + Apollo and so I want to use a subscription.
For the frontend I am using NextJS + Apollos client and I am getting following error:
WebSocket connection to: 'ws://localhost:8080/graphql' failed: SubscriptionClient.connect @ client.js?2ed1:427 eval @ client.js?2ed1:396
My code in the frontend client looks like this:
const wsLink = process.browser ? new WebSocketLink({ // if you instantiate in the server, the error will be thrown
uri: `ws://localhost:8080/graphql`,
options: {
reconnect: true
}
}) : null;
const httplink = new HttpLink({
uri: 'http://localhost:8080/graphql',
credentials: "include"
});
const link = process.browser ? split( //only create the split in the browser
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
},
wsLink,
httplink,
) : httplink;
const client = new ApolloClient({
link,
fetch,
credentials: "include",
cache: new InMemoryCache(),
});
export default client;
I switched to apollo-link-ws which fixed it