Skip to content

Instantly share code, notes, and snippets.

View JeffML's full-sized avatar
🏠
Barely working

Jeff Lowery JeffML

🏠
Barely working
View GitHub Profile
@JeffML
JeffML / rootSchema.js
Last active August 21, 2017 00:29
Reorg: root schema
import {
makeExecutableSchema
} from 'graphql-tools';
import {
schema as authorpostsSchema,
resolvers as authorpostsResolvers
} from './authorposts';
@JeffML
JeffML / authorpostResolvers.js
Created August 21, 2017 00:40
authorposts schema
import {
authors,
posts
} from './dataSource';
const rootResolvers = {
Query: {
posts: () => posts,
author: (_, {
id
@JeffML
JeffML / author.js
Created August 21, 2017 01:25
Refactoring authorposts schema
export default `
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
}`
@JeffML
JeffML / dataSource.js
Last active August 21, 2017 04:16
MyLittleDomain
const myLittleTypes = [{
id: 1,
description: 'This is good',
}, {
id: 2,
description: 'This is better',
}, {
id: 3,
description: 'This is the best!',
}];
@JeffML
JeffML / schema.js
Created September 3, 2017 21:20
Step 1: adding
// This example demonstrates a simple server with some relational data: Posts and Authors. You can get the posts for a particular author,
// and vice-versa Read the complete docs for graphql-tools here: http://dev.apollodata.com/tools/graphql-tools/generate-schema.html
import {
makeExecutableSchema,
addMockFunctionsToSchema
} from 'graphql-tools';
import {
schema as authorpostsSchema,
@JeffML
JeffML / handrolled.js
Created October 29, 2017 21:55
handrolled rowset to JSON conversion
const handrolled = (rs) => {
const authors = {}
rs.forEach(r => {
const authname = [r.last_name, r.first_name].join(',');
if (!authors[authname]) {
authors[authname] = {
categories: {}
}
}
@JeffML
JeffML / treeize.js
Created October 30, 2017 00:19
Using treeize to generate JSON heirarchies
import Treeize from 'treeize'
const treeized = rs => {
var authors = new Treeize();
const seed = rs.map(r => ({
name: [r.last_name, r.first_name].join(', '),
'categories:type': r.category,
'categories:titles:name': r.title
}))
@JeffML
JeffML / addEntities.js
Created October 30, 2017 05:10
Adding casual-generated data to sql database
const addEntities = (dataset) => {
dataset.forEach(d => {
d.title = d.title.replace(/'/, "''")
const stmt =
`
IF NOT EXISTS (
select * from author
where first_name = '${d.first_name}'
and last_name = '${d.last_name}')
@JeffML
JeffML / reducedTreeize.js
Created October 30, 2017 05:33
Reduces treeize data
if (useMethod === 'treeizedReduced') {
var rs = treeized(resultSet)
const rsr = rs.reduce((o, r) => {
const rc = r.categories.reduce((oo, c) => {
oo[c.type] = {
titles: c.titles.map(t => t.name)
}
return oo;
}, {})
@JeffML
JeffML / resolver.js
Created November 26, 2017 17:12
Resolver code for PostOps
const voteHandler = (postId, updown) => {
return new Promise((resolve, reject) => {
const post = posts.find(p => p.id === postId);
if (!post) {
reject(`Couldn't find post with id ${postId}`);
}
post.votes += updown;
resolve(post);
})
};