Skip to content

Commit b690902

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#2134 from rmosolgo/fix-scoping-nil
Fix scoping nil
2 parents 25be1b9 + 5bdf597 commit b690902

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/graphql/schema/field/scope_extension.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ class Schema
55
class Field
66
class ScopeExtension < GraphQL::Schema::FieldExtension
77
def after_resolve(value:, context:, **rest)
8-
ret_type = @field.type.unwrap
9-
if ret_type.respond_to?(:scope_items)
10-
ret_type.scope_items(value, context)
11-
else
8+
if value.nil?
129
value
10+
else
11+
ret_type = @field.type.unwrap
12+
if ret_type.respond_to?(:scope_items)
13+
ret_type.scope_items(value, context)
14+
else
15+
value
16+
end
1317
end
1418
end
1519
end

spec/graphql/schema/member/scoped_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def self.scope_items(items, context)
1313
elsif context[:english]
1414
items.select { |i| i.name == "Paperclip" }
1515
else
16-
[]
16+
# boot everything
17+
items.reject { true }
1718
end
1819
end
1920

@@ -55,6 +56,12 @@ class Query < BaseObject
5556
field :unscoped_items, [Item], null: false,
5657
scope: false,
5758
resolver_method: :items
59+
60+
field :nil_items, [Item], null: true
61+
def nil_items
62+
nil
63+
end
64+
5865
field :french_items, [FrenchItem], null: false,
5966
resolver_method: :items
6067
if TESTING_INTERPRETER
@@ -122,6 +129,19 @@ def get_item_names_with_context(ctx, field_name: "items")
122129
assert_equal ["Trombone", "Paperclip"], get_item_names_with_context({}, field_name: "unscopedItems")
123130
end
124131

132+
it "returns null when the value is nil" do
133+
query_str = "
134+
{
135+
nilItems {
136+
name
137+
}
138+
}
139+
"
140+
res = ScopeSchema.execute(query_str)
141+
refute res.key?("errors")
142+
assert_nil res.fetch("data").fetch("nilItems")
143+
end
144+
125145
it "is inherited" do
126146
assert_equal ["Trombone"], get_item_names_with_context({}, field_name: "frenchItems")
127147
end

0 commit comments

Comments
 (0)