Skip to content

Commit cb9c19f

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#1580 from swalkinshaw/add-possible-type-methods-to-abstract-types
Add possible type helpers to abstract types
2 parents 0a01aea + 2108de3 commit cb9c19f

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

lib/graphql/interface_type.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,23 @@ def get_field(field_name)
6464
def all_fields
6565
fields.values
6666
end
67+
68+
# Get a possible type of this {InterfaceType} by type name
69+
# @param type_name [String]
70+
# @param ctx [GraphQL::Query::Context] The context for the current query
71+
# @return [GraphQL::ObjectType, nil] The type named `type_name` if it exists and implements this {InterfaceType}, (else `nil`)
72+
def get_possible_type(type_name, ctx)
73+
type = ctx.query.get_type(type_name)
74+
type if type && ctx.query.schema.possible_types(self).include?(type)
75+
end
76+
77+
# Check if a type is a possible type of this {InterfaceType}
78+
# @param type [String, GraphQL::BaseType] Name of the type or a type definition
79+
# @param ctx [GraphQL::Query::Context] The context for the current query
80+
# @return [Boolean] True if the `type` exists and is a member of this {InterfaceType}, (else `nil`)
81+
def possible_type?(type, ctx)
82+
type_name = type.is_a?(String) ? type : type.graphql_name
83+
!get_possible_type(type_name, ctx).nil?
84+
end
6785
end
6886
end

lib/graphql/union_type.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ def possible_types
6767
end
6868
end
6969

70+
# Get a possible type of this {UnionType} by type name
71+
# @param type_name [String]
72+
# @param ctx [GraphQL::Query::Context] The context for the current query
73+
# @return [GraphQL::ObjectType, nil] The type named `type_name` if it exists and is a member of this {UnionType}, (else `nil`)
74+
def get_possible_type(type_name, ctx)
75+
type = ctx.query.get_type(type_name)
76+
type if type && ctx.query.schema.possible_types(self).include?(type)
77+
end
78+
79+
# Check if a type is a possible type of this {UnionType}
80+
# @param type [String, GraphQL::BaseType] Name of the type or a type definition
81+
# @param ctx [GraphQL::Query::Context] The context for the current query
82+
# @return [Boolean] True if the `type` exists and is a member of this {UnionType}, (else `nil`)
83+
def possible_type?(type, ctx)
84+
type_name = type.is_a?(String) ? type : type.graphql_name
85+
!get_possible_type(type_name, ctx).nil?
86+
end
87+
7088
def resolve_type(value, ctx)
7189
ctx.query.resolve_type(self, value)
7290
end

spec/graphql/interface_type_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,48 @@
149149
assert_equal expected_result, result["data"]
150150
end
151151
end
152+
153+
describe "#get_possible_type" do
154+
let(:query_string) {%|
155+
query fav {
156+
favoriteEdible { fatContent }
157+
}
158+
|}
159+
160+
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string) }
161+
162+
it "returns the type definition if the type exists and is a possible type of the interface" do
163+
assert interface.get_possible_type("Cheese", query.context)
164+
end
165+
166+
it "returns nil if the type is not found in the schema" do
167+
assert_nil interface.get_possible_type("Foo", query.context)
168+
end
169+
170+
it "returns nil if the type is not a possible type of the interface" do
171+
assert_nil interface.get_possible_type("Beverage", query.context)
172+
end
173+
end
174+
175+
describe "#possible_type?" do
176+
let(:query_string) {%|
177+
query fav {
178+
favoriteEdible { fatContent }
179+
}
180+
|}
181+
182+
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string) }
183+
184+
it "returns true if the type exists and is a possible type of the interface" do
185+
assert interface.possible_type?("Cheese", query.context)
186+
end
187+
188+
it "returns false if the type is not found in the schema" do
189+
refute interface.possible_type?("Foo", query.context)
190+
end
191+
192+
it "returns false if the type is not a possible type of the interface" do
193+
refute interface.possible_type?("Beverage", query.context)
194+
end
195+
end
152196
end

spec/graphql/union_type_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,54 @@
158158
assert_equal 3, union_2.possible_types.size
159159
end
160160
end
161+
162+
describe "#get_possible_type" do
163+
let(:query_string) {%|
164+
{
165+
__type(name: "Beverage") {
166+
name
167+
}
168+
}
169+
|}
170+
171+
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string) }
172+
let(:union) { Dummy::BeverageUnion }
173+
174+
it "returns the type definition if the type exists and is a possible type of the union" do
175+
assert union.get_possible_type("Milk", query.context)
176+
end
177+
178+
it "returns nil if the type is not found in the schema" do
179+
assert_nil union.get_possible_type("Foo", query.context)
180+
end
181+
182+
it "returns nil if the type is not a possible type of the union" do
183+
assert_nil union.get_possible_type("Cheese", query.context)
184+
end
185+
end
186+
187+
describe "#possible_type?" do
188+
let(:query_string) {%|
189+
{
190+
__type(name: "Beverage") {
191+
name
192+
}
193+
}
194+
|}
195+
196+
let(:query) { GraphQL::Query.new(Dummy::Schema, query_string) }
197+
let(:union) { Dummy::BeverageUnion }
198+
199+
it "returns true if the type exists and is a possible type of the union" do
200+
assert union.possible_type?("Milk", query.context)
201+
end
202+
203+
it "returns false if the type is not found in the schema" do
204+
refute union.possible_type?("Foo", query.context)
205+
end
206+
207+
it "returns false if the type is not a possible type of the union" do
208+
refute union.possible_type?("Cheese", query.context)
209+
end
210+
end
161211
end

0 commit comments

Comments
 (0)