WebSocket connection cannot be established when used in graphql client

1.5k Views Asked by At

i am trying to fetch data to the client through subscriptions from a graphql server. in order to do so we need to establish a websocket connection first. However when I try to do it i get the following error back

error:

WebSocket connection to 'ws://localhost:8888/' failed: Unexpected response code: 400
liveReload — index.js:53

client side code:

import { WebSocketLink } from "@apollo/client/link/ws";
const wsLink = new WebSocketLink({
  uri: "ws://localhost:8888",
  options: {
    reconnect: false
  }
});

i am 100% certain that localhost:8888 is where the graphql server is hosted.i have tried both in safari and firefox

1

There are 1 best solutions below

0
On

you have to specify the subscription path explicitly on the graphql server declaration as below

const server = new ApolloServer({
  typeDefs,
  resolvers,
  subscriptions: {
    **path: "/subscriptions"**
  },
  context: {
    pubsub
  }
});