grape icon indicating copy to clipboard operation
grape copied to clipboard

A mechanism to configure global before hooks

Open dhruvCW opened this issue 10 months ago • 3 comments

I am looking to implement query tags for activerecord when it's used inside a grape API class. One of the things I am looking into is a way to populate a context for the query tags callback inside the grape before callback.

It would be awesome if we could inject such callbacks into all Grape::API classes configuring them once. Sort of what Rails allows with their railties system.

dhruvCW avatar Jan 15 '25 16:01 dhruvCW

Does subclassing Grape::API into MyGrape::API < Grape::API, using that everywhere and adding the hooks not work?

dblock avatar Jan 15 '25 21:01 dblock

Does subclassing Grape::API into MyGrape::API < Grape::API, using that everywhere and adding the hooks not work?

it does 👍 This is honestly a half baked idea but I was thinking we could have something "configured" globally. The use case is to be able to inject information into a global singleton like ActiveSupport::ExecutionContext that is inturn used by active record query log tags

dhruvCW avatar Jan 16 '25 06:01 dhruvCW

I am not against it. Generally globals are useful when authoring libraries that can be composed, and I can definitely see the use case that you provided as a valid one - include gem 'grape-active-record-query-log-tags' to get query log tags in all your Grape APIs.

dblock avatar Jan 16 '25 19:01 dblock