Created
April 11, 2017 09:58
-
-
Save svsh227/65bb434fccc331237f361805c9d8e01d 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
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