Class: GraphQL::InterfaceType

Inherits:
ObjectType show all
Defined in:
lib/graphql/interface_type.rb

Overview

A collection of types which implement the same fields

Examples:

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

Constant Summary collapse

DEFAULT_RESOLVE_TYPE =

The default implementation of #resolve_type gets `object.class.name` and finds a type with the same name

-> (object) {
  type_name = object.class.name
  possible_types.find {|t| t.name == type_name}
}

Instance Attribute Summary

Attributes inherited from ObjectType

#description, #fields, #interfaces, #name

Instance Method Summary collapse

Methods inherited from ObjectType

#==, #to_s

Methods included from DefinitionHelpers::DefinedByConfig

included

Methods included from DefinitionHelpers::NonNullWithBang

#!

Instance Method Details

#kindObject


23
24
25
# File 'lib/graphql/interface_type.rb', line 23

def kind
  GraphQL::TypeKinds::INTERFACE
end

#possible_typesArray<GraphQL::ObjectType>

Returns Types which declare that they implement this interface.

Returns:


28
29
30
# File 'lib/graphql/interface_type.rb', line 28

def possible_types
  @possible_types ||= []
end

#resolve_type(object) ⇒ GraphQL::ObjectType

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!

Parameters:

  • object (Object)

    the object which needs a type to expose it

Returns:


38
39
40
# File 'lib/graphql/interface_type.rb', line 38

def resolve_type(object)
  instance_exec(object, &@resolve_type_proc)
end

#resolve_type=(new_proc) ⇒ Object


42
43
44
# File 'lib/graphql/interface_type.rb', line 42

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