-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Integrate with ActiveRecord::QueryLogs (old marginalia) #4946
Comments
Hey, thanks for the suggestion! I think that's a great idea. Here's how you could add that in your own app in the meantime: # In your Application setup:
config.active_record.query_log_tags = [
# Rails default keys:
:application, :controller, :action, :job,
# Tell QueryLogs about this new key:
{ graphql_query: -> { Current.graphql_query_names } }
]
# Assuming `class Current < ActiveSupport::CurrentAttributes` as in https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
# Substitute your own global state object as-needed
module ActiveRecordQueryLogsTrace
def execute_multiplex(multiplex:)
Current.graphql_query_names = multiplex.queries.map { |q| q.selected_operation&.name || "anonymous" }
end
end I think that would tag ActiveRecord queries with the currently running operation(s). Is that what you had in mind? A deeper integration is also possible, for example, tagging SQL with the currently-running Dataloader::Source or GraphQL field. |
This looks good, I'm trying to adapt it to my code because I'm using an old version of the gem, v2.0.14 (sorry, forgot to mention it!). I'm guessing the Thanks! |
Here's an approach that would work in 2.0.x: # Assuming `class Current < ActiveSupport::CurrentAttributes` as in https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
# Substitute your own global state object as-needed
module ActiveRecordQueryLogsInstrumenter
def self.begin_multiplex(multiplex)
Current.graphql_query_names = multiplex.queries.map { |q| q.selected_operation&.name || "anonymous" }
end
def self.end_multiplex(_multiplex); end
end
# ...
class MySchema < GraphQL::Schema
instrument(:multiplex, ActiveRecordQueryLogsInstrumenter
end (The |
Awesome, it works! Thanks! I just had to change the method names, they should be I'll leave the issue open in case you want to track it to automatically add intergration with QueryLogs, but feel free to close it if needed! |
Hello @rmosolgo, it seems that in your example (#4946 (comment)), Like this: # In your Application setup:
config.active_record.query_log_tags = [
# Rails default keys:
:application, :controller, :action, :job,
# Tell QueryLogs about this new key:
{ graphql_query: -> { Current.graphql_query_names } }
]
# Assuming `class Current < ActiveSupport::CurrentAttributes` as in https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
# Substitute your own global state object as-needed
module ActiveRecordQueryLogsTrace
def execute_multiplex(multiplex:)
Current.graphql_query_names = multiplex.queries.map { |q| q.selected_operation&.name || "anonymous" }
super
end
end |
v2.3.11 will include a new module, |
Awesome! this is gonna be very helpful for debugging, thank you ❤ Glad the suggestion was useful! |
Is your feature request related to a problem? Please describe.
ActiveRecord::QueryLogs
(the oldmarginalia
gem, now bundled in Rails 7) allow us to add comments on SQL queries to detect where is the query triggered from (what controller and action). But when using GraphQL, the comment becomes useless because it's always the same controller and action, so we become blind.Describe the solution you'd like
ActiveRecord::QueryLogs allow for customization so we could add the current GraphQL query being resolved.
Describe alternatives you've considered
None.
Additional context
None.
The text was updated successfully, but these errors were encountered: