lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.7k stars 971 forks source link

Setting ActiveRecord DatabaseResolver to enable multi database does not work with Faraday adapter #1529

Closed madamkiwi closed 9 months ago

madamkiwi commented 9 months ago

Basic Info

Issue description

Please provide a description of the issue you're experiencing. I'm having trouble getting Faraday adapter to work when enabling the ActiveRecord automatic connection switching feature. See ActiveRecord::Middleware::DatabaseSelector

If we cannot overwrite the active record resolver, is there a way to configure Faraday http adapter to redirect to multiple databases?

Please also provide the exception message/stacktrace or any other useful detail.

     Failure/Error:
       expect { graphql_client.execute(query) }.to raise_error(
         Graphlient::Errors::GraphQLError, /query: unauthorized/
       )

       expected Graphlient::Errors::GraphQLError with message matching /query: unauthorized/, got #<NoMethodError: undefined method `[]=' for nil:NilClass

                 options[:id] = id
                        ^^^^^^^> with backtrace:
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:265:in `context'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:260:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-2.2.4/lib/rack/etag.rb:27:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-2.2.4/lib/rack/conditional_get.rb:40:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-2.2.4/lib/rack/head.rb:12:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/railties-6.1.6.1/lib/rails/rack/logger.rb:37:in `call_app'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/railties-6.1.6.1/lib/rails/rack/logger.rb:26:in `block in call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/railties-6.1.6.1/lib/rails/rack/logger.rb:26:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/shopify_app-21.6.0/lib/shopify_app/middleware/jwt_middleware.rb:24:in `call_next'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/shopify_app-21.6.0/lib/shopify_app/middleware/jwt_middleware.rb:12:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-2.2.4/lib/rack/runtime.rb:22:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-2.2.4/lib/rack/sendfile.rb:110:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/sentry-raven-3.1.2/lib/raven/integrations/rack.rb:51:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/railties-6.1.6.1/lib/rails/engine.rb:539:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-test-2.0.2/lib/rack/test.rb:358:in `process_request'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/rack-test-2.0.2/lib/rack/test.rb:155:in `request'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/faraday-rack-2.0.0/lib/faraday/adapter/rack.rb:64:in `execute_request'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/faraday-rack-2.0.0/lib/faraday/adapter/rack.rb:48:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/restforce-6.1.0/lib/restforce/middleware/json_response.rb:43:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/restforce-6.1.0/lib/restforce/middleware/json_request.rb:49:in `call'
         # /Users/lily/.rvm/gems/ruby-3.1.3/gems/faraday-2.6.0/lib/faraday/middleware.rb:17:in `call'

Steps to reproduce

If possible, please provide the steps to reproduce the issue. Add the following in application.rb to let Rails automatically switch connection to multiple databases:

  config.active_record.database_selector = { delay: 2.seconds }
  config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
  config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session

Run graphql client

Graphlient::Client.new('http://localhost:3000/graphql') do |client|
      client.http do |h|
        h.connection do |c|
          c.adapter Faraday::Adapter::Rack, app
        end
      end
end
iMacTia commented 9 months ago

@madamkiwi I'm sorry I'm a bit confused by the request. I'd like to help but I'll need some clarification.

The stacktrace I see seems related to a GraphQL query executed using graphlient (which internally uses Faraday), but the issue title and description mention the ActiveRecord DatabaseResolver. I'm not really sure of how the two are connected?

Focusing on the GraphQL issue, I understand this error is popping up while running tests, and the last code block in your issue description shows how you setup the Graphlient::Client. Since you're using the Faraday::Adapter::Rack adapter, you need to provide a suitable rack app to process your test calls. Effectively, this acts as a mock service for your tests. Considering your error is being raised by the rack gem, I suspect your rack app has not been configured correctly.

My guess is that you're trying to follow this guide to test your GraphQL client, so I'd suggest one of the following:

  1. Check your rack app (the one you pass as app in c.adapter Faraday::Adapter::Rack, app) and make sure there are no issues with it
  2. Try a different testing method: my personal preference is WebMock, but I see graphlient supports more.
  3. Open an issue in the graphlient project: since this error does not originate from Faraday, it's hard for me to help further, so I'd suggest reaching out to the graphlient team instead asking for help.

I don't think I will be able to help further with this issue, so I'm going to close it for now in the hope that the pointers above can be helpful. Feel free to update with additional details if you see anything else pointing to Faraday

madamkiwi commented 9 months ago

Thanks for helping out, and you're right that the actual issue is not on Faraday, this is the real issue: https://github.com/rmosolgo/graphql-ruby/issues/2929

iMacTia commented 9 months ago

Glad I could help! And thank you for sharing the link to the real issue, I'm sure that will help people in future.