-
Notifications
You must be signed in to change notification settings - Fork 125
feat(gql): Add BillableMetricId field on Alert object #3692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30db8ab
to
61aa457
Compare
annvelents
approved these changes
May 21, 2025
julienbourdeau
added a commit
that referenced
this pull request
May 28, 2025
## Context By design, GraphQL can create a lot of N+1. The `graphql` gem provides [a way to inspect the query](https://graphql-ruby.org/queries/lookahead.html) 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)` or `includes(:thresholds)` respectively. Another useful technique is to also [expose ids alongside the relationships](#3692), like in the Alert Object. ### How to use * Add `extras` Resolver configuration * Make sure you receive the `lookahead:` named argument * Explore lookahead object with `selection` and `select?` ```ruby class SomethingResolver < Resolvers::BaseResolver REQUIRED_PERMISSION = "something:view" # Explicitly tell GraphQL that you want to use `lookahead` extras [:lookahead] argument :limit, Integer, required: false argument :page, Integer, required: false type Types::Something::Object.collection_type, null: false def resolve(lookahead:, limit: nil, page: nil) # Automatically receive lookahead scope = Something.all # Notice the :collection outer object if lookahead.selection(:collection).selects?(:organization) scope = scope.includes(:organization) end if lookahead.selection(:collection).selection(:organization).selects?(:billing_entity) scope = scope.includes(organization: :billing_entity) end scope end end ``` Links: * https://graphql-ruby.org/queries/lookahead.html * https://gist.github.com/DmitryTsepelev/d0d4f52b1d0a0f6acf3c5894b11a52ca ### Testing This PR also sets up [`bullet` gem for tests](https://github.com/flyerhzm/bullet?tab=readme-ov-file#run-in-tests), until now it was only a dev dependency. Notice the `with_bullet: true` example metadata. ```ruby context "when doing something" do let(:model) { ... } it "does something", with_bullet: true do #... # Must be start manually for maximum flexibility Bullet.start_request subject # Service.call, gql query, api requests, function call... expect(Bullet.notification?).to eq false # Ensure no N+1 ! end end ```
diegocharles
pushed a commit
that referenced
this pull request
Jun 2, 2025
## Context By design, GraphQL can create a lot of N+1. The `graphql` gem provides [a way to inspect the query](https://graphql-ruby.org/queries/lookahead.html) 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)` or `includes(:thresholds)` respectively. Another useful technique is to also [expose ids alongside the relationships](#3692), like in the Alert Object. ### How to use * Add `extras` Resolver configuration * Make sure you receive the `lookahead:` named argument * Explore lookahead object with `selection` and `select?` ```ruby class SomethingResolver < Resolvers::BaseResolver REQUIRED_PERMISSION = "something:view" # Explicitly tell GraphQL that you want to use `lookahead` extras [:lookahead] argument :limit, Integer, required: false argument :page, Integer, required: false type Types::Something::Object.collection_type, null: false def resolve(lookahead:, limit: nil, page: nil) # Automatically receive lookahead scope = Something.all # Notice the :collection outer object if lookahead.selection(:collection).selects?(:organization) scope = scope.includes(:organization) end if lookahead.selection(:collection).selection(:organization).selects?(:billing_entity) scope = scope.includes(organization: :billing_entity) end scope end end ``` Links: * https://graphql-ruby.org/queries/lookahead.html * https://gist.github.com/DmitryTsepelev/d0d4f52b1d0a0f6acf3c5894b11a52ca ### Testing This PR also sets up [`bullet` gem for tests](https://github.com/flyerhzm/bullet?tab=readme-ov-file#run-in-tests), until now it was only a dev dependency. Notice the `with_bullet: true` example metadata. ```ruby context "when doing something" do let(:model) { ... } it "does something", with_bullet: true do #... # Must be start manually for maximum flexibility Bullet.start_request subject # Service.call, gql query, api requests, function call... expect(Bullet.notification?).to eq false # Ensure no N+1 ! end end ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
In the frontend, we want to filter the list of alert_type and billable_metric_id to remove those that already exists.
Description
In GQL, when an object as a realtionship, we typically add this as a full object. I'm also adding the
billabled_metric_id
on Alert object which as 2 benefits:In order to check if an alert exists, we can loop over the array like this