Refactor GraphqlController

This commit is contained in:
Alberto Miedes Garcés
2017-01-25 11:17:08 +01:00
parent 18fb1485eb
commit ef6d089022

View File

@@ -1,5 +1,4 @@
class GraphqlController < ApplicationController
attr_accessor :query_variables, :query_string
skip_before_action :verify_authenticity_token
skip_authorization_check
@@ -8,19 +7,7 @@ class GraphqlController < ApplicationController
def query
begin
# ------------------------------------------------------------------------
api_types_creator = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS)
created_api_types = api_types_creator.create
query_type_creator = GraphQL::QueryTypeCreator.new(created_api_types)
query_type = query_type_creator.create
consul_schema = GraphQL::Schema.define do
query query_type
max_depth 12
end
# ------------------------------------------------------------------------
set_query_environment
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
@@ -32,25 +19,25 @@ class GraphqlController < ApplicationController
private
def set_query_environment
set_query_string
set_query_variables
def consul_schema
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
query_type = GraphQL::QueryTypeCreator.new(api_types).create
GraphQL::Schema.define do
query query_type
max_depth 12
end
end
def set_query_string
def query_string
if request.headers["CONTENT_TYPE"] == 'application/graphql'
@query_string = request.body.string # request.body.class => StringIO
request.body.string # request.body.class => StringIO
else
@query_string = params[:query]
params[:query]
end
if query_string.nil? then raise GraphqlController::QueryStringError end
end
def set_query_variables
if params[:variables].blank?
@query_variables = {}
else
@query_variables = JSON.parse(params[:variables])
end
def query_variables
params[:variables].blank? ? {} : JSON.parse(params[:variables])
end
end