Skip to content

Instantly share code, notes, and snippets.

@josser
Created February 19, 2020 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josser/49ec9b53f3640cd1221e0d090904fff3 to your computer and use it in GitHub Desktop.
Save josser/49ec9b53f3640cd1221e0d090904fff3 to your computer and use it in GitHub Desktop.
import { gql } from 'apollo-server-koa'
import { find, filter } from 'lodash'
const authors = [
{ id: 1, name: 'J.K. Rowling' },
{ id: 2, name: 'Michael Crichton' }
]
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 1,
},
{
title: 'Jurassic Park',
author: 2,
},
];
export const resolvers = {
Query: {
author: (parent, args, context, info) => {
console.log('Query.author')
return find(authors, { id: args.id });
}
},
Author: {
books(author) {
console.log('Author.books')
return filter(books, { author: author.id });
},
}
};
export const typeDefs = gql`
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields for every book in our data source.
type Book {
title: String
author: Author
}
type Author {
id: Int
books: [Book]
}
type Query {
author(id: Int): Author
}
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment