graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Rails ERB templates supporting multiple clients

Open Mange opened this issue 6 years ago • 1 comments

I have an Rails app that is using multiple GraphQL APIs.

From what I can tell by reading the code, only a single client can ever be assigned to views for use with the <%graphql blocks, via the Rails.application.config.graphql.client setting.

Is there some way of setting this per controller, maybe?

Mange avatar Feb 13 '19 11:02 Mange

I got everything to work by not requiring the railtie and setting things up manually:

  1. Include the ERB implementation, as the Railtie would have done.

    # config/application.rb
    initializer "graphql.configure_erb_implementation" do |_app|
       require "graphql/client/erb"
       ActionView::Template::Handlers::ERB.erb_implementation = GraphQL::Client::ERB
     end
    
  2. Set up a custom view module per API

    require "graphql/client"
    require "graphql/client/view_module"
    
    module OneOfTheApis
      module Views
        extend GraphQL::Client::ViewModule
        self.path = Rails.application.config.paths["app/views"].first
        self.client = OneOfTheApis::Client
      end
    end
    
  3. Refer to the slightly different constants in your controller/views:

    IndexQuery = <<~GRAPHQL
      query {
        example { ...OneOfTheApis::Views::Example::List }
      }
    GRAPHQL
    

Mange avatar Feb 13 '19 13:02 Mange