2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-21 07:42:07 -04:00
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
class Variables
|
|
|
|
Invalid = Class.new(Gitlab::Graphql::StandardGraphqlError)
|
|
|
|
|
|
|
|
def initialize(param)
|
|
|
|
@param = param
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
ensure_hash(@param)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Handle form data, JSON body, or a blank value
|
|
|
|
def ensure_hash(ambiguous_param)
|
|
|
|
case ambiguous_param
|
|
|
|
when String
|
|
|
|
if ambiguous_param.present?
|
2020-04-30 20:09:59 -04:00
|
|
|
ensure_hash(Gitlab::Json.parse(ambiguous_param))
|
2018-05-21 07:42:07 -04:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
2021-10-29 11:11:56 -04:00
|
|
|
when Hash
|
2018-05-21 07:42:07 -04:00
|
|
|
ambiguous_param
|
2021-10-29 11:11:56 -04:00
|
|
|
when ActionController::Parameters
|
|
|
|
# We can and have to trust the "Parameters" because `graphql-ruby` handles this hash safely
|
|
|
|
# Also, `graphql-ruby` uses hash-specific methods, for example `size`:
|
|
|
|
# https://sourcegraph.com/github.com/rmosolgo/graphql-ruby@61232b03412df6685406fc46c414e11d3f447817/-/blob/lib/graphql/query.rb?L304
|
|
|
|
ambiguous_param.to_unsafe_h
|
2018-05-21 07:42:07 -04:00
|
|
|
when nil
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
raise Invalid, "Unexpected parameter: #{ambiguous_param}"
|
|
|
|
end
|
|
|
|
rescue JSON::ParserError => e
|
2021-05-04 11:10:36 -04:00
|
|
|
raise Invalid, e
|
2018-05-21 07:42:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|