Sha256: 3903b40be9dfe45aaff9f3970559f4774dcc1e8659b6d2f6041ea3d3f038ef78

Contents?: true

Size: 1.14 KB

Versions: 33

Compression:

Stored size: 1.14 KB

Contents

class GraphqlController < ApplicationController
  def execute
    variables = ensure_hash(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 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

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.10.0.pre1 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.13 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.12 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.11 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.10 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.9 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.8 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.7 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.6 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.5 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.4 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.3 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.2 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.8.17 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.8.16 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.1 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.0 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.8.15 lib/generators/graphql/templates/graphql_controller.erb
graphql-1.9.0.pre4 lib/generators/graphql/templates/graphql_controller.erb