2018-11-09 18:39:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-21 11:42:07 +00: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-05-01 00:09:59 +00:00
|
|
|
ensure_hash(Gitlab::Json.parse(ambiguous_param))
|
2018-05-21 11:42:07 +00:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
when Hash, ActionController::Parameters
|
|
|
|
ambiguous_param
|
|
|
|
when nil
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
raise Invalid, "Unexpected parameter: #{ambiguous_param}"
|
|
|
|
end
|
|
|
|
rescue JSON::ParserError => e
|
2021-05-04 15:10:36 +00:00
|
|
|
raise Invalid, e
|
2018-05-21 11:42:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|