T
TanStack•17mo ago
sensitive-blue

Question about injection context

When I started, I had the impression that everything inside injectQuery (the main callback for the function, and any other functions like queryFn) is guaranteed to be called in injection context, but I now noticed that this might not be the case. If I understood correctly - the first time the callback is called (on "initialization") it IS in an injection context, but if one of the dependent signal changes and triggers a rerun of the method, this will not work anymore, causing exceptions. that are quite hard to debug, if you used inject somewhere inside it. I wonder if it might be a better implementation to inject(Injector) for when injectQuery is called, and use that injector with runInInjectionContext so the developer could use inject inside these functions. It's very likely that almost any query will have DI dependencies (like HTTTP) and I believe the current implementation might make the DX a bit difficult.
3 Replies
conscious-sapphire
conscious-sapphire•17mo ago
Do you have an example? Could you create an issue for that with repro? Or a PR with a unit test running such code in an injection context. It sounds like you already have a solution in mind 😄
sensitive-blue
sensitive-blueOP•17mo ago
Sure an example would be easy. I need to see how quickly I can create a PR and run tests though Will look at it next week
sensitive-blue
sensitive-blueOP•17mo ago
GitHub
feat(angular-query-experimental): support injection in injectQuery ...
Before, when creating a query with injectQuery, if you injected dependencies (Like inject(HttpClient)) those injections would fail on subsequence changes of dependent signals. That change makes it ...

Did you find this page helpful?