Sha256: 0b55744c6f49fe92cf090deb358c50b019f72714f78b86da6ffd7ebd933eb2e5

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require_dependency 'graphql_devise/application_controller'

module GraphqlDevise
  class GraphqlController < ApplicationController
    include GraphqlDevise::Concerns::SetUserByToken

    def auth
      result = if params[:_json]
        GraphqlDevise::Schema.multiplex(
          params[:_json].map do |param|
            { query: param[:query] }.merge(execute_params(param))
          end
        )
      else
        GraphqlDevise::Schema.execute(params[:query], execute_params(params))
      end

      render json: result unless performed?
    end

    attr_accessor :client_id, :token, :resource

    private

    def execute_params(item)
      {
        operation_name: item[:operationName],
        variables:      ensure_hash(item[:variables]),
        context:        { controller: self }
      }
    end

    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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
graphql_devise-0.14.2 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.14.1 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.14.0 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.13.6 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.13.5 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.13.4 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.13.3 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.13.2 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.13.1 app/controllers/graphql_devise/graphql_controller.rb
graphql_devise-0.13.0 app/controllers/graphql_devise/graphql_controller.rb