dry-rb_autoinject and GraphQL

141 Views Asked by At

I am trying to make a GraphQL API, but also implement dependency injection through dry-rb_autoinject. I managed to do this via controller and context. Here's my test QueryType.

Types::QueryType = GraphQL::ObjectType.define do
  name "Query"
  # Add root-level fields here.
  # They will be entry points for queries on your schema.

  # TODO: remove me
  field :testField, types.String do
    description "An example field added by the generator"
    resolve ->(obj, args, ctx) {
     ctx[:services][:output_service].()
    }
  end
end

And in the graphql_controller I just do

class GraphqlController < ApplicationController
  include IMPORT[:output_service]
  def execute
   ...
      services: {
        output_service: output_service
      }
    ...

But this solution doesn't seem really good as I import all the services, event those I don't need for current field. Is there any fancier way to to that, maybe not via context?

1

There are 1 best solutions below

0
On BEST ANSWER

The best solution, apparently is to write resolver function for each root field and inject there, since resolver function is a class inhertiting from GraphQL::Function