Skip to content

Instantly share code, notes, and snippets.

@nzaghini
Created June 29, 2018 08:07
Show Gist options
  • Save nzaghini/ce54e0a77eeb6cdfce0963b3c7599767 to your computer and use it in GitHub Desktop.
Save nzaghini/ce54e0a77eeb6cdfce0963b3c7599767 to your computer and use it in GitHub Desktop.
Apollo Express setup for GraphQL with Context.
import express from 'express'
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
import bodyParser from 'body-parser'
import schema from './src/schema'
import movieService from './src/dataSource/movieService'
const GRAPHQL_PORT = 3000
const graphQLServer = express()
// by setting the context here, the Service is now available for Resolvers as context.movieService
graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress({ schema, context: { movieService } }))
graphQLServer.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }))
graphQLServer.listen(GRAPHQL_PORT, () =>
console.log(
`GraphiQL is now running on http://localhost:${GRAPHQL_PORT}/graphiql`
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment