Skip to content

Instantly share code, notes, and snippets.

@arockwell
Created December 12, 2017 20:29
Show Gist options
  • Save arockwell/41efc60eb1d60b3e9593297f45d367bd to your computer and use it in GitHub Desktop.
Save arockwell/41efc60eb1d60b3e9593297f45d367bd to your computer and use it in GitHub Desktop.
Simple GraphQL proxy
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