Skip to content

Instantly share code, notes, and snippets.

@neerajkumar161
Last active December 29, 2021 14:15
Show Gist options
  • Save neerajkumar161/d100f638cf091f0de0f9b56b927c3092 to your computer and use it in GitHub Desktop.
Save neerajkumar161/d100f638cf091f0de0f9b56b927c3092 to your computer and use it in GitHub Desktop.
server.ts
import {ApolloServerPluginDrainHttpServer} from 'apollo-server-core'
import {ApolloServer} from 'apollo-server-express'
import config from 'config'
import express from 'express'
import {GraphQLError} from 'graphql'
import http from 'http'
import {connectMongoDB} from './db/mongodb'
import {mainSchema} from './src/schema-loader' // will create in next steps, skip for now
const port = config.get('port') || 5000
const app = express()
const httpServer = http.createServer(app)
const formatError = (error: GraphQLError) => {
console.log(error)
const apiError: any = error.originalError
return {
message: apiError.message,
status: 500
}
}
const server = new ApolloServer({
schema: mainSchema, // will create in next steps, skip for now
context: () => {
return { message: 'This is context used by all resolvers' }
},
formatError,
plugins: [ApolloServerPluginDrainHttpServer({httpServer})]
})
const startApolloServer = async () => {
await server.start()
server.applyMiddleware({app})
httpServer.listen({port})
connectMongoDB()
console.log(
`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`
)
}
startApolloServer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment