apollo-federation-ruby icon indicating copy to clipboard operation
apollo-federation-ruby copied to clipboard

fix: guard compatibility for GetServiceDefinition requests

Open davydovanton opened this issue 5 years ago • 1 comments

Hey!

Thanks for your work around this library. We're researching GQL gateway solutions right now. And apollo gateway one of the possible solutions for us, that's why this library is really useful for us.

Before start using the gateway, we need to understand how to start using a gateway with our codebase and for this, we need to think about migration to a gateway.

For example, we have a legacy GQL endpoint with User type like this:

# user service
# `http://user_service/graphql` endpoint
class User < BaseObject
  key fields: 'id'

  field :id, ID, null: false
  field :reviews, [Review], null: true

  def reviews
    # HTTP call to another service
  end
end

For start using a gateway, we want to check that everything is good and make this migration without zero downtime. For this we want to see something like this:

# user service
# `http://user_service/graphql` endpoint
class User < BaseObject
  key fields: 'id'

  field :id, ID, null: false

  # if we call it from client we want to use this field
  # if we call it from gateway we don't want to use this field
  if request == CLIENT
    field :reviews, [Review], null: true
  elsif request == GATEWAY
    # nothing
  end

  def reviews
    # HTTP call to another service
  end
end

# review service
# `http://review_service/graphql` endpoint
class User < BaseObject
  key fields: 'id'
  extend_type

  field :id, ID, null: false, external: true
  field :reviews, [Review], null: true

  def reviews
    # DB call
  end
end

To make this happen I found that I can use https://github.com/exAspArk/graphql-guard/ library with schema masking.

class User < BaseObject
  key fields: 'id'

  field :id, ID, null: false
  field :reviews, [Review], null: true do
    mask ->(ctx) { ctx[:request_type] == 'client' }
  end
end

I tried to use it with the current implementation of apollo gateway but found an issue around GetServiceDefinition call:

Screenshot 2019-11-06 at 02 02 29

I started searching where the problem and found that you change FederatedDocumentFromSchemaDefinition but use it with an empty warden object (link to original code).

I added hack from this PR and after that, I got the right behavior:

Screenshot 2019-11-06 at 02 07 54

I'm not sure that my fix is good from a code perspective, but I'll be happy to start using the apollo federation ASAP. That's why I have a question: can we merge it soon or how I can change the code?

davydovanton avatar Nov 05 '19 23:11 davydovanton

Hi @davydovanton. Thanks for creating this PR and sorry for the delay. If I understand the issue you're trying to fix, it looks like https://github.com/Gusto/apollo-federation-ruby/pull/45 may have already fixed it. Does that look right?

rylanc avatar May 27 '20 23:05 rylanc

Closing this as stale. Please feel free to open a new PR/issue as needed.

grxy avatar Mar 14 '23 15:03 grxy