Sha256: 602e4bf53051950b6fd84eef3f5c5bc5f19ab197f36faec65b2f5dc15a29aa68

Contents?: true

Size: 1.69 KB

Versions: 11

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

# Comment
class AdminGraphqlController < ApplicationController
  include GraphqlDevise::Concerns::SetUserByToken

  attr_reader :current_resource

  # 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 = prepare_variables(params[:variables])
    query = params[:query]
    operation_name = params[:operationName]

    context = gql_devise_context(AdminUser)

    @current_resource = context[:current_resource]

    result = <%= class_name %>AdminSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
    render json: result
  rescue StandardError => e
    raise e unless Rails.env.development?

    handle_error_in_development(e)
  end

  private

  # Handle variables in form data, JSON body, or a blank value
  def prepare_variables(variables_param)
    case variables_param
    when String
      if variables_param.present?
        JSON.parse(variables_param) || {}
      else
        {}
      end
    when Hash
      variables_param
    when ActionController::Parameters
      variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables.
    when nil
      {}
    else
      raise ArgumentError, "Unexpected parameter: #{variables_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: :internal_server_error
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphql_scaffold_fan-0.3.3 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.3.2 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.3.1 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.3.0 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.2.9 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.2.8 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.2.7 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.2.6 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.2.5 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.2.4 lib/generators/templates/admin_controller.haml
graphql_scaffold_fan-0.2.3 lib/generators/templates/admin_controller.haml