Skip to content

Commit 03c2bb6

Browse files
author
Robert Mosolgo
authored
Merge pull request #3450 from eapache/patch-2
Respect directive visibility
2 parents 8c981e7 + a9363c5 commit 03c2bb6

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/graphql/static_validation/rules/directives_are_defined.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module StaticValidation
44
module DirectivesAreDefined
55
def initialize(*)
66
super
7-
@directive_names = context.schema.directives.keys
7+
@directive_names = context.warden.directives.map(&:graphql_name)
88
end
99

1010
def on_directive(node, parent)

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)