Sha256: 7a617b69a2f1ecb206217df5ec9984695beed052fd089897f14744ecfb0d7160

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/string/filters'
require 'graphql_rails/attribute'

module GraphqlRails
  class Controller
    # stores all graphql_rails contoller specific config
    class ActionConfiguration
      attr_reader :attributes, :return_type, :pagination_options

      def initialize
        @attributes = {}
        @can_return_nil = false
      end

      def permit(*no_type_attributes, **typed_attributes)
        no_type_attributes.each { |attribute| permit_attribute(attribute) }
        typed_attributes.each { |attribute, type| permit_attribute(attribute, type) }
        self
      end

      def paginated(pagination_options = {})
        @pagination_options = pagination_options
        permit(:before, :after, first: :int, last: :int)
      end

      def description(new_description = nil)
        if new_description
          @description = new_description
          self
        else
          @description
        end
      end

      def can_return_nil
        @can_return_nil = true
        self
      end

      def returns(new_return_type)
        @return_type = new_return_type
        self
      end

      def can_return_nil?
        @can_return_nil
      end

      def paginated?
        !!pagination_options # rubocop:disable Style/DoubleNegation
      end

      private

      def permit_attribute(name, type = nil)
        field_name = name.to_s.remove(/!\Z/)
        attributes[field_name] = Attribute.new(name.to_s, type)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
graphql_rails-0.3.3 lib/graphql_rails/controller/action_configuration.rb
graphql_rails-0.3.2 lib/graphql_rails/controller/action_configuration.rb
graphql_rails-0.3.1 lib/graphql_rails/controller/action_configuration.rb
graphql_rails-0.3.0 lib/graphql_rails/controller/action_configuration.rb