I am building a GraphQL server and have subscriptions in it. Once I deploy the server on my local machine and check the subscription in Playground it works, i.e. it listens to events and I get the newly added data. It means the subscriptions implementation is correct and there is nothing wrong with them. The subscriptions are working fine when I'm running on local (i.e localhost) but when I deploy the server on live (Heroku) it gives the below error when I listen to subscriptions in Playground
:
{
"error": "Could not connect to websocket endpoint wss://foodesk.herokuapp.com/graphql. Please check if the endpoint url is correct."
}
Just for more information, my queries and mutations are also working on live server, it's just the subscriptions that are not working.
This is my code:
/**
* third party libraries
*/
const bodyParser = require('body-parser');
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const helmet = require('helmet');
const http = require('http');
const mapRoutes = require('express-routes-mapper');
const mkdirp = require('mkdirp');
const shortid = require('shortid');
/**
* server configuration
*/
const config = require('../config/');
const auth = require('./policies/auth.policy');
const dbService = require('./services/db.service');
const { schema } = require('./graphql');
// environment: development, testing, production
const environment = process.env.NODE_ENV;
const graphQLServer = new ApolloServer({
schema,
uploads: false
});
/**
* express application
*/
const api = express();
const server = http.createServer(api);
graphQLServer.installSubscriptionHandlers(server)
const mappedRoutes = mapRoutes(config.publicRoutes, 'api/controllers/');
const DB = dbService(environment, config.migrate).start();
// allow cross origin requests
// configure to allow only requests from certain origins
api.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', '*');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', '*');
// Pass to next layer of middleware
next();
});
// secure express app
api.use(helmet({
dnsPrefetchControl: false,
frameguard: false,
ieNoOpen: false,
}));
// parsing the request bodys
api.use(bodyParser.urlencoded({ extended: false }));
api.use(bodyParser.json());
// public REST API
api.use('/rest', mappedRoutes);
// private GraphQL API
api.post('/graphql', (req, res, next) => auth(req, res, next));
graphQLServer.applyMiddleware({
app: api,
cors: {
origin: true,
credentials: true,
methods: ['POST'],
allowedHeaders: [
'X-Requested-With',
'X-HTTP-Method-Override',
'Content-Type',
'Accept',
'Authorization',
'Access-Control-Allow-Origin',
],
},
playground: {
settings: {
'editor.theme': 'light',
},
},
});
server.listen(config.port, () => {
if (environment !== 'production'
&& environment !== 'development'
&& environment !== 'testing'
) {
console.error(`NODE_ENV is set to ${environment}, but only production and development are valid.`);
process.exit(1);
}
return DB;
});