Can GraphQl be used to request full objects, as opposed to fields?

We have a scenario where we have multiple nested objects, for example:

  • A tree may have branches
  • A branch may have fruits
  • A fruit may have

Our Api has many consumers with different needs:

  • Some only want to know about the tree itself.
  • Some want to know about the tree, and the branches within the tree.
  • Some want to know about certain trees, and all the branches, fruits and seeds for those trees.

Ideally we’d therefore have some GraphQl-like endpoint which allows you to specify the objects you want and filter on these:

  • { trees } //only get trees
  • { trees: branches } // get trees and their branches

But from what I’ve seen GraphQl is about specifying the fields you want rather than objects. Thus for a tree query you’d need to specify all the tree and branch properties you wish to receive etc.

This makes it more awkward on the backend, because we then need a mapping layer for every single field which can be requested, and map this into a corresponding database field to build a query.

Are there use cases of GraphQL which simply request the high level objects in the queries, and the API returns every property on those objects?

Read more here: Source link