mirror of
https://github.com/seigler/graphql-playground
synced 2025-07-26 17:16:10 +00:00
feat(server): add mutations, store data on mongoDB
This commit is contained in:
parent
62b1a67be7
commit
fae058cc68
3 changed files with 67 additions and 25 deletions
9
server/models/author.js
Normal file
9
server/models/author.js
Normal 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
10
server/models/book.js
Normal 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);
|
|
@ -1,6 +1,3 @@
|
||||||
const graphql = require('graphql');
|
|
||||||
const _ = require('lodash');
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
GraphQLObjectType,
|
GraphQLObjectType,
|
||||||
GraphQLString,
|
GraphQLString,
|
||||||
|
@ -8,22 +5,11 @@ const {
|
||||||
GraphQLInt,
|
GraphQLInt,
|
||||||
GraphQLList,
|
GraphQLList,
|
||||||
GraphQLSchema
|
GraphQLSchema
|
||||||
} = graphql;
|
} = require('graphql');
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
// temp data
|
const Book = require('../models/book');
|
||||||
const books = [
|
const Author = require('../models/author');
|
||||||
{ 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 BookType = new GraphQLObjectType({
|
const BookType = new GraphQLObjectType({
|
||||||
name: 'Book',
|
name: 'Book',
|
||||||
|
@ -34,7 +20,7 @@ const BookType = new GraphQLObjectType({
|
||||||
author: {
|
author: {
|
||||||
type: AuthorType,
|
type: AuthorType,
|
||||||
resolve (parent, args) {
|
resolve (parent, args) {
|
||||||
return _.find(authors, { id: parent.authorId });
|
// return _.find(authors, { id: parent.authorId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -49,7 +35,7 @@ const AuthorType = new GraphQLObjectType({
|
||||||
books: {
|
books: {
|
||||||
type: new GraphQLList(BookType),
|
type: new GraphQLList(BookType),
|
||||||
resolve (parent, args) {
|
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,
|
type: BookType,
|
||||||
args: { id: { type: GraphQLID } },
|
args: { id: { type: GraphQLID } },
|
||||||
resolve (parent, args) {
|
resolve (parent, args) {
|
||||||
return _.find(books, { id: args.id });
|
// return _.find(books, { id: args.id });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
author: {
|
author: {
|
||||||
type: AuthorType,
|
type: AuthorType,
|
||||||
args: { id: { type: GraphQLID } },
|
args: { id: { type: GraphQLID } },
|
||||||
resolve (parent, args) {
|
resolve (parent, args) {
|
||||||
return _.find(authors, { id: args.id });
|
// return _.find(authors, { id: args.id });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
books: {
|
books: {
|
||||||
type: new GraphQLList(BookType),
|
type: new GraphQLList(BookType),
|
||||||
resolve (parent, args) {
|
resolve (parent, args) {
|
||||||
return books;
|
// return books;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
authors: {
|
authors: {
|
||||||
type: new GraphQLList(AuthorType),
|
type: new GraphQLList(AuthorType),
|
||||||
resolve (parent, args) {
|
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({
|
module.exports = new GraphQLSchema({
|
||||||
query: RootQuery
|
query: RootQuery,
|
||||||
|
mutation: Mutation
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue