Skip to content

Instantly share code, notes, and snippets.

@jamesmoriarty
Last active November 9, 2017 02:03
Show Gist options
  • Save jamesmoriarty/dbeed79f9b52c7567390ac04eea10a97 to your computer and use it in GitHub Desktop.
Save jamesmoriarty/dbeed79f9b52c7567390ac04eea10a97 to your computer and use it in GitHub Desktop.
express-graphql, request-promise, graphql-tools, resolvers
import * as express from 'express';
import * as request from 'request-promise';
import * as graphqlHTTP from 'express-graphql';
import { makeExecutableSchema } from 'graphql-tools';
const typeDefs = `
type V2 {
origin: String
title: String
}
type Query {
getV2(id: String!, apiKey: String!): V2
}
schema {
query: Query
}`
const resolvers = {
V2: {
origin(json) { return json["origin"]; },
title(json) { return json["title"]; }
},
Query: {
getV2: async (root, { id, apiKey }) => { return root.get(id, apiKey); }
}
}
const schema = makeExecutableSchema({ typeDefs, resolvers });
const root = {
get: async (id, apiKey) => {
const body = await request.get(`http://api.newsapi.com.au/content/v2/${ id }?api_key=${ apiKey }`)
return JSON.parse(body);
}
};
const app = express();
app.use('/graphql', graphqlHTTP(async (req, res, params) => ({
schema: schema,
rootValue: root,
graphiql: true
})));
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment