Skip to content

Instantly share code, notes, and snippets.

@matthewmueller
Created July 6, 2016 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewmueller/0b1d147081e5b9cc78144cdeaaf2539b to your computer and use it in GitHub Desktop.
Save matthewmueller/0b1d147081e5b9cc78144cdeaaf2539b to your computer and use it in GitHub Desktop.
Simple Graph.ql server for tying in the Stripe + Diffbot services
let Graph = require('graph.ql')
module.exports = Graph(`
scalar Date
enum Size {
SMALL
MEDIUM
LARGE
}
type Feed {
# Feed ID
id: ID!
# Name of the author
author: String!
# Get the thumbnail of the author
# @param {Size} size - specify a thumbnail size
# @return {String} a URL of the string
thumbnail(size: Size): String
# Description
# @param {Int} length - specify a length
# @return {String} a description
description(length: Int): String
# Content of the article
content: String!
# Published
date_published: Date!
}
type User {
id: ID!
customer: Customer
}
type Customer {
stripe_id: ID!
delinquent: Boolean!
plan: String!
quantity: Int!
}
type Query {
# Fetch the contents of an article
# @param {String} token - Diffbot token
# @param {String} source - Source of the article as a URL
# @return {Feed} object containing the feed
feed(token: String!, source: String!): Feed
user(id: ID!): User
}
`, {
// Scalar resolvers
Date: {
serialize (v) {
return v.toISOString()
},
parse (v) {
return new Date(v)
}
},
// Feed model
//
// Note: I envision each one of these computed properties to be lambda functions.
Feed: {
thumbnail (feed, { size = 'medium' }) {
// assumes the diffbot object returns
// a shape like this: { thumbnails: { small: url, medium: url, large: url }}
return feed.thumbnails[size]
},
// assumes the diffbot object returns
// a shape like this: { thumbnails: { small: url, medium: url, large: url }}
description (feed, { length = 100 }) {
return feed.description.slice(0, length) + '...'
}
},
User: {
// fetch the user from stripe
* customer(user) {
// user.stripe_id is returned from the user's table,
// but isn't exposed by the GraphQL server
if (!user.stripe_id) return null
return yield stripe.customers.retrieve(user.stripe_id)
}
},
Query: {
// query diffbot
* feed (query, params) {
let response = yield request
.get(params.url)
.send('token', params.token)
.end()
if (!response.ok) throw new Error(response.text)
// big data object from Diffbot
return response.body
},
* user (query, params) {
// implicitly assumed state is bound and connected on this
return yield this.postgres.query(`
SELECT * FROM users WHERE id = :id
`, { id: id })
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment