feat(gql): Introduce lookahead to eager load relationships (N+1) #3695
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
By design, GraphQL can create a lot of N+1. The
graphql
gem provides a way to inspect the query and eager load accordingly.This is generally not an issue because Lago's vast majority of the load is on the REST API, not the dashboard. But still, it can lead to slow page loading.
Description
In the SubscriptionAlertResolver, a query can trigger 2 extra requests per item, one for thresholds, one for billable metric.
With lookahead, we check if billableMetric or thresholds are request end ensure we call
includes(:billable_metric)
orincludes(:thresholds)
respectively.Another useful technique is to also expose ids alongside the relationships, like in the Alert Object.
How to use
extras
Resolver configurationlookahead:
named argumentselection
andselect?
Links:
Testing
This PR also sets up
bullet
gem for tests, until now it was only a dev dependency.Notice the
with_bullet: true
example metadata.