feat(server): add mutations, store data on mongoDB

This commit is contained in:
Joshua Seigler 2020-01-29 00:48:16 -05:00
parent 62b1a67be7
commit fae058cc68
3 changed files with 67 additions and 25 deletions

9
server/models/author.js Normal file
View file

@ -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);

10
server/models/book.js Normal file
View file

@ -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);

View file

@ -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
});