graphql-client
graphql-client copied to clipboard
Rails ERB templates supporting multiple clients
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?
I got everything to work by not requiring the railtie and setting things up manually:
-
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 -
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 -
Refer to the slightly different constants in your controller/views:
IndexQuery = <<~GRAPHQL query { example { ...OneOfTheApis::Views::Example::List } } GRAPHQL