Skip to content

Commit 39ff412

Browse files
authored
Merge pull request rmosolgo#3904 from rmosolgo/subscription-complexity
Add a test for complexity errors on subscriptions
2 parents 492a79d + bb01108 commit 39ff412

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

spec/graphql/subscriptions_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "spec_helper"
33

44
class InMemoryBackend
5+
MAX_COMPLEXITY = 5
56
class Subscriptions < GraphQL::Subscriptions
67
attr_reader :deliveries, :pushes, :extra, :queries, :events
78

@@ -182,6 +183,7 @@ class Schema < GraphQL::Schema
182183
query(Query)
183184
subscription(Subscription)
184185
use InMemoryBackend::Subscriptions, extra: 123
186+
max_complexity(InMemoryBackend::MAX_COMPLEXITY)
185187
end
186188
end
187189

@@ -240,6 +242,7 @@ class FromDefinitionInMemoryBackend < InMemoryBackend
240242
},
241243
}
242244
Schema = GraphQL::Schema.from_definition(SchemaDefinition, default_resolve: Resolvers, using: {InMemoryBackend::Subscriptions => { extra: 123 }})
245+
Schema.max_complexity(MAX_COMPLEXITY)
243246
# TODO don't hack this (no way to add metadata from IDL parser right now)
244247
Schema.get_field("Subscription", "myEvent").subscription_scope = :me
245248
end
@@ -765,6 +768,29 @@ def str
765768
assert_includes err.message, "arguments of StreamInput"
766769
end
767770
end
771+
772+
describe "max_complexity" do
773+
it "rejects subscriptions with errors" do
774+
query_str = <<-GRAPHQL
775+
subscription($type: PayloadType) {
776+
myEvent(payloadType: $type) {
777+
s1: str
778+
s2: str
779+
s3: str
780+
s4: str
781+
s5: str
782+
s6: str
783+
}
784+
}
785+
GRAPHQL
786+
787+
res = schema.execute(query_str, context: { socket: "1"})
788+
errs = ["Query has complexity of 7, which exceeds max complexity of 5"]
789+
assert_equal errs, res["errors"].map { |e| e["message"] }
790+
assert_equal 0, implementation.events.size
791+
assert_equal 0, implementation.queries.size
792+
end
793+
end
768794
end
769795
end
770796

0 commit comments

Comments
 (0)