mirror of
https://github.com/seigler/graphql-playground
synced 2025-07-26 17:16:10 +00:00
115 lines
2.4 KiB
JavaScript
115 lines
2.4 KiB
JavaScript
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 _.find(authors, { id: 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 _.filter(books, { authorId: parent.id });
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
const RootQuery = new GraphQLObjectType({
|
|
name: 'RootQueryType',
|
|
fields: {
|
|
book: {
|
|
type: BookType,
|
|
args: { id: { type: GraphQLID } },
|
|
resolve (parent, args) {
|
|
// return _.find(books, { id: args.id });
|
|
}
|
|
},
|
|
author: {
|
|
type: AuthorType,
|
|
args: { id: { type: GraphQLID } },
|
|
resolve (parent, args) {
|
|
// return _.find(authors, { id: args.id });
|
|
}
|
|
},
|
|
books: {
|
|
type: new GraphQLList(BookType),
|
|
resolve (parent, args) {
|
|
// return books;
|
|
}
|
|
},
|
|
authors: {
|
|
type: new GraphQLList(AuthorType),
|
|
resolve (parent, args) {
|
|
// 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,
|
|
mutation: Mutation
|
|
});
|