0

Suppose if we want to query multiple fields on GraphQL, how to do that ?

{
  book(id:"123",name:"book1") {
    id,
    name,
    authors{
      id
    }
  }
}

Also, how to filter out subfield, like author based on name??

{
  book(id:"123",name:"book1") {
    id,
    name,
    authors(name:"author1"){
      id
    }
  }
}

I have found this article, but just wanted to know is there any better way . https://www.yld.io/blog/query-by-2-or-more-fields-on-graphql/

andyrandy
  • 72,880
  • 8
  • 113
  • 130
Rajat Sharma
  • 121
  • 7

1 Answers1

2

Assuming you are building a GraphQL server and want to know how to filter on sub-field.

Depending upon the server library you are using, you world want to write a "Field Resolver" on the parent "Book" type. Field resolver is called once for each resolved parent so you will have reference to the resolved parent type(Book in your case) as well as arguments to field(name: "author1") and then you can resolve it.

You might also want to use dataloader since one database call to resolve author sub-field per parent field can cause flood of database queries.

Manan Vaghasiya
  • 881
  • 1
  • 10
  • 25