Files
grecia/app/controllers/graphql_controller.rb
Javi Martín d0d681a44b Add and apply EmptyLineAfterGuardClause rule
We were inconsistent on this one. I consider it particularly useful when
a method starts with a `return` statement.

In other cases, we probably shouldn't have a guard rule in the middle of
a method in any case, but that's a different refactoring.
2019-10-24 17:56:03 +02:00

58 lines
1.5 KiB
Ruby

class GraphqlController < ApplicationController
include FeatureFlags
feature_flag :graphql_api
skip_before_action :verify_authenticity_token
skip_authorization_check
class QueryStringError < StandardError
end
def query
begin
if query_string.nil? then raise GraphqlController::QueryStringError end
response = consul_schema.execute query_string, variables: query_variables
render json: response, status: :ok
rescue GraphqlController::QueryStringError
render json: { message: "Query string not present" }, status: :bad_request
rescue JSON::ParserError
render json: { message: "Error parsing JSON" }, status: :bad_request
rescue GraphQL::ParseError
render json: { message: "Query string is not valid JSON" }, status: :bad_request
rescue
unless Rails.env.production? then raise end
end
end
private
def consul_schema
api_types = GraphQL::ApiTypesCreator.create(API_TYPE_DEFINITIONS)
query_type = GraphQL::QueryTypeCreator.create(api_types)
GraphQL::Schema.define do
query query_type
max_depth 8
max_complexity 2500
end
end
def query_string
if request.headers["CONTENT_TYPE"] == "application/graphql"
request.body.string # request.body.class => StringIO
else
params[:query]
end
end
def query_variables
if params[:variables].blank? || params[:variables] == "null"
{}
else
JSON.parse(params[:variables])
end
end
end