const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLInt, GraphQLList, GraphQLSchema } = require('graphql'); const _ = require('lodash'); const Book = require('../models/book'); const Author = require('../models/author'); const BookType = new GraphQLObjectType({ name: 'Book', fields: () => ({ id: { type: GraphQLID }, name: { type: GraphQLString }, genre: { type: GraphQLString }, author: { type: AuthorType, resolve (parent, args) { return Author.findById(parent.authorId); } } }) }); const AuthorType = new GraphQLObjectType({ name: 'Author', fields: () => ({ id: { type: GraphQLID }, name: { type: GraphQLString }, age: { type: GraphQLInt }, books: { type: new GraphQLList(BookType), resolve (parent, args) { return Book.find({ authorId: parent.id }); } } }) }); const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: { book: { type: BookType, args: { id: { type: GraphQLID } }, resolve (parent, args) { return Book.findById(args.id); } }, author: { type: AuthorType, args: { id: { type: GraphQLID } }, resolve (parent, args) { return Author.findById(args.id); } }, books: { type: new GraphQLList(BookType), resolve (parent, args) { return Book.find({}); } }, authors: { type: new GraphQLList(AuthorType), resolve (parent, args) { return Author.find({}); } } } }); const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: { addAuthor: { type: AuthorType, args: { name: { type: GraphQLString }, age: { type: GraphQLInt } }, resolve (parent, { name, age }) { const author = new Author({ name, age }); return author.save(); } }, addBook: { type: BookType, args: { name: { type: GraphQLString }, genre: { type: GraphQLString }, authorId: { type: GraphQLID } }, resolve (parent, { name, genre, authorId }) { const book = new Book({ name, genre, authorId }); return book.save(); } } } }); module.exports = new GraphQLSchema({ query: RootQuery, mutation: Mutation });