Last active
December 10, 2018 19:29
-
-
Save kkworden/e1e105dcc407bd195ade65881d894864 to your computer and use it in GitHub Desktop.
Execute GraphQL Query Inside of Endpoint
This file contains hidden or 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
'use strict'; | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const graphql = require('graphql'); | |
const app = express(); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.get('/', function(req, res) { | |
res.send('We\'re in business'); | |
}); | |
app.post('/api', function(req, res) { | |
const schema = graphql.buildSchema(` | |
type Query { | |
hello: String | |
} | |
`); | |
const root = { | |
hello: () => { | |
return 'Hello world!'; | |
}, | |
}; | |
graphql.graphql(schema, req.body.query, root).then((response) => { | |
res.send(response); | |
}); | |
}); | |
app.listen(8080, (app) => { | |
console.log('App is listening'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment