Add graphql end point
This commit is contained in:
parent
5a7eb734ff
commit
bf6fbbf2b6
3 changed files with 69 additions and 0 deletions
20
app/controllers/api/v2/base_controller.rb
Normal file
20
app/controllers/api/v2/base_controller.rb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
class API::V2::BaseController < ApplicationController
|
||||||
|
protect_from_forgery with: :null_session
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def context
|
||||||
|
{
|
||||||
|
administrateur_id: current_administrateur&.id,
|
||||||
|
token: authorization_bearer_token
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def authorization_bearer_token
|
||||||
|
received_token = nil
|
||||||
|
authenticate_with_http_token do |token, _options|
|
||||||
|
received_token = token
|
||||||
|
end
|
||||||
|
received_token
|
||||||
|
end
|
||||||
|
end
|
45
app/controllers/api/v2/graphql_controller.rb
Normal file
45
app/controllers/api/v2/graphql_controller.rb
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
class API::V2::GraphqlController < API::V2::BaseController
|
||||||
|
def execute
|
||||||
|
variables = ensure_hash(params[:variables])
|
||||||
|
|
||||||
|
result = Api::V2::Schema.execute(params[:query],
|
||||||
|
variables: variables,
|
||||||
|
context: context,
|
||||||
|
operation_name: params[:operationName])
|
||||||
|
|
||||||
|
render json: result
|
||||||
|
rescue => e
|
||||||
|
if Rails.env.development?
|
||||||
|
handle_error_in_development e
|
||||||
|
else
|
||||||
|
raise e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
def handle_error_in_development(e)
|
||||||
|
logger.error e.message
|
||||||
|
logger.error e.backtrace.join("\n")
|
||||||
|
|
||||||
|
render json: { error: { message: e.message, backtrace: e.backtrace }, data: {} }, status: 500
|
||||||
|
end
|
||||||
|
end
|
|
@ -225,6 +225,10 @@ Rails.application.routes.draw do
|
||||||
resources :dossiers, only: [:index, :show]
|
resources :dossiers, only: [:index, :show]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
namespace :v2 do
|
||||||
|
post :graphql, to: "graphql#execute"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue