Skip to content

Commit

Permalink
Add tests for root types from blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Aug 8, 2024
1 parent f5033aa commit d9aac57
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ def subscription(new_subscription_object = nil, &lazy_load_block)
nil
elsif @subscription_object.is_a?(Proc)
@subscription_object = @subscription_object.call
add_subscription_extension_if_necessary
@subscription_object
else
@subscription_object || find_inherited_value(:subscription)
end
Expand Down Expand Up @@ -1387,7 +1389,8 @@ def instrumenters

# @api private
def add_subscription_extension_if_necessary
if !defined?(@subscription_extension_added) && subscription && self.subscriptions
# TODO: when there's a proper API for extending root types, migrat this to use it.
if !defined?(@subscription_extension_added) && @subscription_object.is_a?(Class) && self.subscriptions
@subscription_extension_added = true
subscription.all_field_definitions.each do |field|
if !field.extensions.any? { |ext| ext.is_a?(Subscriptions::DefaultSubscriptionResolveExtension) }
Expand Down
20 changes: 20 additions & 0 deletions spec/graphql/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,24 @@ class QueryRequiredSchema < GraphQL::Schema
it "starts with no references_to" do
assert_equal({}, GraphQL::Schema.references_to)
end

it "defers root type blocks until those types are used" do
calls = []
schema = Class.new(GraphQL::Schema) do
self.use_schema_subset = true
query { calls << :query; Class.new(GraphQL::Schema::Object) { graphql_name("Query") } }
mutation { calls << :mutation; Class.new(GraphQL::Schema::Object) { graphql_name("Mutation") } }
subscription { calls << :subscription; Class.new(GraphQL::Schema::Object) { graphql_name("Subscription") } }
# Test this because it tries to modify `subscription` -- currently hardcoded in Schema.add_subscription_extension_if_necessary
use GraphQL::Subscriptions
end

assert_equal [], calls
assert_equal "Query", schema.query.graphql_name
assert_equal [:query], calls
assert_equal "Mutation", schema.mutation.graphql_name
assert_equal [:query, :mutation], calls
assert_equal "Subscription", schema.subscription.graphql_name
assert_equal [:query, :mutation, :subscription], calls
end
end
4 changes: 2 additions & 2 deletions spec/graphql/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ class Query < GraphQL::Schema::Object
end

class Schema < GraphQL::Schema
query(Query)
subscription(Subscription)
query { Query }
subscription { Subscription }
use InMemoryBackend::Subscriptions, extra: 123
max_complexity(InMemoryBackend::MAX_COMPLEXITY)
end
Expand Down

0 comments on commit d9aac57

Please sign in to comment.