Created
June 24, 2019 17:44
-
-
Save chris001177/045b6daef88d410808576e6e827ecddb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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