Skip to content

Commit 6dd1fe7

Browse files
authored
fix: service field context (#50)
* fix service field context * require keyword context * switch to positional arguments instead of keyword * adds tests * rubocop * switch back to keyword arguments
1 parent ccb8ec1 commit 6dd1fe7

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

lib/apollo-federation/service_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module ServiceField
1010
field(:_service, Service, null: false)
1111

1212
def _service
13-
{ sdl: context.schema.class.federation_sdl(context) }
13+
{ sdl: context.schema.class.federation_sdl(context: context) }
1414
end
1515
end
1616
end

spec/apollo-federation/service_field_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,76 @@ def execute_sdl(schema)
371371
GRAPHQL
372372
)
373373
end
374+
375+
context 'with a filter' do
376+
let(:schema) do
377+
product = Class.new(base_object) do
378+
graphql_name 'Product'
379+
380+
field :upc, String, null: false
381+
end
382+
383+
query_obj = Class.new(base_object) do
384+
graphql_name 'Query'
385+
386+
field :product, product, null: true
387+
end
388+
389+
Class.new(base_schema) do
390+
query query_obj
391+
end
392+
end
393+
let(:filter) do
394+
class PermissionWhitelist
395+
def call(_schema_member, context)
396+
context[:user_role] == :admin
397+
end
398+
end
399+
400+
PermissionWhitelist.new
401+
end
402+
let(:context) { { user_role: :admin } }
403+
let(:executed_with_context) do
404+
schema.execute('{ _service { sdl } }', only: filter, context: context)
405+
end
406+
let(:executed_without_context) { schema.execute('{ _service { sdl } }', only: filter) }
407+
408+
it 'passes context to filters' do
409+
expect(executed_with_context['data']['_service']['sdl']).to match_sdl(
410+
<<~GRAPHQL,
411+
type Product {
412+
upc: String!
413+
}
414+
415+
type Query {
416+
product: Product
417+
}
418+
GRAPHQL
419+
)
420+
end
421+
422+
it 'works without context' do
423+
expect(executed_without_context['errors']).to(
424+
match_array(
425+
[
426+
include('message' => "Field '_service' doesn't exist on type 'Query'"),
427+
],
428+
),
429+
)
430+
end
431+
432+
context 'when not authorized' do
433+
let(:context) { { user_role: :foo } }
434+
435+
it 'returns an error message' do
436+
expect(executed_with_context['errors']).to(
437+
match_array(
438+
[
439+
include('message' => "Field '_service' doesn't exist on type 'Query'"),
440+
],
441+
),
442+
)
443+
end
444+
end
445+
end
374446
end

0 commit comments

Comments
 (0)