Last active
October 19, 2016 03:02
-
-
Save gutenye/f8a81155a7500c8501d94d7163476ac9 to your computer and use it in GitHub Desktop.
graphql-thinky.js
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
require('babel-polyfill') | |
const GraphQLThinky = require('graphql-thinky').default | |
const Thinky = require('thinky') | |
const Fixtures = require('rethinkdb-fixtures') | |
const { graphql } = require('graphql') | |
const { makeExecutableSchema } = require('graphql-tools') | |
global.pd = console.log.bind(console) | |
// Thinky //{{{1 | |
const DB = 'test' | |
const thinky = Thinky({db: DB}) | |
const {r, Errors} = thinky | |
const t = thinky.type | |
const Post = thinky.createModel('Post', { | |
title: t.string(), | |
}) | |
const Author = thinky.createModel('Author', { | |
name: t.string(), | |
}) | |
const Comment = thinky.createModel('Comment', { | |
content: t.string(), | |
}) | |
Post.belongsTo(Author, 'author', 'authorId', 'id') | |
Post.hasMany(Comment, 'comments', 'id', 'postId') | |
const graphQLThinky = new GraphQLThinky(thinky) | |
const resolve = graphQLThinky.resolve | |
const FIXTURES = { | |
Author: [ | |
{id: '1', name: 'Guten'}, | |
{id: '2', name: 'Tagen'}, | |
], | |
Post: [ | |
{id: '1', title: 'Facebook', authorId: '1'}, | |
{id: '2', title: 'Google', authorId: '2'}, | |
], | |
Comment: [ | |
{id: '1', content: 'Hello', postId: '1'}, | |
{id: '2', content: 'World', postId: '1'}, | |
], | |
} | |
function fixtures() { | |
return Promise.all(Object.values(thinky.models).map(v => v.ready())).then(() => { | |
const fixtures = Fixtures({db: DB, clear: true}) | |
fixtures.Insert(FIXTURES).then(() => { | |
fixtures.base.close() | |
}) | |
}) | |
} | |
//}}}1 | |
// TypeDefs //{{{1 | |
const typeDefs = [` | |
type Query { | |
post(id: ID): Post | |
posts(limit: Int): [Post] | |
} | |
type Post { | |
id: Int! | |
title: String | |
author: Author | |
comments: [Comment] | |
} | |
type Author { | |
id: Int! | |
name: String | |
} | |
type Comment { | |
id: Int! | |
content: String | |
} | |
`] //}}}1 | |
// Resolvers //{{{1 | |
const resolvers = { | |
Query: { | |
post: resolve('Post'), | |
posts: resolve('Post', null, { | |
before: (opts, args, context, info) => { | |
//pd('before', opts, args) | |
//opts.limit = 2 | |
return opts | |
}, | |
after: (result, opts, args, info) => { | |
//pd('after', result, opts, args) | |
return result | |
}, | |
}) , | |
}, | |
Post: { | |
author: resolve('Post', 'author'), | |
comments: resolve('Post', 'comments'), | |
} | |
} //}}}1 | |
const schema = makeExecutableSchema({typeDefs, resolvers}) | |
var query = ` | |
{ | |
post(id: 1) { | |
title | |
author { | |
name | |
} | |
comments { | |
content | |
} | |
} | |
posts(limit: 1) { | |
title | |
author { | |
name | |
} | |
} | |
} | |
` | |
var root = null | |
var context = {} | |
fixtures().then(() => { | |
graphql(schema, query, root, context).then(v => pd(JSON.stringify(v, null, 2))) | |
}) | |
// vim: fdm=marker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment