Sha256: 0729644a1fe02b5c0a8e83441cdfdb624dce540089776ff706f53bd56a7ab2fb

Contents?: true

Size: 1.93 KB

Versions: 25

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true
module GraphQL
  # A Union is is a collection of object types which may appear in the same place.
  #
  # The members of a union are declared with `possible_types`.
  #
  # @example A union of object types
  #   MediaUnion = GraphQL::UnionType.define do
  #     name "Media"
  #     description "Media objects which you can enjoy"
  #     possible_types [AudioType, ImageType, VideoType]
  #   end
  #
  # A union itself has no fields; only its members have fields.
  # So, when you query, you must use fragment spreads to access fields.
  #
  # @example Querying for fields on union members
  #  {
  #    searchMedia(name: "Jens Lekman") {
  #      ... on Audio { name, duration }
  #      ... on Image { name, height, width }
  #      ... on Video { name, length, quality }
  #    }
  #  }
  #
  class UnionType < GraphQL::BaseType
    accepts_definitions :possible_types
    ensure_defined :possible_types

    def initialize
      super
      @dirty_possible_types = []
      @clean_possible_types = nil
    end

    def initialize_copy(other)
      super
      @clean_possible_types = nil
      @dirty_possible_types = other.dirty_possible_types.dup
    end

    def kind
      GraphQL::TypeKinds::UNION
    end

    # @return [Boolean] True if `child_type_defn` is a member of this {UnionType}
    def include?(child_type_defn)
      possible_types.include?(child_type_defn)
    end

    def possible_types=(new_possible_types)
      @clean_possible_types = nil
      @dirty_possible_types = new_possible_types
    end

    # @return [Array<GraphQL::ObjectType>] Types which may be found in this union
    def possible_types
      @clean_possible_types ||= begin
        if @dirty_possible_types.respond_to?(:map)
          @dirty_possible_types.map { |type| GraphQL::BaseType.resolve_related_type(type) }
        else
          @dirty_possible_types
        end
      end
    end

    protected

    attr_reader :dirty_possible_types
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
graphql-1.6.4 lib/graphql/union_type.rb
graphql-1.5.15 lib/graphql/union_type.rb
graphql-1.6.3 lib/graphql/union_type.rb
graphql-1.6.2 lib/graphql/union_type.rb
graphql-1.6.1 lib/graphql/union_type.rb
graphql-1.6.0 lib/graphql/union_type.rb
graphql-1.5.14 lib/graphql/union_type.rb
graphql-1.5.13 lib/graphql/union_type.rb
graphql-1.5.7.1 lib/graphql/union_type.rb
graphql-1.5.12 lib/graphql/union_type.rb
graphql-1.5.11 lib/graphql/union_type.rb
graphql-1.5.10 lib/graphql/union_type.rb
graphql-1.5.9 lib/graphql/union_type.rb
graphql-1.5.8 lib/graphql/union_type.rb
graphql-1.5.7 lib/graphql/union_type.rb
graphql-1.5.6 lib/graphql/union_type.rb
graphql-1.5.5 lib/graphql/union_type.rb
graphql-1.5.4 lib/graphql/union_type.rb
graphql-1.5.3 lib/graphql/union_type.rb
graphql-1.4.5 lib/graphql/union_type.rb