HexxNine
HexxNine

Reputation: 456

How do I change the subscription server endpoint in Apollo express server?

When creating an Apollo Server with express the http and subscriptions endpoints default to /graphql. When changing the http endpoint to something other than /graphql the subscription endpoint stays pointed at /graphql. How do I make my subscription endpoint the same as my http endpoint?

Here is the example from Apollo website where I only add path: custom_endpoint

const { ApolloServer } = require('apollo-server-express');
const express = require('express');

const PORT = 4000;
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });

server.applyMiddleware({app, path: custom_endpoint})

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

// ⚠️ Pay attention to the fact that we are calling `listen` on the http server variable, and not on `app`.
httpServer.listen(PORT, () => {
  console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`) //this changes to my custom_endpoint
  console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`) // this does not chnage to my custome_endpoint.```

Upvotes: 3

Views: 2763

Answers (2)

Liran Brimer
Liran Brimer

Reputation: 3556

initialize the ApolloServer instance as follows:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  subscriptions: { path: '/custom-graphql-ws' }
});

Upvotes: -1

Daniel Rearden
Daniel Rearden

Reputation: 84837

The ApolloServer constructor accepts a subscriptions parameter that can be used to customize the subscription endpoint as shown in the docs.

subscriptions: <Object> | <String> | false

String defining the path for subscriptions or an Object to customize the subscriptions server. Set to false to disable subscriptions

  • path: <String>
  • keepAlive: <Number>
  • onConnect: <Function>
  • onDisconnect: <Function>

Upvotes: 1

Related Questions