Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save s-h-a-d-o-w/7f58b2f22bab36afee022fcf9ff5c45b to your computer and use it in GitHub Desktop.
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)
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