Last active
August 17, 2017 15:14
-
-
Save gimenete/77b1b406059a313676175fffb3a390ba 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
#!/usr/bin/env node | |
const express = require('express') | |
const graphqlHTTP = require('express-graphql') | |
const { buildSchema } = require('graphql') | |
const gql = ` | |
type Message { | |
id: Int! | |
text: String! | |
} | |
type User { | |
id: Int! | |
name: String! | |
getMessages(limit: Int): [Message] | |
} | |
type Query { | |
listUsers(limit: Int): [User] | |
} | |
` | |
const schema = buildSchema(gql) | |
const root = { | |
listUsers(params) { | |
// You can use params.limit | |
// You can return a promise | |
return [ | |
{ | |
id: 1, | |
name: 'user1', | |
getMessages(params) { | |
// You can use params.limit | |
// You can return a promise | |
return [ | |
{ | |
id: 1, | |
text: 'message 1' | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
var app = express() | |
app.use( | |
'/graphql', | |
graphqlHTTP({ | |
schema: schema, | |
rootValue: root, | |
graphiql: true | |
}) | |
) | |
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql')) |
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
{ | |
"name": "graphql-example", | |
"version": "0.1.0", | |
"description": "A simple GraphQL example", | |
"main": "index.js", | |
"scripts": { | |
"dev": "nodemon index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Alberto Gimeno <[email protected]>", | |
"license": "MIT", | |
"bin": "./index.js", | |
"dependencies": { | |
"express": "^4.14.0", | |
"express-graphql": "^0.5.4", | |
"graphql": "^0.7.1" | |
}, | |
"devDependencies": { | |
"nodemon": "^1.11.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment