Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subscription inheritance/override #4108

Merged
merged 1 commit into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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