Skip to content

Instantly share code, notes, and snippets.

@chris001177
Created June 24, 2019 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris001177/045b6daef88d410808576e6e827ecddb to your computer and use it in GitHub Desktop.
Save chris001177/045b6daef88d410808576e6e827ecddb to your computer and use it in GitHub Desktop.
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express')
const { makeExecutableSchema } = require('graphql-tools')
const typeDefs = `
type Query {
hello(name: String): String!
}
`
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name || 'World'}`,
},
}
const myGraphQLSchema = makeExecutableSchema({ typeDefs, resolvers })
const PORT = 4000
const app = express()
// app.use(cors()) // not having cors enabled will cause an access control error
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema }))
app.get('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }))
console.log(`Server listening on http://localhost:${PORT} ...`)
app.listen(PORT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment