Skip to content

Instantly share code, notes, and snippets.

@svsh227
Created April 11, 2017 09:58
Show Gist options
  • Save svsh227/65bb434fccc331237f361805c9d8e01d to your computer and use it in GitHub Desktop.
Save svsh227/65bb434fccc331237f361805c9d8e01d to your computer and use it in GitHub Desktop.
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
rollDice(numDice: Int!, numSides: Int): [Int]
}
`);
// The root provides a resolver function for each API endpoint
var root = {
rollDice: function ({numDice, numSides}) {
var output = [];
for (var i = 0; i < numDice; i++) {
output.push(1 + Math.floor(Math.random() * (numSides || 6)));
}
return output;
}
};
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment