Skip to content

Commit 089854a

Browse files
committed
add more tests.
1 parent 7d71989 commit 089854a

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed

lib/graphql/stitching/executor.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,8 @@ def perform(raw: false)
4545
result["data"] = raw ? @data : Shaper.new(@request).perform!(@data)
4646
end
4747

48-
@request.plan.errors.each do |error|
49-
case error.code
50-
when "unauthorized"
51-
@errors << {
52-
"message" => "Unauthorized access",
53-
"path" => error.path,
54-
}
55-
end
48+
@request.plan.errors.each do |err|
49+
@errors << err.to_h
5650
end
5751

5852
if @errors.length > 0

lib/graphql/stitching/plan.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def ==(other)
6666
end
6767

6868
class Error
69+
MESSAGE_BY_CODE = {
70+
"unauthorized" => "Unauthorized access",
71+
}.freeze
72+
6973
attr_reader :code, :path
7074

7175
def initialize(code:, path:)
@@ -79,6 +83,14 @@ def as_json
7983
path: path,
8084
}
8185
end
86+
87+
def to_h
88+
{
89+
"message" => MESSAGE_BY_CODE[@code],
90+
"path" => @path,
91+
"extensions" => { "code" => @code },
92+
}
93+
end
8294
end
8395

8496
class << self

test/graphql/stitching/integration/authorizations_test.rb

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,85 @@ def setup
1111
})
1212
end
1313

14-
def test_responds_with_error
14+
def test_responds_with_errors_for_each_unauthorized_child_field
1515
query = %|{
1616
orderA(id: "1") {
1717
customer1 {
1818
phone
19+
slack
1920
}
2021
}
2122
}|
2223

23-
result = plan_and_execute(@supergraph, query, claims: ["orders"]) do |plan|
24-
pp plan.as_json
25-
end
24+
result = plan_and_execute(@supergraph, query, claims: ["orders"])
25+
expected = {
26+
"data" => {
27+
"orderA" => {
28+
"customer1" => {
29+
"phone" => nil,
30+
"slack" => nil,
31+
},
32+
},
33+
},
34+
"errors" => [{
35+
"message" => "Unauthorized access",
36+
"path" => ["orderA", "customer1", "phone"],
37+
"extensions" => { "code" => "unauthorized" },
38+
}, {
39+
"message" => "Unauthorized access",
40+
"path" => ["orderA", "customer1", "slack"],
41+
"extensions" => { "code" => "unauthorized" },
42+
}],
43+
}
2644

27-
pp result.to_h
28-
assert true
45+
assert_equal expected, result.to_h
46+
end
47+
48+
def test_errors_of_non_null_child_fields_bubble
49+
query = %|{
50+
orderA(id: "1") {
51+
customer1 {
52+
email
53+
}
54+
}
55+
}|
56+
57+
result = plan_and_execute(@supergraph, query, claims: ["orders"])
58+
expected = {
59+
"data" => {
60+
"orderA" => { "customer1" => nil },
61+
},
62+
"errors" => [{
63+
"message" => "Unauthorized access",
64+
"path" => ["orderA", "customer1", "email"],
65+
"extensions" => { "code" => "unauthorized" },
66+
}],
67+
}
68+
69+
assert_equal expected, result.to_h
70+
end
71+
72+
def test_responds_with_error_for_unauthorized_parent_field
73+
query = %|{
74+
orderA(id: "1") {
75+
customer2 {
76+
phone
77+
}
78+
}
79+
}|
80+
81+
result = plan_and_execute(@supergraph, query, claims: ["orders"])
82+
expected = {
83+
"data" => {
84+
"orderA" => { "customer2" => nil },
85+
},
86+
"errors" => [{
87+
"message" => "Unauthorized access",
88+
"path" => ["orderA", "customer2"],
89+
"extensions" => { "code" => "unauthorized" },
90+
}],
91+
}
92+
93+
assert_equal expected, result.to_h
2994
end
3095
end

test/schemas/authorizations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Customer < GraphQL::Schema::Object
1818
directive GraphQL::Stitching::Directives::Authorization, scopes: [["customers"]]
1919
field :email, String, null: false
2020
field :phone, String, null: true
21+
field :slack, String, null: true
2122
end
2223

2324
class Product < GraphQL::Schema::Object

0 commit comments

Comments
 (0)