Sha256: ea543cdd922621e1e3c52a59395dbe877406b103367a8bbd3a85b3d7dd0a9bdd

Contents?: true

Size: 1.32 KB

Versions: 37

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true
module GraphQL
  class Schema
    # Find the members of a union or interface within a given schema.
    #
    # (Although its members never change, unions are handled this way to simplify execution code.)
    #
    # Internally, the calculation is cached. It's assumed that schema members _don't_ change after creating the schema!
    #
    # @example Get an interface's possible types
    #   possible_types = GraphQL::Schema::PossibleTypes(MySchema)
    #   possible_types.possible_types(MyInterface)
    #   # => [MyObjectType, MyOtherObjectType]
    class PossibleTypes
      def initialize(schema)
        @object_types = schema.types.values.select { |type| type.kind.object? }

        @interface_implementers = Hash.new do |hash, key|
          hash[key] = @object_types.select { |type| type.interfaces.include?(key) }.sort_by(&:name)
        end
      end

      def possible_types(type_defn)
        case type_defn
        when Module
          possible_types(type_defn.graphql_definition)
        when GraphQL::UnionType
          type_defn.possible_types
        when GraphQL::InterfaceType
          @interface_implementers[type_defn]
        when GraphQL::BaseType
          [type_defn]
        else
          raise "Unexpected possible_types object: #{type_defn.inspect}"
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/schema/possible_types.rb
graphql-1.10.0.pre2 lib/graphql/schema/possible_types.rb
graphql-1.9.16 lib/graphql/schema/possible_types.rb
graphql-1.9.15 lib/graphql/schema/possible_types.rb
graphql-1.9.14 lib/graphql/schema/possible_types.rb
graphql-1.10.0.pre1 lib/graphql/schema/possible_types.rb
graphql-1.9.13 lib/graphql/schema/possible_types.rb
graphql-1.9.12 lib/graphql/schema/possible_types.rb
graphql-1.9.11 lib/graphql/schema/possible_types.rb
graphql-1.9.10 lib/graphql/schema/possible_types.rb
graphql-1.9.9 lib/graphql/schema/possible_types.rb
graphql-1.9.8 lib/graphql/schema/possible_types.rb
graphql-1.9.7 lib/graphql/schema/possible_types.rb
graphql-1.9.6 lib/graphql/schema/possible_types.rb
graphql-1.9.5 lib/graphql/schema/possible_types.rb
graphql-1.9.4 lib/graphql/schema/possible_types.rb
graphql-1.9.3 lib/graphql/schema/possible_types.rb
graphql-1.9.2 lib/graphql/schema/possible_types.rb
graphql-1.8.17 lib/graphql/schema/possible_types.rb
graphql-1.8.16 lib/graphql/schema/possible_types.rb