Skip to content

Commit e4e6902

Browse files
authored
Merge pull request #5258 from Shopify/add-default-validate-timeout-value
Set default timeout for GraphQL schema validation
2 parents 9302a55 + 0a17a1e commit e4e6902

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

guides/queries/timeout.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,21 @@ end
6767

6868
Queries can originate from a user, and may be crafted in a manner to take a long time to validate against the schema.
6969

70-
It is possible to limit how many seconds the static validation rules and analysers are allowed to run before returning a validation timeout error. The default is no timeout.
70+
It is possible to limit how many seconds the static validation rules and analysers are allowed to run before returning a validation timeout error. By default, validation and query analysis have a 3-second timeout. You can customize this timeout or disable it completely:
7171

7272
For example:
7373

7474
```ruby
75+
# Customize timeout (in seconds)
7576
class MySchema < GraphQL::Schema
7677
# Applies to static validation and query analysis
7778
validate_timeout 10
7879
end
80+
81+
# OR disable timeout completely
82+
class MySchema < GraphQL::Schema
83+
validate_timeout nil
84+
end
7985
```
8086

8187
**Note:** This configuration uses Ruby's built-in `Timeout` API, which can interrupt IO calls mid-flight, resulting in [very weird bugs](https://www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/). None of GraphQL-Ruby's validators make IO calls but if you want to use this configuration and you have custom static validators that make IO calls, open an issue to discuss implementing this in an IO-safe way.

lib/graphql/schema.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ def validate_timeout(new_validate_timeout = NOT_CONFIGURED)
827827
elsif defined?(@validate_timeout)
828828
@validate_timeout
829829
else
830-
find_inherited_value(:validate_timeout)
830+
find_inherited_value(:validate_timeout) || 3
831831
end
832832
end
833833

spec/graphql/schema_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,24 @@ def test
604604
assert_equal expected_errors, schema.execute(query_str).to_h['errors']
605605
end
606606
end
607+
describe ".validate_timeout" do
608+
it "provides a default timeout when not explicitly set" do
609+
schema = Class.new(GraphQL::Schema)
610+
assert_equal 3, schema.validate_timeout
611+
end
612+
613+
it "allows overriding the default timeout" do
614+
schema = Class.new(GraphQL::Schema) do
615+
validate_timeout 15
616+
end
617+
assert_equal 15, schema.validate_timeout
618+
end
619+
620+
it "allows disabling the timeout" do
621+
schema = Class.new(GraphQL::Schema) do
622+
validate_timeout nil
623+
end
624+
assert_nil schema.validate_timeout
625+
end
626+
end
607627
end

0 commit comments

Comments
 (0)