Repository navigation
Allow passing a per relationship field depth in the select option of the Payload find Local API query to optimize performance
#15249
Replies: 1 comment
|
The base issue for this feature was initially discussed here #15244 with a clearer use case of this feature: a "Product Selection" document on an e-commerce website, and a page displaying the list of the product selections:
The "Product Selection" document should contain a relationship field that references multiple products (up to 100). An on the page "Product Selection List," we need the same thing as with posts: display the title of the selection and the author's name. To load the author name, we should use Yes, we can invent workarounds by loading the list with the |
Uh oh!
There was an error while loading. Please reload this page.
For now, we can set the
depthglobally for the Payload app, and override it individually for each API query. But loading all nested documents, referenced by relationship fields, usually negatively affects the performance of the database request, in result, Payoad users face performance issues, because by default the depth is 2.But in most cases, developers don't need all the relationships exposed, especially with the depth=2 by default, but just several of them. Having all of them exposed is just "nice to have" but not with decreasing the performance.
The suggestion to resolve this issue is allow developers to pass the desired depth individually for each relationship field, to spend database resources on loading only the relationship documents that are really needed for this request.
But seems this is not possible with the current version of the Payload Local API, but, surprisingly, possible with the GraphQL API, but using GraphQL locally is not good for performance, and not very convenient, having the Local API available.
Here is an idea of how we can implement this:
In the
findAPI query, we have aselectoption, where we can pass a list of fields with the boolean value, indicating whether it should be included or not.We can extend this value for the relationship fields to be an object too, accepting the information about loading the referenced document and exposing its fields, something like this:
This is an example of a query for a page that displays just the list of posts with authors' names, like this:
The query will join only documents from the collection
authors, wasting no additional resources on attaching other collections for all other relationship fields, making the page fast and lightweight.What do you think about this idea? If there is some way to achieve this using the Payload Local API that already exists, please share a usage example.
All reactions