Skip to content

Commit

Permalink
fix: visibility checks should handle duplicate types and fields (#245)
Browse files Browse the repository at this point in the history
* fix: visibility checks should handle duplicate types and fields

* Additionally visibility test
  • Loading branch information
jturkel authored May 23, 2023
1 parent 681a22b commit 6401feb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def build_field_node(field_type)
def build_type_definition_nodes(types)
non_federation_types = types.select do |type|
if query_type?(type)
!type.fields.values.all? { |field| FEDERATION_QUERY_FIELDS.include?(field.graphql_name) }
!warden.fields(type).all? { |field| FEDERATION_QUERY_FIELDS.include?(field.graphql_name) }
else
!FEDERATION_TYPES.include?(type.graphql_name)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/apollo-federation/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def schema_entities

# Walk through all of the types and determine which ones are entities (any type with a
# "key" directive)
types_schema.types.values.select do |type|
types_schema.send(:non_introspection_types).values.flatten.select do |type|
# TODO: Interfaces can have a key...
!type.introspection? && type.include?(ApolloFederation::Object) &&
type.include?(ApolloFederation::Object) &&
type.federation_directives&.any? { |directive| directive[:name] == 'key' }
end
end
Expand Down
88 changes: 88 additions & 0 deletions spec/apollo-federation/service_field_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,94 @@ def hello
)
end
end

if Gem::Version.new(GraphQL::VERSION) >= Gem::Version.new('1.13.0')
context 'with visibility checks on types and fields with duplicate names' do
let(:schema) do
regular_product = Class.new(base_object) do
graphql_name 'Product'
key fields: :upc

field :upc, String, null: false
field :regular_field, String, null: true

def self.visible?(context)
context[:graph_type] == :regular
end
end

admin_product = Class.new(base_object) do
graphql_name 'Product'
key fields: :upc

field :upc, String, null: false
field :admin_field, String, null: true

def self.visible?(context)
context[:graph_type] == :admin
end
end

query_obj = Class.new(base_object) do
graphql_name 'Query'

field :hello, String, null: false

field :product, regular_product, null: true do
def visible?(context)
context[:graph_type] == :regular
end
end

field :product, admin_product, null: true do
def visible?(context)
context[:graph_type] == :admin
end
end
end

Class.new(base_schema) do
query query_obj
end
end

it 'applies visibility checks during SDL generation to expose schema members' do
results = schema.execute('{ _service { sdl } }', context: { graph_type: :regular })

expect(results.dig('data', '_service', 'sdl')).to match_sdl(
<<~GRAPHQL,
type Product @key(fields: "upc") {
regularField: String
upc: String!
}
type Query {
hello: String!
product: Product
}
GRAPHQL
)
end

it 'applies visibility checks during SDL generation to expose alternate schema members' do
results = schema.execute('{ _service { sdl } }', context: { graph_type: :admin })

expect(results.dig('data', '_service', 'sdl')).to match_sdl(
<<~GRAPHQL,
type Product @key(fields: "upc") {
adminField: String
upc: String!
}
type Query {
hello: String!
product: Product
}
GRAPHQL
)
end
end
end
end

if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('1.12.0')
Expand Down

0 comments on commit 6401feb

Please sign in to comment.