app/controllers/graphql_controller.rb
# frozen_string_literal: true class GraphqlController < APIController # If accessing from outside this domain, nullify the session # This allows for outside API access while preventing CSRF attacks, # but you'll have to authenticate your user separately # protect_from_forgery with: :null_session def execute variables = ensure_hash(params[:variables]) query = params[:query] operation_name = params[:operationName] context = { request:, auth_token:, current_user: } result = BlogSchema.execute(query, variables:, context:, operation_name:) render json: result rescue StandardError => e raise e unless Rails.env.development? handle_error_in_development e 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(err) logger.error err.message logger.error err.backtrace.join("\n") render json: { errors: [{ message: err.message, backtrace: err.backtrace }], data: {} }, status: :server_error endend