|
2 | 2 | require "spec_helper"
|
3 | 3 |
|
4 | 4 | class InMemoryBackend
|
| 5 | + MAX_COMPLEXITY = 5 |
5 | 6 | class Subscriptions < GraphQL::Subscriptions
|
6 | 7 | attr_reader :deliveries, :pushes, :extra, :queries, :events
|
7 | 8 |
|
@@ -182,6 +183,7 @@ class Schema < GraphQL::Schema
|
182 | 183 | query(Query)
|
183 | 184 | subscription(Subscription)
|
184 | 185 | use InMemoryBackend::Subscriptions, extra: 123
|
| 186 | + max_complexity(InMemoryBackend::MAX_COMPLEXITY) |
185 | 187 | end
|
186 | 188 | end
|
187 | 189 |
|
@@ -240,6 +242,7 @@ class FromDefinitionInMemoryBackend < InMemoryBackend
|
240 | 242 | },
|
241 | 243 | }
|
242 | 244 | Schema = GraphQL::Schema.from_definition(SchemaDefinition, default_resolve: Resolvers, using: {InMemoryBackend::Subscriptions => { extra: 123 }})
|
| 245 | + Schema.max_complexity(MAX_COMPLEXITY) |
243 | 246 | # TODO don't hack this (no way to add metadata from IDL parser right now)
|
244 | 247 | Schema.get_field("Subscription", "myEvent").subscription_scope = :me
|
245 | 248 | end
|
@@ -765,6 +768,29 @@ def str
|
765 | 768 | assert_includes err.message, "arguments of StreamInput"
|
766 | 769 | end
|
767 | 770 | 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 |
768 | 794 | end
|
769 | 795 | end
|
770 | 796 |
|
|
0 commit comments