Skip to content

Instantly share code, notes, and snippets.

@kkworden
Last active December 10, 2018 19:29
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 kkworden/e1e105dcc407bd195ade65881d894864 to your computer and use it in GitHub Desktop.
Save kkworden/e1e105dcc407bd195ade65881d894864 to your computer and use it in GitHub Desktop.
Execute GraphQL Query Inside of Endpoint
'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