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.
2
Answers
This should do:-
You don’t need to import SubscriptionServer from subscriptions-transport-ws. Here is a simple GraphQL server template for you.
server.ts
resolver
Note
To answer your question, it’s highly likely that Typescript isn’t reading the file where you wrote your subscription resolver, resulting in no GraphQL schema being created. Be sure to add the following lines to your tsconfig.json file. Sometimes, vscode auto imports paths and add them to this line.