Sha256: 138f9f6c6cf3fb13e9ac3c915c5c37802f2d14f9cb226490c41f26264cb4ee63

Contents?: true

Size: 1.26 KB

Versions: 10

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'graphql_rails/controller/action'
require_relative 'request'
require_relative 'action'

module GraphqlRails
  class Controller
    # graphql resolver which redirects actions to appropriate controller and controller action
    class ControllerFunction < GraphQL::Function
      # accepts path of given format "controller_name#action"
      attr_reader :type

      def initialize(controller:, action_name:, type:)
        @controller = controller
        @action_name = action_name
        @type = type
      end

      def self.from_route(route)
        action = Action.new(route)

        action_function_class(action).new(
          controller: action.controller,
          action_name: action.name,
          type: action.return_type
        )
      end

      def self.action_function_class(action)
        Class.new(self) do
          description(action.description)

          action.arguments.each do |attribute|
            argument(attribute.field_name, attribute.graphql_field_type)
          end
        end
      end

      def call(object, inputs, ctx)
        request = Request.new(object, inputs, ctx)
        controller.new(request).call(action_name)
      end

      private

      attr_reader :controller, :action_name
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
graphql_rails-0.4.3 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.4.2 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.4.1 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.4.0 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.3.3 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.3.2 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.3.1 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.3.0 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.2.4 lib/graphql_rails/controller/controller_function.rb
graphql_rails-0.2.3 lib/graphql_rails/controller/controller_function.rb