Created
February 19, 2020 09:53
-
-
Save josser/49ec9b53f3640cd1221e0d090904fff3 to your computer and use it in GitHub Desktop.
This file contains 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
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