One thing I like about Angular are dependency injections. When you need something — you ask for it and it’s there. Could we borrow that for GraphQL?

Resolvers are most often flat functions that take arguments and return a shape of data. Sound familiar? Think of them as controllers or directives in Angular. They need to return a specific shape that matches what the schema expects. What’s great about pure function resolvers is we can chain them together.

I’ve talked about composing resolvers once before — but here I’m focusing in on one kind of resolver: the injector. This functions takes in an argument and injects some data into our query context. It assigns an object, such as a data model or a return value from an API. One can even use this pattern for authentication or providing service factories. That’s enough Angular for now.

If you’re not already, you should be using Apollo Resolvers by Andrew E. Rhyne. It’s a great library that makes working with apollo-server a breeze. With this library, here is what an injector resolver looks like:

Inject an object into context that our next resolver can use and manipulate

Now you’re wondering why bother having a one-line function that mutates its input. Why would I want stateful functions in my application?

Any kind of refactoring work will benefit from this pattern. These functions insulate your resolvers from dependency changes or API refactors. An injector resolver separates out business logic that might be susceptible to change. Injector resolvers are useful for fetching objects from arguments but they can inject anything into context. These single-concern functions are reusable too. Here’s what a major refactor looks like:

A high impact change isolated to one modular function

Why does this matter? API changes happen often and the growing number of dependencies in our application adds exponential impact. I recently spent five months changing half a hundred modules to add a single argument. This kind of abstraction would save thousands of hours and keep your schema nimble.

Give the injector pattern and try and share your thoughts. The next developer will appreciate such a small change and the impact it will have.

And who knows, that next developer may be you.

More about GraphQL