Class: GraphQL::ObjectType

Inherits:
BaseType show all
Defined in:
lib/graphql/object_type.rb,
lib/graphql/unresolved_type_error.rb

Overview

This type exposes fields on an object.

Examples:

defining a type for your IMDB clone

MovieType = GraphQL::ObjectType.define do
  name "Movie"
  description "A full-length film or a short film"
  interfaces [ProductionInterface, DurationInterface]

  field :runtimeMinutes, !types.Int, property: :runtime_minutes
  field :director, PersonType
  field :cast, CastType
  field :starring, types[PersonType] do
    argument :limit, types.Int
    resolve -> (object, args, ctx) {
      stars = object.cast.stars
      args[:limit] && stars = stars.limit(args[:limit])
      stars
    }
   end
end

Direct Known Subclasses

Relay::Edge

Constant Summary collapse

UnresolvedTypeError =
Deprecated.

Use UnresolvedTypeError instead

GraphQL::UnresolvedTypeError

Instance Attribute Summary collapse

Attributes inherited from BaseType

#description, #name

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #connection_type, #define_connection, #define_edge, #edge_type, resolve_related_type, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #validate_input

Methods included from Define::InstanceDefinable

#define, #definition_proc=, included, #metadata

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeObjectType

Returns a new instance of ObjectType.



35
36
37
38
# File 'lib/graphql/object_type.rb', line 35

def initialize
  @fields = {}
  @dirty_interfaces = []
end

Instance Attribute Details

#fieldsHash<String => GraphQL::Field>

Returns Map String fieldnames to their Field implementations.

Returns:



# File 'lib/graphql/object_type.rb', line 28

#mutationGraphQL::Relay::Mutation?

Returns The mutation this field was derived from, if it was derived from a mutation.

Returns:



# File 'lib/graphql/object_type.rb', line 31

Instance Method Details

#all_fieldsArray<GraphQL::Field>

Returns All fields, including ones inherited from interfaces.

Returns:

  • (Array<GraphQL::Field>)

    All fields, including ones inherited from interfaces



65
66
67
# File 'lib/graphql/object_type.rb', line 65

def all_fields
  interface_fields.merge(self.fields).values
end

#get_field(field_name) ⇒ GraphQL::Field

Returns The field definition for ‘field_name` (may be inherited from interfaces).

Returns:

  • (GraphQL::Field)

    The field definition for ‘field_name` (may be inherited from interfaces)



60
61
62
# File 'lib/graphql/object_type.rb', line 60

def get_field(field_name)
  fields[field_name] || interface_fields[field_name]
end

#interfacesObject



46
47
48
49
50
51
52
53
# File 'lib/graphql/object_type.rb', line 46

def interfaces
  @clean_interfaces ||= begin
    ensure_defined
    @dirty_interfaces.map { |i_type| GraphQL::BaseType.resolve_related_type(i_type) }
  rescue
    @dirty_interfaces
  end
end

#interfaces=(new_interfaces) ⇒ Object

Parameters:

  • new_interfaces (Array<GraphQL::Interface>)

    interfaces that this type implements



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

def interfaces=(new_interfaces)
  @clean_interfaces = nil
  @dirty_interfaces = new_interfaces
end

#kindObject



55
56
57
# File 'lib/graphql/object_type.rb', line 55

def kind
  GraphQL::TypeKinds::OBJECT
end