Skip to content

Commit

Permalink
fix: service field context (#50)
Browse files Browse the repository at this point in the history
* fix service field context

* require keyword context

* switch to positional arguments instead of keyword

* adds tests

* rubocop

* switch back to keyword arguments
  • Loading branch information
matthewjf authored Feb 19, 2020
1 parent ccb8ec1 commit 6dd1fe7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/apollo-federation/service_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module ServiceField
field(:_service, Service, null: false)

def _service
{ sdl: context.schema.class.federation_sdl(context) }
{ sdl: context.schema.class.federation_sdl(context: context) }
end
end
end
72 changes: 72 additions & 0 deletions spec/apollo-federation/service_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,76 @@ def execute_sdl(schema)
GRAPHQL
)
end

context 'with a filter' do
let(:schema) do
product = Class.new(base_object) do
graphql_name 'Product'

field :upc, String, null: false
end

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

field :product, product, null: true
end

Class.new(base_schema) do
query query_obj
end
end
let(:filter) do
class PermissionWhitelist
def call(_schema_member, context)
context[:user_role] == :admin
end
end

PermissionWhitelist.new
end
let(:context) { { user_role: :admin } }
let(:executed_with_context) do
schema.execute('{ _service { sdl } }', only: filter, context: context)
end
let(:executed_without_context) { schema.execute('{ _service { sdl } }', only: filter) }

it 'passes context to filters' do
expect(executed_with_context['data']['_service']['sdl']).to match_sdl(
<<~GRAPHQL,
type Product {
upc: String!
}
type Query {
product: Product
}
GRAPHQL
)
end

it 'works without context' do
expect(executed_without_context['errors']).to(
match_array(
[
include('message' => "Field '_service' doesn't exist on type 'Query'"),
],
),
)
end

context 'when not authorized' do
let(:context) { { user_role: :foo } }

it 'returns an error message' do
expect(executed_with_context['errors']).to(
match_array(
[
include('message' => "Field '_service' doesn't exist on type 'Query'"),
],
),
)
end
end
end
end

0 comments on commit 6dd1fe7

Please sign in to comment.