Sha256: 464846c0c1699bca81fb91847a94b6c16075683ef161cce2a0cce42c6b5dab89

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Ibrain
  class GraphqlController < Ibrain::BaseController
    include Devise::Controllers::ScopedViews

    before_action :authenticate_user!, unless: :skip_operations
    before_action :map_user_class_to_request

    helpers = %w(resource scope_name resource_name signed_in_resource
                 resource_class resource_params devise_mapping)
    helper_method(*helpers)

    def execute
      query, variables, operation_name = normalize_entity

      result = schema.execute(
        query,
        variables: variables,
        context: {
          session: session,
          current_user: try_ibrain_current_user,
          controller: self,
          request: request
        },
        operation_name: operation_name
      )

      render_json_ok(result['data'], nil, result['errors'])
    end

    protected

    def normalize_entity
      query = params[:query]
      operation_name = params[:operationName]
      variables = prepare_variables(params[:variables])

      [query, variables, operation_name]
    end

    # 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 # GraphQLRuby will validate name and type of incoming variables.
      when nil
        {}
      else
        raise ArgumentError, "Unexpected parameter: #{variables_param}"
      end
    end

    def schema
      Ibrain::Config.graphql_schema.safe_constantize
    end

    def map_user_class_to_request
      return if request.env['devise.mapping'].present?

      request.env['devise.mapping'] = Ibrain.user_class
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ibrain-core-0.1.8 app/controllers/ibrain/graphql_controller.rb