Sha256: 14aee546ad9e306bff6999bb0e8a2ad2ddd96d6752f55c1fcb4e79435641e315

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module GraphQL
  # This type exposes fields on an object.
  #
  # @example defining a type for your IMDB clone
  #   MovieType = GraphQL::ObjectType.define do
  #     name "Movie"
  #     description "A full-length film or a short film"
  #     interfaces [ProductionInterface, DurationInterface]
  #
  #     field :runtimeMinutes, !types.Int, property: :runtime_minutes
  #     field :director, PersonType
  #     field :cast, CastType
  #     field :starring, types[PersonType] do
  #       arguments :limit, types.Int
  #       resolve -> (object, args, ctx) {
  #         stars = object.cast.stars
  #         args[:limit] && stars = stars.limit(args[:limit])
  #         stars
  #       }
  #      end
  #   end
  #
  class ObjectType < GraphQL::BaseType
    accepts_definitions :interfaces, field: GraphQL::Define::AssignObjectField
    attr_accessor :name, :description, :interfaces

    # @return [Hash<String, GraphQL::Field>] Map String fieldnames to their {GraphQL::Field} implementations
    attr_accessor :fields

    def initialize
      @fields = {}
      @interfaces = []
    end

    # @param new_interfaces [Array<GraphQL::Interface>] interfaces that this type implements
    def interfaces=(new_interfaces)
      @interfaces = new_interfaces
    end

    def kind
      GraphQL::TypeKinds::OBJECT
    end

    # @return [GraphQL::Field] The field definition for `field_name` (may be inherited from interfaces)
    def get_field(field_name)
      fields[field_name] || interface_fields[field_name]
    end

    # @return [Array<GraphQL::Field>] All fields, including ones inherited from interfaces
    def all_fields
      interface_fields.merge(self.fields).values
    end

    private

    # Create a {name => defn} hash for fields inherited from interfaces
    def interface_fields
      interfaces.reduce({}) do |memo, iface|
        memo.merge!(iface.fields)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-0.13.0 lib/graphql/object_type.rb