diff --git a/server/models/author.js b/server/models/author.js new file mode 100644 index 0000000..34067b0 --- /dev/null +++ b/server/models/author.js @@ -0,0 +1,9 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const authorSchema = new Schema({ + name: String, + age: Number +}); + +module.exports = mongoose.model('Author', authorSchema); diff --git a/server/models/book.js b/server/models/book.js new file mode 100644 index 0000000..62d5ece --- /dev/null +++ b/server/models/book.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const bookSchema = new Schema({ + name: String, + genre: String, + authorId: String +}); + +module.exports = mongoose.model('Book', bookSchema); diff --git a/server/schema/schema.js b/server/schema/schema.js index 9d4d7d2..1c0e87d 100644 --- a/server/schema/schema.js +++ b/server/schema/schema.js @@ -1,6 +1,3 @@ -const graphql = require('graphql'); -const _ = require('lodash'); - const { GraphQLObjectType, GraphQLString, @@ -8,22 +5,11 @@ const { GraphQLInt, GraphQLList, GraphQLSchema -} = graphql; +} = require('graphql'); +const _ = require('lodash'); -// temp data -const books = [ - { name: 'Name of the Wind', genre: 'Fantasy', id: '1', authorId: '1' }, - { name: 'The Final Empire', genre: 'Fantasy', id: '2', authorId: '2' }, - { name: 'The Hero of Ages', genre: 'Fantasy', id: '4', authorId: '2' }, - { name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorId: '3' }, - { name: 'The Colour of Magic', genre: 'Fantasy', id: '5', authorId: '3' }, - { name: 'The Light Fantastic', genre: 'Fantasy', id: '6', authorId: '3' } -]; -const authors = [ - { name: 'Patrick Rothfuss', age: 44, id: '1' }, - { name: 'Brandon Sanderson', age: 42, id: '2' }, - { name: 'Terry Pratchett', age: 66, id: '3' } -]; +const Book = require('../models/book'); +const Author = require('../models/author'); const BookType = new GraphQLObjectType({ name: 'Book', @@ -34,7 +20,7 @@ const BookType = new GraphQLObjectType({ author: { type: AuthorType, resolve (parent, args) { - return _.find(authors, { id: parent.authorId }); + // return _.find(authors, { id: parent.authorId }); } } }) @@ -49,7 +35,7 @@ const AuthorType = new GraphQLObjectType({ books: { type: new GraphQLList(BookType), resolve (parent, args) { - return _.filter(books, { authorId: parent.id }); + // return _.filter(books, { authorId: parent.id }); } } }) @@ -62,31 +48,68 @@ const RootQuery = new GraphQLObjectType({ type: BookType, args: { id: { type: GraphQLID } }, resolve (parent, args) { - return _.find(books, { id: args.id }); + // return _.find(books, { id: args.id }); } }, author: { type: AuthorType, args: { id: { type: GraphQLID } }, resolve (parent, args) { - return _.find(authors, { id: args.id }); + // return _.find(authors, { id: args.id }); } }, books: { type: new GraphQLList(BookType), resolve (parent, args) { - return books; + // return books; } }, authors: { type: new GraphQLList(AuthorType), resolve (parent, args) { - return authors; + // return authors; + } + } + } +}); + +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 + query: RootQuery, + mutation: Mutation });