Created
October 14, 2019 16:53
-
-
Save s-h-a-d-o-w/7f58b2f22bab36afee022fcf9ff5c45b to your computer and use it in GitHub Desktop.
Next.js + type-graphql (via apollo-server-express)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "reflect-metadata"; | |
import { ApolloServer } from "apollo-server-express"; | |
import compression from "compression"; | |
import express from "express"; | |
import next from "next"; | |
import { buildSchema } from "type-graphql"; | |
import { UserResolver } from "../experiment/resolvers"; | |
const port = process.env.PORT || 3000; | |
const isDev = process.env.NODE_ENV !== "production"; | |
const nextApp = next({ dev: isDev }); | |
const nextRequestHandler = nextApp.getRequestHandler(); | |
async function bootstrap() { | |
await nextApp.prepare(); | |
const server = express(); | |
server.use(compression()); | |
const schema = await buildSchema({ | |
resolvers: [UserResolver], | |
emitSchemaFile: true | |
}); | |
const apolloServer = new ApolloServer({ schema, playground: true }); | |
apolloServer.applyMiddleware({ app: server }); | |
server.get("*", (req, res) => { | |
return nextRequestHandler(req, res); | |
}); | |
server.listen(port, () => { | |
console.log(`> ${isDev ? "Dev" : "Prod"} ready @ Port ${port}`); | |
console.log(`GraphQL API @ ${apolloServer.graphqlPath}`); | |
}); | |
} | |
bootstrap(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment