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
import React from "react";
import { Query } from 'react-apollo';
import gql from "graphql-tag";
const query = gql`{
currencyOfInterest @client
}`
const ExchangeSelector = ({data: {rates}}) => (
<Query query={query}>
@JeffML
JeffML / query_async_schema.js
Created April 8, 2018 17:15
A simple schema and resolver for demonstrating parallel vs sequential execution
import {makeExecutableSchema} from 'graphql-tools';
const typeDefs = `
type Query {
message(id: ID!): String!
}
type Mutation {
message(id: ID!): String!
}
@JeffML
JeffML / UCI_state
Created April 8, 2018 04:51
UCI state example
GUI engine
// tell the engine to switch to UCI mode
uci
// engine identify
id name Shredder
id author Stefan MK
// engine sends the options it can change
@JeffML
JeffML / authorOps.js
Created November 26, 2017 17:24
Resolver code for AuthorOps
const addBook = (book, authorId) => {
console.log("addBook", book, authorId)
return new Promise((resolve, reject) => {
book.authorId = authorId
books.push(book)
resolve(books.length)
})
}
const removeBook = (book, authorId) => {
@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);
})
};
@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 / 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 / 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 / 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 / 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,