Skip to content

Commit 6183718

Browse files
committed
Add a module and spec
1 parent abfc290 commit 6183718

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

lib/graphql/query/null_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def initialize
2121
@query = NullQuery.new
2222
@dataloader = GraphQL::Dataloader::NullDataloader.new
2323
@schema = NullSchema
24-
@warden = Schema::Warden::NullWarden.new(nil, context: self, schema: @schema)
24+
@warden = Schema::Warden::NullWarden.new(context: self, schema: @schema)
2525
end
2626

2727
def interpreter?

lib/graphql/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22
require "graphql/schema/addition"
3+
require "graphql/schema/always_visible"
34
require "graphql/schema/base_64_encoder"
45
require "graphql/schema/find_inherited_value"
56
require "graphql/schema/finder"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
module GraphQL
3+
class Schema
4+
class AlwaysVisible
5+
def self.use(schema, **opts)
6+
schema.warden_class = GraphQL::Schema::Warden::NullWarden
7+
end
8+
end
9+
end
10+
end

lib/graphql/schema/warden.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ def arguments(owner, ctx); owner.arguments(ctx); end
8888
end
8989

9090
class NullWarden
91-
def initialize(_filter, context:, schema:)
91+
def initialize(_filter = nil, context:, schema:)
9292
@schema = schema
93-
# TODO consider per-query caches even though there's no filtering
9493
end
9594

9695
def visible_field?(field_defn, _ctx = nil, owner = nil); true; end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
require "spec_helper"
3+
4+
describe GraphQL::Schema::AlwaysVisible do
5+
class AlwaysVisibleSchema < GraphQL::Schema
6+
class Query < GraphQL::Schema::Object
7+
def self.visible?(ctx)
8+
ctx[:visible_was_called] = true
9+
false
10+
end
11+
12+
field :one, Integer
13+
def one; 1; end
14+
end
15+
query(Query)
16+
use GraphQL::Schema::AlwaysVisible
17+
end
18+
19+
class NotAlwaysVisibleSchema < GraphQL::Schema
20+
query(AlwaysVisibleSchema::Query)
21+
end
22+
23+
it "Doesn't call visibility methods" do
24+
res = NotAlwaysVisibleSchema.execute("{ one }")
25+
assert res.context[:visible_was_called]
26+
assert_equal ["Schema is not configured for queries"], res["errors"].map { |err| err["message"] }
27+
28+
res = AlwaysVisibleSchema.execute("{ one }")
29+
refute res.context[:visible_was_called]
30+
assert_equal 1, res["data"]["one"]
31+
end
32+
end

0 commit comments

Comments
 (0)