2018-11-19 21:18:17 +01:00
|
|
|
class API::V2::GraphqlController < API::V2::BaseController
|
2021-11-11 10:31:01 +01:00
|
|
|
include GraphqlOperationLogConcern
|
|
|
|
|
2018-11-19 21:18:17 +01:00
|
|
|
def execute
|
2022-10-31 18:21:24 +01:00
|
|
|
result = API::V2::Schema.execute(query,
|
2018-11-19 21:18:17 +01:00
|
|
|
variables: variables,
|
|
|
|
context: context,
|
|
|
|
operation_name: params[:operationName])
|
|
|
|
|
|
|
|
render json: result
|
2022-10-29 12:35:52 +02:00
|
|
|
rescue GraphQL::ParseError, JSON::ParserError => exception
|
2021-11-19 15:15:10 +01:00
|
|
|
handle_parse_error(exception)
|
2020-12-18 12:03:55 +01:00
|
|
|
rescue => exception
|
2021-02-10 09:54:37 +01:00
|
|
|
if Rails.env.production?
|
2020-12-18 12:03:55 +01:00
|
|
|
handle_error_in_production(exception)
|
2021-02-10 09:54:37 +01:00
|
|
|
else
|
|
|
|
handle_error_in_development(exception)
|
2018-11-19 21:18:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-28 22:05:11 +02:00
|
|
|
def append_info_to_payload(payload)
|
|
|
|
super
|
|
|
|
|
|
|
|
payload.merge!({
|
2022-10-31 18:21:24 +01:00
|
|
|
graphql_operation: operation_log(query(fallback: ''), params[:operationName], to_unsafe_hash(params[:variables]))
|
2021-10-28 22:05:11 +02:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2020-09-30 11:59:49 +02:00
|
|
|
def process_action(*args)
|
|
|
|
super
|
|
|
|
rescue ActionDispatch::Http::Parameters::ParseError => exception
|
2020-12-18 12:03:55 +01:00
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{ message: exception.cause.message }
|
|
|
|
],
|
|
|
|
data: nil
|
|
|
|
}, status: 400
|
2020-09-30 11:59:49 +02:00
|
|
|
end
|
|
|
|
|
2022-10-31 18:21:24 +01:00
|
|
|
def query(fallback: nil)
|
|
|
|
if params[:queryId].present?
|
|
|
|
API::V2::StoredQuery.get(params[:queryId], fallback: fallback)
|
|
|
|
else
|
|
|
|
params[:query]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def variables
|
|
|
|
ensure_hash(params[:variables])
|
|
|
|
end
|
|
|
|
|
2018-11-19 21:18:17 +01:00
|
|
|
# Handle form data, JSON body, or a blank value
|
|
|
|
def ensure_hash(ambiguous_param)
|
|
|
|
case ambiguous_param
|
|
|
|
when String
|
|
|
|
if ambiguous_param.present?
|
|
|
|
ensure_hash(JSON.parse(ambiguous_param))
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
when Hash, ActionController::Parameters
|
|
|
|
ambiguous_param
|
|
|
|
when nil
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-29 12:35:52 +02:00
|
|
|
def to_unsafe_hash(ambiguous_param)
|
|
|
|
case ambiguous_param
|
|
|
|
when String
|
|
|
|
if ambiguous_param.present?
|
|
|
|
JSON.parse(ambiguous_param)
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
when ActionController::Parameters
|
|
|
|
ambiguous_param.to_unsafe_h
|
|
|
|
else
|
|
|
|
ambiguous_param
|
|
|
|
end
|
|
|
|
rescue JSON::ParserError
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2021-11-19 15:15:10 +01:00
|
|
|
def handle_parse_error(exception)
|
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{ message: exception.message }
|
|
|
|
],
|
|
|
|
data: nil
|
|
|
|
}, status: 400
|
|
|
|
end
|
|
|
|
|
2020-12-18 12:03:55 +01:00
|
|
|
def handle_error_in_development(exception)
|
|
|
|
logger.error exception.message
|
|
|
|
logger.error exception.backtrace.join("\n")
|
2018-11-19 21:18:17 +01:00
|
|
|
|
2020-12-18 12:03:55 +01:00
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{ message: exception.message, backtrace: exception.backtrace }
|
|
|
|
],
|
|
|
|
data: nil
|
|
|
|
}, status: 500
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_error_in_production(exception)
|
|
|
|
id = SecureRandom.uuid
|
2022-10-31 18:21:24 +01:00
|
|
|
Sentry.capture_exception(exception, extra: { exception_id: id })
|
2020-12-18 12:03:55 +01:00
|
|
|
|
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: "Internal Server Error",
|
|
|
|
extensions: {
|
|
|
|
exception: { id: id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
data: nil
|
|
|
|
}, status: 500
|
2018-11-19 21:18:17 +01:00
|
|
|
end
|
|
|
|
end
|