To make server with expressjs, apollo-server, and graphql #7797
Unanswered
naeemkhan9293
asked this question in
General
Replies: 2 comments
-
@naeemkhan9293 I recommend using the apollo-server-express package instead to initialize your Apollo Sever. It was made specifically for the Apollo Server to work within an Express application. Example sourced from the docs:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
@naeemkhan9293 what is the question? Is this answered by @qu8n's example? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
const express = require("express");
const { createHandler } = require("graphql-http/lib/use/express");
const cors = require("cors");
const schema = require("./schema");
const { ApolloServer } = require("@apollo/server");
const { expressMiddleware } = require("@apollo/server/express4");
const http = require("http");
const app = express();
const {
ApolloServerPluginDrainHttpServer,
} = require("@apollo/server/plugin/drainHttpServer");
const port = 4000;
const httpServer = http.createServer(app);
const bodyParser = require("body-parser");
const corsOptions = {
origin: "http://localhost:4000/",
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
};
const server = new ApolloServer({
schema,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
server.start().then(() => {
app.use(cors(), bodyParser.json(), expressMiddleware(server));
httpServer.listen(port, () => {
console.log(
🚀 Server ready at http://localhost:${port}
);});
});
Beta Was this translation helpful? Give feedback.
All reactions