I have a project set up with Apollo Server with express. I have been trying to set up subscriptions but it is just not happening. I have followed all the steps as per the following link :
https://typegraphql.com/docs/subscriptions.html
Further, I wasn't able to listen when I ran my subscription initially. So I wrapped my express app with an http server and in my app.listen, I added a new subscription server and then the subscription was listening. But now , when I run the mutation that is supposed to trigger the subscription, it doesn't get triggered. I have been trying a bunch of things and nothing on SO or Github has helped me so far.
The following is the code of the server
import express from "express";
import { ApolloServer } from "apollo-server-express";
import cors from "cors";
import { SubscriptionServer } from "subscriptions-transport-ws";
import { execute, subscribe } from "graphql";
import { createServer } from "http";
const main = () => {
const app = express();
app.use(
cors({
origin: "http://localhost:3000",
credentials: true,
})
);
const apolloServer = new ApolloServer({
playground: {
settings: {
"request.credentials": "include",
},
},
schema: await buildSchema({
resolvers: [Resolver1, Resolver2, Resolver3],
validate: false,
}),
context: ({ req, res }) => ({ req, res, redis }),
});
apolloServer.applyMiddleware({
app,
cors: false,
});
const server = createServer(app);
server.listen(4000, async () => {
console.log("Server running on port 4000");
new SubscriptionServer(
{
execute,
subscribe,
schema: await buildSchema({
resolvers: [Resolver1, Resolver2, Resolver3],
validate: false,
}),
},
{
server,
path: "/graphql",
}
);
});
};
Please let me know if I'm doing anything wrong or guide me in the right direction.
This should do:-