Skip to content

Instantly share code, notes, and snippets.

@gimenete
Last active August 17, 2017 15:14
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 gimenete/77b1b406059a313676175fffb3a390ba to your computer and use it in GitHub Desktop.
Save gimenete/77b1b406059a313676175fffb3a390ba to your computer and use it in GitHub Desktop.
#!/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'))
{
"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 <gimenete@gimenete.net>",
"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