Last active
October 26, 2017 18:06
-
-
Save exogen/d5ddf86dd7f9efd15d3fabdace759abd to your computer and use it in GitHub Desktop.
Simple GraphQL server
This file contains hidden or 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
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