Sha256: 112f59a5eaacd87c7a8c6c490c20037cd519d93281f0e51eef883497be0797d7

Contents?: true

Size: 1.13 KB

Versions: 35

Compression:

Stored size: 1.13 KB

Contents

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? }

        @storage = 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 GraphQL::UnionType
          type_defn.possible_types
        when GraphQL::InterfaceType
          @storage[type_defn]
        else
          raise "#{type_defn} doesn't have possible types"
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
graphql-1.0.0 lib/graphql/schema/possible_types.rb
graphql-0.19.4 lib/graphql/schema/possible_types.rb
graphql-0.19.3 lib/graphql/schema/possible_types.rb
graphql-0.19.2 lib/graphql/schema/possible_types.rb
graphql-0.19.1 lib/graphql/schema/possible_types.rb
graphql-0.19.0 lib/graphql/schema/possible_types.rb
graphql-0.18.15 lib/graphql/schema/possible_types.rb
graphql-0.18.14 lib/graphql/schema/possible_types.rb
graphql-0.18.13 lib/graphql/schema/possible_types.rb
graphql-0.18.12 lib/graphql/schema/possible_types.rb
graphql-0.18.11 lib/graphql/schema/possible_types.rb
graphql-0.18.10 lib/graphql/schema/possible_types.rb
graphql-0.18.9 lib/graphql/schema/possible_types.rb
graphql-0.18.8 lib/graphql/schema/possible_types.rb
graphql-0.18.7 lib/graphql/schema/possible_types.rb
graphql-0.18.6 lib/graphql/schema/possible_types.rb
graphql-0.18.5 lib/graphql/schema/possible_types.rb
graphql-0.18.4 lib/graphql/schema/possible_types.rb
graphql-0.18.3 lib/graphql/schema/possible_types.rb
graphql-0.18.2 lib/graphql/schema/possible_types.rb