The simplest way to optimize nested and recursive queries is using caching and batching techniques. An alternative solution to optimize queries is using fields' ASTs, but that may become complicated.
Let's say we have a viewer
field of a type User
that type may contain viewedOffers
field of type List<Offer>
where each offer in the list has an author
field of type User
. This viewer
field might be queried as follows:
{ viewer { displayName, viewedOffers(first: 10) { title, author { displayName } } } }
This query is supposed to return the currently logged in user, as well as the list of recently viewed by the user offers (ads) along with author information for each offer. Obviously, in order to execute this query, our GraphQL server may send many excess SQL requests to the database: SELECT ... FROM User WHERE id = ?
, SELECT ... FROM Offer WHERE id = ?
; one for each requested user and offer objects (regardless of how...