Skip to content

Commit 0b680d4

Browse files
committed
Rescue SystemStackErrors during validation
1 parent 558a43f commit 0b680d4

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/graphql/query/validation_pipeline.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def ensure_has_validated
9090
end
9191

9292
@valid = @validation_errors.empty?
93+
rescue SystemStackError => err
94+
@valid = false
95+
@schema.query_stack_error(@query, err)
9396
end
9497

9598
# If there are max_* values, add them,

lib/graphql/schema.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,10 @@ def add_subscription_extension_if_necessary
16631663
end
16641664
end
16651665

1666+
def query_stack_error(query, err)
1667+
query.analysis_errors.push(GraphQL::AnalysisError.new("This query is too large to execute."))
1668+
end
1669+
16661670
private
16671671

16681672
def lazy_methods

spec/graphql/analysis/ast/max_query_depth_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,20 @@
131131
assert_equal "Query has depth of 7, which exceeds max depth of 4", result.message
132132
end
133133
end
134+
135+
describe "when the query would cause a stack error" do
136+
let(:query_string) {
137+
str = "query { cheese(id: 1) { ".dup
138+
n = 10_000
139+
n.times { str << "similarCheese(source: SHEEP) { " }
140+
str << "id "
141+
n.times { str << "} " }
142+
str << "} }"
143+
str
144+
}
145+
146+
it "returns an error" do
147+
assert_equal ["This query is too large to execute."], query.static_errors.map(&:message)
148+
end
149+
end
134150
end

0 commit comments

Comments
 (0)