Sha256: a8355dc693013ac1e99215ceda4504ecdbc2639d76c4d660014abd819e921d22

Contents?: true

Size: 1.86 KB

Versions: 13

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module GraphqlRails
  module Model
    # Executes model method and adds additional meta data if needed
    class CallGraphqlModelMethod
      require 'graphql_rails/concerns/service'

      include ::GraphqlRails::Service

      PAGINATION_KEYS = %i[before after first last].freeze

      def initialize(model:, method_keyword_arguments:, graphql_context:, attribute_config:)
        @model = model
        @method_keyword_arguments = method_keyword_arguments
        @graphql_context = graphql_context
        @attribute_config = attribute_config
      end

      def call
        with_graphql_context do
          run_method
        end
      end

      private

      attr_reader :model, :attribute_config, :graphql_context, :method_keyword_arguments

      def run_method
        if custom_keyword_arguments.empty?
          model.send(method_name)
        else
          formatted_arguments = formatted_method_input(custom_keyword_arguments)
          model.send(method_name, **formatted_arguments)
        end
      end

      def formatted_method_input(keyword_arguments)
        keyword_arguments.transform_values do |input_argument|
          formatted_method_input_argument(input_argument)
        end
      end

      def formatted_method_input_argument(argument)
        return argument.to_h if argument.is_a?(GraphQL::Schema::InputObject)

        argument
      end

      def method_name
        attribute_config.property
      end

      def paginated?
        attribute_config.paginated?
      end

      def custom_keyword_arguments
        return method_keyword_arguments unless paginated?

        method_keyword_arguments.except(*PAGINATION_KEYS)
      end

      def with_graphql_context
        return yield unless model.respond_to?(:with_graphql_context)

        model.with_graphql_context(graphql_context) { yield }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
graphql_rails-3.0.0 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-2.4.0 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-2.3.0 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-2.2.0 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-2.1.0 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-2.0.0 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-1.2.6 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-1.2.4 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-1.2.3 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-1.2.2 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-1.2.1 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-1.2.0 lib/graphql_rails/model/call_graphql_model_method.rb
graphql_rails-1.1.0 lib/graphql_rails/model/call_graphql_model_method.rb