class GraphQL::InterfaceType

An Interface contains a collection of types which implement some of the same fields.

Interfaces can have fields, defined with `field`, just like an object type.

Objects which implement this field inherit field definitions from the interface. An object type can override the inherited definition by redefining that field.

@example An interface with three 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

@example Implementing an interface with an object type

Laptoptype = GraphQL::ObjectType.define do
  interfaces [DeviceInterface]
end

Attributes

fields[RW]
orphan_types[RW]
resolve_type_proc[RW]

Public Class Methods

new() click to toggle source
Calls superclass method GraphQL::BaseType.new
# File lib/graphql/interface_type.rb, line 30
def initialize
  super
  @fields = {}
  @orphan_types = []
  @resolve_type_proc = nil
end

Public Instance Methods

all_fields() click to toggle source

These fields don't have instrumenation applied @see [Schema#get_fields] Get fields with instrumentation @return [Array<GraphQL::Field>] All fields on this type

# File lib/graphql/interface_type.rb, line 63
def all_fields
  fields.values
end
get_field(field_name) click to toggle source

@return [GraphQL::Field] The defined field for `field_name`

# File lib/graphql/interface_type.rb, line 56
def get_field(field_name)
  fields[field_name]
end
get_possible_type(type_name, ctx) click to toggle source

Get a possible type of this {InterfaceType} by type name @param type_name [String] @param ctx [GraphQL::Query::Context] The context for the current query @return [GraphQL::ObjectType, nil] The type named `type_name` if it exists and implements this {InterfaceType}, (else `nil`)

# File lib/graphql/interface_type.rb, line 71
def get_possible_type(type_name, ctx)
  type = ctx.query.get_type(type_name)
  type if type && ctx.query.schema.possible_types(self).include?(type)
end
initialize_copy(other) click to toggle source
Calls superclass method GraphQL::BaseType#initialize_copy
# File lib/graphql/interface_type.rb, line 37
def initialize_copy(other)
  super
  @fields = other.fields.dup
  @orphan_types = other.orphan_types.dup
end
kind() click to toggle source
# File lib/graphql/interface_type.rb, line 43
def kind
  GraphQL::TypeKinds::INTERFACE
end
possible_type?(type, ctx) click to toggle source

Check if a type is a possible type of this {InterfaceType} @param type [String, GraphQL::BaseType] Name of the type or a type definition @param ctx [GraphQL::Query::Context] The context for the current query @return [Boolean] True if the `type` exists and is a member of this {InterfaceType}, (else `nil`)

# File lib/graphql/interface_type.rb, line 80
def possible_type?(type, ctx)
  type_name = type.is_a?(String) ? type : type.graphql_name
  !get_possible_type(type_name, ctx).nil?
end
resolve_type(value, ctx) click to toggle source
# File lib/graphql/interface_type.rb, line 47
def resolve_type(value, ctx)
  ctx.query.resolve_type(self, value)
end
resolve_type=(resolve_type_callable) click to toggle source
# File lib/graphql/interface_type.rb, line 51
def resolve_type=(resolve_type_callable)
  @resolve_type_proc = resolve_type_callable
end