Sha256: 88a29e6fed83b740cb326bf348f2b3ac7299d5d34a0dadbe43b43e100f082907

Contents?: true

Size: 2 KB

Versions: 6

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

module Rails
  module GraphQL
    class Type
      # The introspection object for a field on objects and interfaces
      class Object::FieldObject < Object
        self.assigned_to = 'Rails::GraphQL::Field'
        self.spec_object = true

        delegate :fake_type_object, to: 'Object::TypeObject'

        rename! '__Field'

        desc <<~DESC
          Fields are the elements that compose both Objects and Interfaces. Each
          field in these other objects may contain arguments and always yields
          a value of a specific type.
        DESC

        field :name,               :string,        null: false, method_name: :gql_name
        field :description,        :string
        field :args,               '__InputValue', full: true
        field :type,               '__Type',       null: false, method_name: :build_type
        field :is_deprecated,      :boolean,       null: false, method_name: :deprecated?
        field :deprecation_reason, :string

        def description
          if current.method(:description).arity == 0
            current.description
          else
            current.description(schema.namespace)
          end
        end

        def build_type
          result = current.type_klass

          if current.array?
            result = fake_type_object(:non_null, result) unless current.nullable?
            result = fake_type_object(:list,     result)
          end

          result = fake_type_object(:non_null, result) unless current.null?
          result
        end

        def args
          all_arguments&.values || EMPTY_ARRAY
        end

        def deprecated?
          !deprecated_instance.nil?
        end

        def deprecation_reason
          deprecated_instance&.args&.reason
        end

        private

          def deprecated_instance
            current.all_directives&.reverse_each do |item|
              return item if item.class <= deprecated_directive
            end

            nil
          end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-graphql-1.0.2 lib/rails/graphql/type/object/field_object.rb
rails-graphql-1.0.1 lib/rails/graphql/type/object/field_object.rb
rails-graphql-1.0.0 lib/rails/graphql/type/object/field_object.rb
rails-graphql-1.0.0.rc2 lib/rails/graphql/type/object/field_object.rb
rails-graphql-1.0.0.rc1 lib/rails/graphql/type/object/field_object.rb
rails-graphql-1.0.0.beta lib/rails/graphql/type/object/field_object.rb