Skip to content

Instantly share code, notes, and snippets.

@pdlug pdlug/index.ts
Last active Oct 21, 2018

Embed
What would you like to do?
Example of embedding Apollo GraphQL server inside next.js
import * as express from 'express'
import * as next from 'next'
import { ApolloServer, gql } from 'apollo-server-express'
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev, quiet: false })
const handle = app.getRequestHandler()
const typeDefs = gql`
type Query {
status: String
}
`
const resolvers = {
Query: {
status: () => 'ok'
}
}
const apollo = new ApolloServer({typeDefs, resolvers})
app.prepare().then(() => {
const server = express()
apollo.applyMiddleware({ app: server })
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.