Sha256: d9f4042fc5e7630849bf6834928509bda916e034c39612051bda0447add29bff

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'graphql_rails/attributes/type_parser'
require 'graphql_rails/attributes/attribute_name_parser'

module GraphqlRails
  module Attributes
    # contains methods which are shared between various attribute-like classes
    # expects `initial_name` and `type` to be defined
    module Attributable
      def field_name
        attribute_name_parser.field_name
      end

      def type_name
        type.to_s
      end

      def name
        attribute_name_parser.name
      end

      def required?
        return @required unless @required.nil?

        (type.nil? && attribute_name_parser.required?) ||
          type.to_s[/!$/].present? ||
          type.is_a?(GraphQL::Schema::NonNull)
      end

      def graphql_model
        type_parser.graphql_model
      end

      def optional?
        !required?
      end

      def scalar_type?
        type_parser.raw_graphql_type? || type_parser.core_scalar_type?
      end

      private

      def type_parser
        @type_parser ||= begin
          type_for_parser = type || attribute_name_parser.graphql_type
          TypeParser.new(type_for_parser, paginated: paginated?)
        end
      end

      def attribute_name_parser
        @attribute_name_parser ||= AttributeNameParser.new(initial_name, options: options)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphql_rails-2.4.0 lib/graphql_rails/attributes/attributable.rb
graphql_rails-2.3.0 lib/graphql_rails/attributes/attributable.rb
graphql_rails-2.2.0 lib/graphql_rails/attributes/attributable.rb
graphql_rails-2.1.0 lib/graphql_rails/attributes/attributable.rb
graphql_rails-2.0.0 lib/graphql_rails/attributes/attributable.rb