Sha256: 4b31bcfc8359048be0be85c9f421ca5bb74ef46a3b7bda0ffe33c2527a490a92

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

class GraphqlController < ApplicationController
  def execute
    result = Schema.execute(params[:query],
                            variables: ensure_hash(params[:variables]),
                            context: {},
                            operation_name: params[:operationName])
    render json: result
  rescue StandardError => error
    raise error unless Rails.env.development?

    handle_error_in_development error
  end

  private

  def ensure_hash(ambiguous_param)
    case ambiguous_param
    when String then ambiguous_param.present? ? ensure_hash(JSON.parse(ambiguous_param)) : {}
    when Hash, ActionController::Parameters then ambiguous_param
    when nil then {}
    else raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
    end
  end

  def handle_error_in_development(error)
    logger.error error.message
    logger.error error.backtrace.join("\n")

    render json: { error: { message: error.message, backtrace: error.backtrace }, data: {} }, status: 500
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
search_object_graphql-0.2 example/app/controllers/graphql_controller.rb