Sha256: 30261e746631c7b1afb24982eea7624f34d37afbd34b9a53ca80ee202803784c

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
module GraphQL
  class Schema
    class Union < GraphQL::Schema::Member
      def initialize(obj, ctx)
        @object = obj
        @context = ctx
      end

      class << self
        def possible_types(*types)
          if types.any?
            @own_possible_types = types
          else
            all_possible_types = own_possible_types
            inherited_possible_types = (superclass < GraphQL::Schema::Union ? superclass.possible_types : [])
            all_possible_types += inherited_possible_types
            all_possible_types.uniq
          end
        end

        def own_possible_types
          @own_possible_types ||= []
        end

        # The class resolves type by:
        # - make an instance
        # - call the instance method
        def resolve_type(value, ctx)
          self.new(value, ctx).resolve_type
        end

        def to_graphql
          type_defn = GraphQL::UnionType.new
          type_defn.name = graphql_name
          type_defn.description = description
          type_defn.possible_types = possible_types
          # If an instance method is defined, use it as a
          # resolve type hook, via the class method
          if method_defined?(:resolve_type)
            type_defn.resolve_type = method(:resolve_type)
          end
          type_defn
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphql-1.8.0.pre6 lib/graphql/schema/union.rb
graphql-1.8.0.pre5 lib/graphql/schema/union.rb
graphql-1.8.0.pre4 lib/graphql/schema/union.rb
graphql-1.8.0.pre3 lib/graphql/schema/union.rb
graphql-1.8.0.pre2 lib/graphql/schema/union.rb
graphql-1.8.0.pre1 lib/graphql/schema/union.rb