Last active
June 26, 2017 11:57
-
-
Save Arthelon/06f9937cdc86e156f4e81b58e5d01e33 to your computer and use it in GitHub Desktop.
Fix for apollo pad demo
This file contains hidden or 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
// 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 { find, filter } from 'lodash'; | |
import { makeExecutableSchema } from 'graphql-tools'; | |
const typeDefs = ` | |
type Author { | |
id: Int! | |
firstName: String | |
lastName: String | |
posts: [Post] # the list of Posts by this author | |
} | |
type Post { | |
id: Int! | |
title: String | |
author: Author | |
votes: Int | |
} | |
# the schema allows the following query: | |
type Query { | |
posts: [Post] | |
author(id: Int!): Author | |
} | |
# this schema allows the following mutation: | |
type Mutation { | |
upvotePost ( | |
postId: Int! | |
): Post | |
} | |
`; | |
const resolvers = { | |
Query: { | |
posts: () => posts, | |
author: (_, { id }) => find(authors, { id: id }), | |
}, | |
Mutation: { | |
upvotePost: (_, { postId }) => { | |
const post = find(posts, { id: postId }); | |
if (!post) { | |
throw new Error(`Couldn't find post with id ${postId}`); | |
} | |
post.votes += 1; | |
return post; | |
}, | |
}, | |
Author: { | |
posts: (author) => filter(posts, { authorId: author.id }), | |
}, | |
Post: { | |
author: (post) => find(authors, { id: post.authorId }), | |
}, | |
}; | |
export const schema = makeExecutableSchema({ | |
typeDefs, | |
resolvers, | |
}); | |
export function context(headers, secrets) { | |
return { | |
headers, | |
secrets, | |
}; | |
}; | |
export const rootValue = {} | |
const authors = [ | |
{ id: 1, firstName: 'Tom', lastName: 'Coleman' }, | |
{ id: 2, firstName: 'Sashko', lastName: 'Stubailo' }, | |
{ id: 3, firstName: 'Mikhail', lastName: 'Novikov' }, | |
]; | |
const posts = [ | |
{ id: 1, authorId: 1, title: 'Introduction to GraphQL', votes: 2 }, | |
{ id: 2, authorId: 2, title: 'Welcome to Meteor', votes: 3 }, | |
{ id: 3, authorId: 2, title: 'Advanced GraphQL', votes: 1 }, | |
{ id: 4, authorId: 3, title: 'Launchpad is Cool', votes: 7 }, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment