Skip to content

Instantly share code, notes, and snippets.

@helfer
helfer / apollo-tutorial-mock.js
Created April 22, 2016 20:37
Mocking file for apollo tutorial
import casual from 'casual';
const mocks = {
String: () => 'It works!',
Query: () => ({
author: (root, args) => {
return { firstName: args.firstName, lastName: args.lastName };
},
}),
Author: () => ({ firstName: () => casual.first_name, lastName: () => casual.last_name }),
const resolvers = {
Query: {
author(root, args){
return { id: 1, firstName: 'Hello', lastName: 'World' };
},
},
Author: {
posts(author){
return [
{ id: 1, title: 'A post', text: 'Some text', views: 2},
import Sequelize from 'sequelize';
import casual from 'casual';
import _ from 'lodash';
const db = new Sequelize('blog', null, null, {
dialect: 'sqlite',
storage: './blog.sqlite',
});
const AuthorModel = db.define('author', {
import { Author } from './connectors';
const resolvers = {
Query: {
author(_, args) {
return Author.find({ where: args });
},
},
Author: {
posts(author) {
// at the top with imports:
import Mongoose from 'mongoose';
// somewhere in the middle:
const mongo = Mongoose.connect('mongodb://localhost/views');
const ViewSchema = Mongoose.Schema({
postId: Number,
views: Number,
});
// add this import at the top
import fetch from 'node-fetch';
// add this somewhere in the middle
const FortuneCookie = {
getOne() {
return fetch('http://fortunecookieapi.herokuapp.com/v1/cookie')
.then(res => res.json())
.then(res => {
return res[0].fortune.message;
// Sample GraphQL schema with decorators
+connector(storage: "MySQL")
+doc(description: "A Person")
type Person {
+mock(type: "ID")
id: Int
+mock(raw: "John")
name: String
import { applyDecorators } from 'graphql-tools';
import {
GraphQLSchema,
GraphQLString,
GraphQLObjectType,
} from 'graphql';
import { Doc, Log } from 'graphql-decorators';
const Logger = { log: (...args) => console.log(...args) };
@helfer
helfer / apolloHAPI.js
Last active March 7, 2017 12:44
Using Apollo Server with HAPI
import hapi from 'hapi';
import graphql from 'graphql';
import { ApolloHAPI } from 'apollo-server';
const myGraphQLSchema = new graphql.GraphQLSchema({
// define your schema in GraphQL.js syntax here ...
});
const server = new hapi.Server();
@helfer
helfer / apolloStoredQueries.js
Last active August 4, 2016 22:17
Using stored queries in Apollo Server
import express from 'express';
import { apolloExpress, OperationStore } from 'apollo-server';
import Schema from './schema';
const PORT = 3000;
const store = new OperationStore(Schema);
store.put('query testquery{ testString }');
const app = express();
const options = {