Reputation: 4984
I'm really new to graphQL
I have a simple graphQL schema here
const graphql = require('graphql');
const _ = require('lodash');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID
} = graphql
const books = [
{ name: "book 1", genre: "book-1", id: "1" },
{ name: "book 2", genre: "book-2", id: "2" },
{ name: "book 3", genre: "book-3", id: "3" }
]
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString }
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return _.find(books, { id: args.id })
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery
})
I can return one book using the id
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return _.find(books, { id: args.id })
}
}
}
})
How would I return all the books, I was thinking something like:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: {},
resolve(parent, args) {
return _.find(books, {})
}
}
}
})
Upvotes: 1
Views: 85
Reputation: 4984
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { displayall: { type: GraphQLID } },
resolve(parent, args) {
return _.find(books, { displayall: args.displayall })
}
}
books:{
type: new GraphQLList(BookType),
resolve(parent, args) {
return books
}
}
}
})
Upvotes: 1