Created
December 12, 2017 20:29
-
-
Save arockwell/41efc60eb1d60b3e9593297f45d367bd to your computer and use it in GitHub Desktop.
Simple GraphQL proxy
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'); | |
var fetch = require('node-fetch'); | |
// Construct a schema, using GraphQL schema language | |
var schema = buildSchema(` | |
type User { | |
first_name: String | |
last_name: String | |
email: String | |
} | |
type Query { | |
getUsers: [User] | |
} | |
`); | |
// The root provides a resolver function for each API endpoint | |
var root = { | |
getUsers: () => { | |
return fetch('http://localhost:5000/users').then( | |
function(response) { | |
return response.json(); | |
} | |
); | |
} | |
}; | |
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