mirror of
https://github.com/seigler/graphql-playground
synced 2025-07-27 01:26: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
|
@ -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
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue