mirror of
https://github.com/seigler/graphql-playground
synced 2025-07-27 09:36:09 +00:00
feat(server): add authors
This commit is contained in:
parent
c401dfbe04
commit
995213d554
1 changed files with 25 additions and 2 deletions
|
@ -4,6 +4,8 @@ const _ = require('lodash');
|
|||
const {
|
||||
GraphQLObjectType,
|
||||
GraphQLString,
|
||||
GraphQLID,
|
||||
GraphQLInt,
|
||||
GraphQLSchema
|
||||
} = graphql;
|
||||
|
||||
|
@ -13,25 +15,46 @@ const books = [
|
|||
{ name: 'The Final Empire', genre: 'Fantasy', id: '2' },
|
||||
{ name: 'The Long Earth', genre: 'Sci-Fi', id: '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: GraphQLString },
|
||||
id: { type: GraphQLID },
|
||||
name: { type: GraphQLString },
|
||||
genre: { type: GraphQLString }
|
||||
})
|
||||
});
|
||||
|
||||
const AuthorType = new GraphQLObjectType({
|
||||
name: 'Author',
|
||||
fields: () => ({
|
||||
id: { type: GraphQLID },
|
||||
name: { type: GraphQLString },
|
||||
age: { type: GraphQLInt }
|
||||
})
|
||||
});
|
||||
|
||||
const RootQuery = new GraphQLObjectType({
|
||||
name: 'RootQueryType',
|
||||
fields: {
|
||||
book: {
|
||||
type: BookType,
|
||||
args: { id: { type: GraphQLString } },
|
||||
args: { id: { type: GraphQLID } },
|
||||
resolve (parent, { id }) {
|
||||
return _.find(books, { id });
|
||||
}
|
||||
},
|
||||
author: {
|
||||
type: AuthorType,
|
||||
args: { id: { type: GraphQLID } },
|
||||
resolve (parent, { id }) {
|
||||
return _.find(authors, { id });
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue