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

Don't allow re-assigning Query#validate after validation #4127

Merged
merged 1 commit into from Jul 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/graphql/query.rb
Expand Up @@ -34,7 +34,16 @@ def initialize(name)
attr_accessor :operation_name

# @return [Boolean] if false, static validation is skipped (execution behavior for invalid queries is undefined)
attr_accessor :validate
attr_reader :validate

# @param new_validate [Boolean] if false, static validation is skipped. This can't be reasssigned after validation.
def validate=(new_validate)
if defined?(@validation_pipeline) && @validation_pipeline && @validation_pipeline.has_validated?
raise ArgumentError, "Can't reassign Query#validate= after validation has run, remove this assignment."
else
@validate = new_validate
end
end

attr_writer :query_string

Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/query/validation_pipeline.rb
Expand Up @@ -45,6 +45,10 @@ def analyzers
@query_analyzers
end

def has_validated?
@has_validated == true
end

private

# If the pipeline wasn't run yet, run it.
Expand Down
18 changes: 18 additions & 0 deletions spec/graphql/query_spec.rb
Expand Up @@ -720,6 +720,24 @@ def self.parse_error(err, ctx)
assert_equal true, query.valid?
assert_equal 0, query.static_errors.length
end

it "can't be reassigned after validating" do
query = GraphQL::Query.new(schema, "{ nonExistingField }")
assert query.fingerprint
query.validate = false
assert_equal true, query.valid?
assert_equal 0, query.static_errors.length
err = assert_raises ArgumentError do
query.validate = true
end

err2 = assert_raises ArgumentError do
query.validate = false
end
expected_message = "Can't reassign Query#validate= after validation has run, remove this assignment."
assert_equal expected_message, err.message
assert_equal expected_message, err2.message
end
end

describe "validating with optional arguments and variables: nil" do
Expand Down