Skip to content

Commit a9363c5

Browse files
committed
Add a test for directive visibility
1 parent 4cd9a6b commit a9363c5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

spec/graphql/authorization_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,17 @@ class Mutation < BaseObject
402402
field :do_unauthorized_stuff, mutation: DoUnauthorizedStuff
403403
end
404404

405+
class Nothing < GraphQL::Schema::Directive
406+
locations(FIELD)
407+
def self.visible?(ctx)
408+
!!ctx[:show_nothing_directive]
409+
end
410+
end
411+
405412
class Schema < GraphQL::Schema
406413
query(Query)
407414
mutation(Mutation)
415+
directive(Nothing)
408416

409417
lazy_resolve(Box, :value)
410418

@@ -601,6 +609,14 @@ def auth_execute(*args, **kwargs)
601609
refute_includes restricted_sdl, 'Hidden'
602610
refute_includes restricted_sdl, 'hidden'
603611
end
612+
613+
it "works with directives" do
614+
query_str = "{ __typename @nothing }"
615+
visible_response = auth_execute(query_str, context: { show_nothing_directive: true })
616+
assert_equal "Query", visible_response["data"]["__typename"]
617+
hidden_response = auth_execute(query_str)
618+
assert_equal ["Directive @nothing is not defined"], hidden_response["errors"].map { |e| e["message"] }
619+
end
604620
end
605621

606622
describe "applying the authorized? method" do

0 commit comments

Comments
 (0)