Refactor GraphqlController
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
class GraphqlController < ApplicationController
|
class GraphqlController < ApplicationController
|
||||||
attr_accessor :query_variables, :query_string
|
|
||||||
|
|
||||||
skip_before_action :verify_authenticity_token
|
skip_before_action :verify_authenticity_token
|
||||||
skip_authorization_check
|
skip_authorization_check
|
||||||
@@ -8,19 +7,7 @@ class GraphqlController < ApplicationController
|
|||||||
|
|
||||||
def query
|
def query
|
||||||
begin
|
begin
|
||||||
# ------------------------------------------------------------------------
|
if query_string.nil? then raise GraphqlController::QueryStringError end
|
||||||
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
|
|
||||||
response = consul_schema.execute query_string, variables: query_variables
|
response = consul_schema.execute query_string, variables: query_variables
|
||||||
render json: response, status: :ok
|
render json: response, status: :ok
|
||||||
rescue GraphqlController::QueryStringError
|
rescue GraphqlController::QueryStringError
|
||||||
@@ -32,25 +19,25 @@ class GraphqlController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_query_environment
|
def consul_schema
|
||||||
set_query_string
|
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
|
||||||
set_query_variables
|
query_type = GraphQL::QueryTypeCreator.new(api_types).create
|
||||||
|
|
||||||
|
GraphQL::Schema.define do
|
||||||
|
query query_type
|
||||||
|
max_depth 12
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_query_string
|
def query_string
|
||||||
if request.headers["CONTENT_TYPE"] == 'application/graphql'
|
if request.headers["CONTENT_TYPE"] == 'application/graphql'
|
||||||
@query_string = request.body.string # request.body.class => StringIO
|
request.body.string # request.body.class => StringIO
|
||||||
else
|
else
|
||||||
@query_string = params[:query]
|
params[:query]
|
||||||
end
|
end
|
||||||
if query_string.nil? then raise GraphqlController::QueryStringError end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_query_variables
|
def query_variables
|
||||||
if params[:variables].blank?
|
params[:variables].blank? ? {} : JSON.parse(params[:variables])
|
||||||
@query_variables = {}
|
|
||||||
else
|
|
||||||
@query_variables = JSON.parse(params[:variables])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user