Sha256: dd4eca500db4b891893e3ae4e5009cb1695741ad130374f4a84e80433e5111f9

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true
module GraphQL
  class Schema
    class Member
      # Shared code for Object and Interface
      module HasFields
        # Add a field to this object or interface with the given definition
        # @see {GraphQL::Schema::Field#initialize} for method signature
        # @return [void]
        def field(*args, &block)
          field_defn = field_class.new(*args, &block)
          add_field(field_defn)
          nil
        end

        # @return [Array<GraphQL::Schema::Field>] Fields on this object, including inherited fields
        def fields
          all_fields = own_fields
          inherited_fields = (superclass.is_a?(HasFields) ? superclass.fields : [])
          # Remove any inherited fields which were overridden on this class:
          inherited_fields.each do |inherited_f|
            if all_fields.none? {|f| f.name == inherited_f.name}
              all_fields << inherited_f
            end
          end
          all_fields
        end

        # Register this field with the class, overriding a previous one if needed
        # @param field_defn [GraphQL::Schema::Field]
        # @return [void]
        def add_field(field_defn)
          own_fields.reject! {|f| f.name == field_defn.name}
          own_fields << field_defn
          nil
        end

        # @return [Class] The class to initialize when adding fields to this kind of schema member
        def field_class(new_field_class = nil)
          if new_field_class
            @field_class = new_field_class
          else
            @field_class || superclass.field_class
          end
        end

        private

        # @return [Array<GraphQL::Schema::Field>] Fields defined on this class _specifically_, not parent classes
        def own_fields
          @own_fields ||= []
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-1.8.0.pre3 lib/graphql/schema/member/has_fields.rb