mirror of
https://github.com/seigler/graphql-playground
synced 2025-07-27 01:26:10 +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 {
|
const {
|
||||||
GraphQLObjectType,
|
GraphQLObjectType,
|
||||||
GraphQLString,
|
GraphQLString,
|
||||||
|
GraphQLID,
|
||||||
|
GraphQLInt,
|
||||||
GraphQLSchema
|
GraphQLSchema
|
||||||
} = graphql;
|
} = graphql;
|
||||||
|
|
||||||
|
@ -13,25 +15,46 @@ const books = [
|
||||||
{ name: 'The Final Empire', genre: 'Fantasy', id: '2' },
|
{ name: 'The Final Empire', genre: 'Fantasy', id: '2' },
|
||||||
{ name: 'The Long Earth', genre: 'Sci-Fi', id: '3' }
|
{ 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({
|
const BookType = new GraphQLObjectType({
|
||||||
name: 'Book',
|
name: 'Book',
|
||||||
fields: () => ({
|
fields: () => ({
|
||||||
id: { type: GraphQLString },
|
id: { type: GraphQLID },
|
||||||
name: { type: GraphQLString },
|
name: { type: GraphQLString },
|
||||||
genre: { 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({
|
const RootQuery = new GraphQLObjectType({
|
||||||
name: 'RootQueryType',
|
name: 'RootQueryType',
|
||||||
fields: {
|
fields: {
|
||||||
book: {
|
book: {
|
||||||
type: BookType,
|
type: BookType,
|
||||||
args: { id: { type: GraphQLString } },
|
args: { id: { type: GraphQLID } },
|
||||||
resolve (parent, { id }) {
|
resolve (parent, { id }) {
|
||||||
return _.find(books, { 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