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

Validate only one Subscription selection #5250

Merged
merged 1 commit into from
Mar 25, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/graphql/static_validation/all_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module StaticValidation
GraphQL::StaticValidation::VariableUsagesAreAllowed,
GraphQL::StaticValidation::MutationRootExists,
GraphQL::StaticValidation::QueryRootExists,
GraphQL::StaticValidation::SubscriptionRootExists,
GraphQL::StaticValidation::SubscriptionRootExistsAndSingleSubscriptionSelection,
GraphQL::StaticValidation::InputObjectNamesAreUnique,
GraphQL::StaticValidation::OneOfInputObjectsAreValid,
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true
module GraphQL
module StaticValidation
class NotSingleSubscriptionError < StaticValidation::Error
def initialize(message, path: nil, nodes: [])
super(message, path: path, nodes: nodes)
end

# A hash representation of this Message
def to_h
extensions = {
"code" => code,
}

super.merge({
"extensions" => extensions
})
end

def code
"notSingleSubscription"
end
end
end
end
17 changes: 0 additions & 17 deletions lib/graphql/static_validation/rules/subscription_root_exists.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true
module GraphQL
module StaticValidation
module SubscriptionRootExistsAndSingleSubscriptionSelection
def on_operation_definition(node, parent)
if node.operation_type == "subscription"
if context.types.subscription_root.nil?
add_error(GraphQL::StaticValidation::SubscriptionRootExistsError.new(
'Schema is not configured for subscriptions',
nodes: node
))
elsif node.selections.size != 1
add_error(GraphQL::StaticValidation::NotSingleSubscriptionError.new(
'A subscription operation may only have one selection',
nodes: node,
))
else
super
end
else
super
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::StaticValidation::SubscriptionRootExistsAndSingleSubscriptionSelection do
include StaticValidationHelpers

let(:query_string) {%|
subscription {
test
}
|}

let(:schema) {
Class.new(GraphQL::Schema) do
query_root = Class.new(GraphQL::Schema::Object) do
graphql_name "Query"
end

query query_root
end
}

it "errors when a subscription is performed on a schema without a subscription root" do
assert_equal(1, errors.length)
missing_subscription_root_error = {
"message"=>"Schema is not configured for subscriptions",
"locations"=>[{"line"=>2, "column"=>5}],
"path"=>["subscription"],
"extensions"=>{"code"=>"missingSubscriptionConfiguration"}
}
assert_includes(errors, missing_subscription_root_error)
end

describe "when multiple subscription selections" do
let(:query_string) {
"subscription { subscription1 subscription2 }"
}

let(:schema) {
Class.new(GraphQL::Schema) do
subscription(Class.new(GraphQL::Schema::Object) do
graphql_name "Subscription"
field :subscription1, String
field :subscription2, String
end)
end
}

it "returns an error" do
expected_errs = [
{
"message" => "A subscription operation may only have one selection",
"locations" => [{"line" => 1, "column" => 1}],
"path" => ["subscription"],
"extensions" => {"code" => "notSingleSubscription"}
}
]
assert_equal(expected_errs, errors)
end
end
end

This file was deleted.

33 changes: 0 additions & 33 deletions spec/graphql/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ def to_param
query_str = <<-GRAPHQL
subscription ($id: ID!){
firstPayload: payload(id: $id) { str, int }
otherPayload: payload(id: "900") { int }
}
GRAPHQL

Expand Down Expand Up @@ -416,38 +415,6 @@ def to_param
end
end

it "sends updated data for multifield subscriptions" do
query_str = <<-GRAPHQL
subscription ($id: ID!){
payload(id: $id) { str, int }
event { int }
}
GRAPHQL

# Initial subscriptions
res = schema.execute(query_str, context: { socket: "1" }, variables: { "id" => "100" }, root_value: root_object)
empty_response = {}

# Initial response is nil, no broadcasts yet
assert_equal(empty_response, res["data"])
assert_equal [], deliveries["1"]

# Application stuff happens.
# The application signals graphql via `subscriptions.trigger`:
schema.subscriptions.trigger(:payload, {"id" => "100"}, root_object.payload)

# Let's see what GraphQL sent over the wire:
assert_equal({"str" => "Update", "int" => 1}, deliveries["1"][0]["data"]["payload"])
assert_nil(deliveries["1"][0]["data"]["event"])

# Trigger another field subscription
schema.subscriptions.trigger(:event, {}, OpenStruct.new(int: 1))

# Now we should get result for another field
assert_nil(deliveries["1"][1]["data"]["payload"])
assert_equal({"int" => 1}, deliveries["1"][1]["data"]["event"])
end

describe "passing a document into #execute" do
it "sends the updated data" do
query_str = <<-GRAPHQL
Expand Down
Loading