Introduction
The QueryManager takes center stage when it comes to handling cross-entity filtering scenarios in GraphWeaver.
Its primary objective is to resolve queries that span multiple data sources, enabling smooth data retrieval across disparate entities.
It is very rare that you would need to use the QueryManager directly but this page explains its use.
How does QueryManager Work?
When the QueryManager receives a query filter, it treats it as a tree structure, where each branch of the tree can exist in different data sources.
The QueryManager's task, along with the help of the visit
function, is to traverse the branches of the tree until reaching the leaf nodes.
Then, it traverses back up the tree, checking at each level whether the data source matches the level below.
If the data source differs at any level, it means that the query needs to be executed at that point.
The QueryManager will then collapse the filter into a set of IDs, making it understandable to the parent data source.
To better understand this complex process, let's consider an example of a cross-data source query:
"Find me tasks where the status is ON_HOLD and the user name is 'ABC'."
The corresponding filter for this query would look like this:
{
"status": "ON_HOLD",
"user": {
"name": "ABC"
}
}
Directly querying the database for these tasks is not feasible since the user’s name is not stored in the database.
Here's how the QueryManager resolves this filter:
- It begins by traversing the leaves of the filter tree.
- On the way back up, it checks if the user field exists in the same data source as the tasks.
- Since the user data source differs, the QueryManager resolves the user portion and queries the datasource to obtain the user’s ID based on the name (
user: { name: 'ABC' }
). - With the user ID in hand, the original query is rewritten as
{ "status": "ON_HOLD", "user": { "id": 123 }}
. - Finally, the modified query can be executed directly against the tasks data source, as only the user ID is stored in the table.
list
function of GraphWeaver and hasn't been implemented for other functions.By leveraging the power of QueryManager, GraphWeaver provides a robust and efficient solution for handling complex cross-entity queries and optimising data loading in GraphQL APIs.
The QueryManager plays a vital role in enabling cross-entity filtering in GraphWeaver.