Skip to content

Instantly share code, notes, and snippets.

@josser

josser/schema.ts

Created Feb 19, 2020
Embed
What would you like to do?
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