How to call a method after all lazy resolves are complete #4217
trevorturk
started this conversation in
General
Replies: 1 comment
-
|
Thanks for sharing what you found! 👍 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I ran into a tricky issue today and I thought I should share the solution I came up with, in case it's helpful to others, or perhaps someone can suggest a better way!
I have a weather app with a
Weathermodel that fetches data from various upstream API endpoints usingAsync::HTTP. I uselazy_resolve Async::Task, :waitto issue the requests in parallel, much like one might do withTyphoeus::Hydra.When those API requests are made, I have an object that keeps count of the cache hit/miss count, which I then save to a database in a background job to avoid excessive writes on the database.
The issue I ran into is that after all lazy resolves are complete, I need to call
Weather#save_cache_counts, but I had trouble finding a workable technique. I couldn't getGraphQL::Schema::FieldExtension#after_resolveto help in my case, but perhaps I was misusing it.What I ended up hitting on seems to work just fine, but it looks like a bit of a hack. Basically, I create a
contextobject and pass that intoSchema.execute, then I stuff myweatherobject in there so I can call methods on it after execution.So, perhaps this will be helpful to others, but I'd also love to hear other ideas worth trying. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions