Is resolver dependency injection preferred to constructor dependency injection? #1174
-
Hello maintainers of graphql-laravel, At work, we have a monolithic GraphQL API and several queries and mutations that are injecting dependencies via the constructor, like this:
After some testing with the debugger, my understanding is that the framework calls the constructors of every query and mutation on every request regardless of what query or mutation is in the request. Is this correct? The documentation only mentions dependency injection via the resolve method:
Given that the constructors of all queries and mutations are called on every request, I'm thinking we should avoid doing dependency injection here for performance reasons and to avoid a single point of failure (that is, if a runtime exception occurs in one of the dependencies being constructed, the entire GraphQL service becomes unusable). At least with resolver dependency injection, the dependencies are created "on demand" only when the actual query or mutation is in the request. Let me know if my understanding is correct, thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes, you got it absolutely right: dependency injection via resolve is the way to go! |
Beta Was this translation helpful? Give feedback.
Yes, you got it absolutely right: dependency injection via resolve is the way to go!