graphql-ruby: Subscriptions incompatible with multiple Postgres schemas?

I’m building a multi-tenant Rails app with apartment and I’m using the one-schema-per-tenant-solution.

Everything works really great except subscriptions. While Actioncable work’s with this approach (which I got from here) …

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user, :tenant

    def connect
      self.tenant = request.subdomain
      Apartment::Tenant.switch(tenant)!
      self.current_user = find_verified_user
    end

    private

    def find_verified_user
      env['warden'].session_serializer.fetch('user') ||
        reject_unauthorized_connection
    end
  end
end

… this one doesn’t: MyApp.subscriptions.trigger('somethingChanged', {}, self) (I’m running this in an after_commit hook), because this function queries the record in the public schema.

self, which is a model that is actually created, exists in my tenant database, but not in the public schema … and that’s why my hook always ends up in a Record not found error.

Is there any way to tell graphql-ruby about the right schema?

Thanks in advance! 😃

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (15 by maintainers)

Most upvoted comments

👋 I just opened a PR to improve support for this a bit (no more monkey patch required) and add a doc. If anyone has a minute to review it, I’d appreciate it! #3574

@dittonjs and everyone who has the same problem: it may not be the nicest looking solution, but this one finally works:

MyAppSschema =
  GraphQL::Schema.define do
    module MultiTenantSerializer
      @@tenant = Apartment::Tenant.current

      def self.load(value)
        Apartment::Tenant.switch(@@tenant) { GraphQL::Subscriptions::Serialize.load(value) }
      end

      def self.dump(obj)
        GraphQL::Subscriptions::Serialize.dump(obj)
      end
    end

    use GraphQL::Subscriptions::ActionCableSubscriptions, serializer: MultiTenantSerializer

    ...
  end