What's the best practice to achieve dependency injection with ruby-graphql?

321 Views Asked by At

I want to use dependency injection with graphql-ruby.

I.e.

module CustomerCredits
  module Types
    class QueryType < GraphQL::Schema::Object

      description 'The query root of this schema'

      field :filter_users, [Types::UserType], null: false do
        argument :parameters, InputTypes::UserFilterParameters, required: true
      end

      # resolvers

      def filter_users(arguments) 
        repo = UserRepository.new(complex_arguments) # I want to inject the dependency UserRepository
    
        repo.filtered_users(**arguments[:parameters])
      end

    end
  end
end

Using dependency injection in initialize is not possible, because QueryType is instantiated by graphql-ruby.

2

There are 2 best solutions below

2
On BEST ANSWER

As you've mentioned, injection through the initializer might not be super straight forward, so if you want to go fully into dependency injection and inversion of control, you could leverage an IOC Container library like Dry Auto Inject. I know it might be a full blown solution, and it could possibly be too heavy handed for your use case (not sure), but since you're already using repositories in Ruby, it might not be.

0
On

Following the schema definition from graphql-ruby, one solution I thought of for this problem was to inject your database reference into a controller class, then when your controller is hit, you pass the database reference as part of the context.

# app/controllers/graphql_controller.rb
result = MySchema.execute(
  params[:query],
  variables: params[:variables],
  context: { 
    current_user: current_user, 
    db: my_database_ref # inject database ref here
    },

)
render json: result

Then in your Query Type definiton you can pull the db from the context.

class QueryType < GraphQL::Schema::Object
  description "The query root of this schema"

  field :post, PostType, "Find a post by ID" do
    argument :id, ID
  end

  def post(id:)
    db = context[:db]  # pull db reference from context here
    db[:posts].where(id:).first
  end
end