Skip to content

Commit

Permalink
Merge pull request #4108 from rmosolgo/subscription-inheritance
Browse files Browse the repository at this point in the history
Fix subscription inheritance/override
  • Loading branch information
rmosolgo committed Jun 20, 2022
2 parents 5d0ec8e + 4df01f6 commit 31ad393
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/graphql/schema.rb
Expand Up @@ -132,7 +132,13 @@ def deprecated_graphql_definition
end

# @return [GraphQL::Subscriptions]
attr_accessor :subscriptions
def subscriptions(inherited: true)
defined?(@subscriptions) ? @subscriptions : (inherited ? find_inherited_value(:subscriptions, nil) : nil)
end

def subscriptions=(new_implementation)
@subscriptions = new_implementation
end

# Returns the JSON response of {Introspection::INTROSPECTION_QUERY}.
# @see {#as_json}
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/subscriptions.rb
Expand Up @@ -26,7 +26,7 @@ class SubscriptionScopeMissingError < GraphQL::Error
def self.use(defn, options = {})
schema = defn.is_a?(Class) ? defn : defn.target

if schema.subscriptions
if schema.subscriptions(inherited: false)
raise ArgumentError, "Can't reinstall subscriptions. #{schema} is using #{schema.subscriptions}, can't also add #{self}"
end

Expand Down
15 changes: 12 additions & 3 deletions spec/graphql/schema_spec.rb
Expand Up @@ -23,6 +23,9 @@ class Subscription < GraphQL::Schema::Object
field :some_field, String
end

class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions
end

let(:base_schema) do
Class.new(GraphQL::Schema) do
query Query
Expand All @@ -47,6 +50,7 @@ class Subscription < GraphQL::Schema::Object
multiplex_analyzer Object.new
rescue_from(StandardError) { }
use GraphQL::Backtrace
use GraphQL::Subscriptions::ActionCableSubscriptions, action_cable: nil, action_cable_coder: JSON
end
end

Expand All @@ -73,11 +77,15 @@ class Subscription < GraphQL::Schema::Object
assert_equal base_schema.query_analyzers, schema.query_analyzers
assert_equal base_schema.multiplex_analyzers, schema.multiplex_analyzers
assert_equal base_schema.disable_introspection_entry_points?, schema.disable_introspection_entry_points?
assert_equal [GraphQL::Backtrace], schema.plugins.map(&:first)
assert_equal [GraphQL::Backtrace, GraphQL::Subscriptions::ActionCableSubscriptions], schema.plugins.map(&:first)
assert_instance_of GraphQL::Subscriptions::ActionCableSubscriptions, schema.subscriptions
end

it "can override configuration from its superclass" do
schema = Class.new(base_schema)
schema = Class.new(base_schema) do
use CustomSubscriptions, action_cable: nil, action_cable_coder: JSON
end

query = Class.new(GraphQL::Schema::Object) do
graphql_name 'Query'
field :some_field, String
Expand Down Expand Up @@ -141,9 +149,10 @@ class Subscription < GraphQL::Schema::Object
assert_equal schema.directives, GraphQL::Schema.default_directives.merge(DummyFeature1.graphql_name => DummyFeature1, DummyFeature2.graphql_name => DummyFeature2)
assert_equal base_schema.query_analyzers + [query_analyzer], schema.query_analyzers
assert_equal base_schema.multiplex_analyzers + [multiplex_analyzer], schema.multiplex_analyzers
assert_equal [GraphQL::Backtrace], schema.plugins.map(&:first)
assert_equal [GraphQL::Backtrace, GraphQL::Subscriptions::ActionCableSubscriptions, CustomSubscriptions], schema.plugins.map(&:first)
assert_equal [GraphQL::Tracing::DataDogTracing, GraphQL::Backtrace::Tracer], base_schema.tracers
assert_equal [GraphQL::Tracing::DataDogTracing, GraphQL::Backtrace::Tracer, GraphQL::Tracing::NewRelicTracing], schema.tracers
assert_instance_of CustomSubscriptions, schema.subscriptions
end
end

Expand Down

0 comments on commit 31ad393

Please sign in to comment.