mirror of
https://github.com/seigler/graphql-playground
synced 2025-07-27 09:36:09 +00:00
feat(server): author -> books
This commit is contained in:
parent
c3b89e918f
commit
4dd04ff725
1 changed files with 16 additions and 6 deletions
|
@ -6,6 +6,7 @@ const {
|
||||||
GraphQLString,
|
GraphQLString,
|
||||||
GraphQLID,
|
GraphQLID,
|
||||||
GraphQLInt,
|
GraphQLInt,
|
||||||
|
GraphQLList,
|
||||||
GraphQLSchema
|
GraphQLSchema
|
||||||
} = graphql;
|
} = graphql;
|
||||||
|
|
||||||
|
@ -13,7 +14,10 @@ const {
|
||||||
const books = [
|
const books = [
|
||||||
{ name: 'Name of the Wind', genre: 'Fantasy', id: '1', authorId: '1' },
|
{ name: 'Name of the Wind', genre: 'Fantasy', id: '1', authorId: '1' },
|
||||||
{ name: 'The Final Empire', genre: 'Fantasy', id: '2', authorId: '2' },
|
{ name: 'The Final Empire', genre: 'Fantasy', id: '2', authorId: '2' },
|
||||||
{ name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorId: '3' }
|
{ 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 = [
|
const authors = [
|
||||||
{ name: 'Patrick Rothfuss', age: 44, id: '1' },
|
{ name: 'Patrick Rothfuss', age: 44, id: '1' },
|
||||||
|
@ -41,7 +45,13 @@ const AuthorType = new GraphQLObjectType({
|
||||||
fields: () => ({
|
fields: () => ({
|
||||||
id: { type: GraphQLID },
|
id: { type: GraphQLID },
|
||||||
name: { type: GraphQLString },
|
name: { type: GraphQLString },
|
||||||
age: { type: GraphQLInt }
|
age: { type: GraphQLInt },
|
||||||
|
books: {
|
||||||
|
type: new GraphQLList(BookType),
|
||||||
|
resolve (parent, args) {
|
||||||
|
return _.filter(books, { authorId: parent.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -51,15 +61,15 @@ const RootQuery = new GraphQLObjectType({
|
||||||
book: {
|
book: {
|
||||||
type: BookType,
|
type: BookType,
|
||||||
args: { id: { type: GraphQLID } },
|
args: { id: { type: GraphQLID } },
|
||||||
resolve (parent, { id }) {
|
resolve (parent, args) {
|
||||||
return _.find(books, { id });
|
return _.find(books, { id: args.id });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
author: {
|
author: {
|
||||||
type: AuthorType,
|
type: AuthorType,
|
||||||
args: { id: { type: GraphQLID } },
|
args: { id: { type: GraphQLID } },
|
||||||
resolve (parent, { id }) {
|
resolve (parent, args) {
|
||||||
return _.find(authors, { id });
|
return _.find(authors, { id: args.id });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue