Sha256: dca0a1ce22e5622b086968a38b6af65d5853953f30d33dedf581a0d4a8722561

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

# A collection of types which implement the same fields
#
# @example An interface with three required fields
#   DeviceInterface = GraphQL::InterfaceType.define do
#     name("Device")
#     description("Hardware devices for computing")
#
#     field :ram, types.String
#     field :processor, ProcessorType
#     field :release_year, types.Int
#   end
#
class GraphQL::InterfaceType < GraphQL::ObjectType
  defined_by_config :name, :description, :fields, :resolve_type

  # The default implementation of {#resolve_type} gets `object.class.name`
  # and finds a type with the same name
  DEFAULT_RESOLVE_TYPE = -> (object) {
    type_name = object.class.name
    possible_types.find {|t| t.name == type_name}
  }

  def kind
    GraphQL::TypeKinds::INTERFACE
  end

  # @return [Array<GraphQL::ObjectType>] Types which declare that they implement this interface
  def possible_types
    @possible_types ||= []
  end

  # Return the implementing type for `object`.
  # The default implementation assumes that there's a type with the same name as `object.class.name`.
  # Maybe you'll need to override this in your own interfaces!
  #
  # @param object [Object] the object which needs a type to expose it
  # @return [GraphQL::ObjectType] the type which should expose `object`
  def resolve_type(object)
    instance_exec(object, &@resolve_type_proc)
  end

  def resolve_type=(new_proc)
    @resolve_type_proc = new_proc || DEFAULT_RESOLVE_TYPE
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphql-0.7.1 lib/graphql/interface_type.rb
graphql-0.7.0 lib/graphql/interface_type.rb
graphql-0.6.2 lib/graphql/interface_type.rb
graphql-0.6.1 lib/graphql/interface_type.rb
graphql-0.6.0 lib/graphql/interface_type.rb