Skip to content

Instantly share code, notes, and snippets.

@exogen
Last active October 26, 2017 18:06
Show Gist options
  • Save exogen/d5ddf86dd7f9efd15d3fabdace759abd to your computer and use it in GitHub Desktop.
Save exogen/d5ddf86dd7f9efd15d3fabdace759abd to your computer and use it in GitHub Desktop.
Simple GraphQL server
const express = require('express')
const graphqlHTTP = require('express-graphql')
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema
const schema = makeExecutableSchema({
typeDefs: `
type Query {
user: User
}
type User {
id: ID!
firstName: String
lastName: String
fullName: String
}
`,
resolvers: {
Query: {
user: () => {
// Replace this with the result of a database or network call...
// Whatever we return here will be the first argument in any User field resolver.
return Promise.resolve({ id: 1, firstName: 'John', lastName: 'Smith' })
}
},
User: {
fullName: (user) => `${user.firstName} ${user.lastName}`
// The resolvers for the remaining fields just default to returning `user[fieldName]`.
}
}
})
const app = express()
app.use('/', graphqlHTTP({ schema, graphiql: true }))
app.listen(3000, () => console.log('Listening on: http://localhost:3000/'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment