Skip to content

Instantly share code, notes, and snippets.

@JeffML
Created August 21, 2017 00:40
Show Gist options
  • Save JeffML/113636aa7d667c9d72424defc60f6e82 to your computer and use it in GitHub Desktop.
Save JeffML/113636aa7d667c9d72424defc60f6e82 to your computer and use it in GitHub Desktop.
authorposts schema
import {
authors,
posts
} from './dataSource';
const rootResolvers = {
Query: {
posts: () => posts,
author: (_, {
id
}) => authors.find(a => a.id === id)
},
Mutation: {
upvotePost: (_, {
postId
}) => {
const post = posts.find(p => p.id === postId);
if (!post) {
throw new Error(`Couldn't find post with id ${postId}`);
}
post.votes += 1;
return post;
}
},
Author: {
posts: (author) => posts.filter(p => p.authorId === author.id)
},
Post: {
author: (post) => authors.find(a => a.id === post.authorId)
}
};
export default rootResolvers;
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:
extend type Query {
posts: [Post]
author(id: Int!): Author
}
# this schema allows the following mutation:
extend type Mutation {
upvotePost (
postId: Int!
): Post
}
`
];
export default typeDefs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment