mirror of
https://github.com/seigler/graphql-playground
synced 2025-07-27 09:36:09 +00:00
92 lines
2.2 KiB
JavaScript
92 lines
2.2 KiB
JavaScript
const graphql = require('graphql');
|
|
const _ = require('lodash');
|
|
|
|
const {
|
|
GraphQLObjectType,
|
|
GraphQLString,
|
|
GraphQLID,
|
|
GraphQLInt,
|
|
GraphQLList,
|
|
GraphQLSchema
|
|
} = graphql;
|
|
|
|
// 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 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;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
module.exports = new GraphQLSchema({
|
|
query: RootQuery
|
|
});
|