Sha256: 0aff3e830b6e1f667f1528a2db0f611526973d83d392f55e27302cf3cedaf225

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Api
  module V1
    class GraphqlController < ApplicationController
      include GraphqlDevise::SetUserByToken

      def graphql
        result = DummySchema.execute(params[:query], **execute_params(params))

        render json: result unless performed?
      end

      def interpreter
        render json: InterpreterSchema.execute(params[:query], **execute_params(params))
      end

      def failing_resource_name
        render json: DummySchema.execute(params[:query], context: graphql_context([:user, :fail]))
      end

      def controller_auth
        result = DummySchema.execute(
          params[:query],
          operation_name: params[:operationName],
          variables:      ensure_hash(params[:variables]),
          context:        gql_devise_context(SchemaUser, User)
        )

        render json: result unless performed?
      end

      private

      def execute_params(item)
        {
          operation_name: item[:operationName],
          variables:      ensure_hash(item[:variables]),
          context:        graphql_context([:user, :schema_user])
        }
      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

      def verify_authenticity_token
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql_devise-0.18.2 spec/dummy/app/controllers/api/v1/graphql_controller.rb
graphql_devise-0.18.1 spec/dummy/app/controllers/api/v1/graphql_controller.rb