Skip to content

Commit f1d4266

Browse files
committed
Validate incoming orphan_types are object types
1 parent 718cd23 commit f1d4266

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/graphql/schema.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,17 @@ def extra_types(*new_extra_types)
857857
def orphan_types(*new_orphan_types)
858858
if new_orphan_types.any?
859859
new_orphan_types = new_orphan_types.flatten
860+
non_object_types = new_orphan_types.reject { |ot| ot.is_a?(Class) && ot < GraphQL::Schema::Object }
861+
if non_object_types.any?
862+
raise ArgumentError, <<~ERR
863+
Only object type classes should be added as `orphan_types(...)`.
864+
865+
- Remove these no-op types from `orphan_types`: #{non_object_types.map(&:inspect).join(", ")}
866+
- See https://graphql-ruby.org/type_definitions/interfaces.html#orphan-types
867+
868+
To add other types to your schema, you might want `extra_types`: https://graphql-ruby.org/schema/definition.html#extra-types
869+
ERR
870+
end
860871
add_type_and_traverse(new_orphan_types, root: false)
861872
own_orphan_types.concat(new_orphan_types.flatten)
862873
end

spec/graphql/schema_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions
152152
end
153153
end
154154

155+
class ExampleOptionEnum < GraphQL::Schema::Enum
156+
end
157+
it "rejects non-object types to orphan_types" do
158+
object_type = Class.new(GraphQL::Schema::Object)
159+
err = assert_raises ArgumentError do
160+
Class.new(GraphQL::Schema) do
161+
orphan_types(ExampleOptionEnum, object_type)
162+
end
163+
end
164+
165+
expected_msg = "Only object type classes should be added as `orphan_types(...)`.
166+
167+
- Remove these no-op types from `orphan_types`: ExampleOptionEnum
168+
- See https://graphql-ruby.org/type_definitions/interfaces.html#orphan-types
169+
170+
To add other types to your schema, you might want `extra_types`: https://graphql-ruby.org/schema/definition.html#extra-types
171+
"
172+
assert_equal expected_msg, err.message
173+
end
174+
155175
describe "merged, inherited caches" do
156176
METHODS_TO_CACHE = {
157177
types: 1,

spec/support/dummy/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ class Schema < GraphQL::Schema
517517
mutation DairyAppMutation
518518
subscription Subscription
519519
max_depth 5
520-
orphan_types Honey, Beverage
520+
orphan_types Honey
521521
trace_with GraphQL::Tracing::CallLegacyTracers
522522

523523
rescue_from(NoSuchDairyError) { |err| raise GraphQL::ExecutionError, err.message }

0 commit comments

Comments
 (0)