Files
nairobi/app/controllers/graphql_controller.rb
Alberto Miedes Garcés 6013f84945 Handle multiple kind of requests in GraphQL controller
* Added support for GET requests
* Added support for application/graphql content-type for POST requests
* Handling query string parsing errors in controller
* Wrote specs for all this
2016-12-03 13:36:14 +01:00

29 lines
772 B
Ruby

class GraphqlController < ApplicationController
skip_before_action :verify_authenticity_token
skip_authorization_check
def query
if request.headers["CONTENT_TYPE"] == 'application/graphql'
query_string = request.body.string # request.body.class => StringIO
else
query_string = params[:query]
end
if query_string.nil?
render json: { message: 'Query string not present' }, status: :bad_request
else
begin
response = ConsulSchema.execute(
query_string,
variables: params[:variables] || {}
)
render json: response, status: :ok
rescue GraphQL::ParseError
render json: { message: 'Query string is not valid JSON' }, status: :bad_request
end
end
end
end