Sha256: 8cac4c4b23bb9e498c6590574864443f0505cb1ad44f4e8be8c39903fff7066a

Contents?: true

Size: 1.56 KB

Versions: 18

Compression:

Stored size: 1.56 KB

Contents

<% module_namespacing_when_supported do -%>
class GraphqlController < ApplicationController
  # 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 = {
      # Query context goes here, for example:
      # current_user: current_user,
    }
    result = <%= schema_name %>.execute(query, variables: variables, context: context, operation_name: operation_name)
    render json: result
  rescue => 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(e)
    logger.error e.message
    logger.error e.backtrace.join("\n")

    render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500
  end
end
<% end -%>

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
graphql-1.11.12 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.11 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.10 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.9 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.8 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.7 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.6 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.5 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.8 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.4 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.3 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.2 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.1 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.12.0 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.7 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.6 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.5 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.11.4 lib/generators/graphql/templates/graphql_controller.erb