Skip to content

Commit

Permalink
Merge pull request #4127 from rmosolgo/query-validate-no-reassignment
Browse files Browse the repository at this point in the history
Don't allow re-assigning Query#validate after validation
  • Loading branch information
rmosolgo committed Jul 4, 2022
2 parents d1b9212 + ef943d4 commit ccd30db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
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

0 comments on commit ccd30db

Please sign in to comment.