Class: GraphQL::ObjectType

Inherits:
BaseType show all
Defined in:
lib/graphql/object_type.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
    arguments :limit, types.Int
    resolve -> (object, args, ctx) {
      stars = object.cast.stars
      args[:limit] && stars = stars.limit(args[:limit])
      stars
    }
   end
end

Defined Under Namespace

Classes: UnresolvedTypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #validate_input

Methods included from Define::InstanceDefinable

included

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeObjectType

Returns a new instance of ObjectType.



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

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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



25
26
27
# File 'lib/graphql/object_type.rb', line 25

def description
  @description
end

#fieldsHash<String, GraphQL::Field>

Returns Map String fieldnames to their Field implementations.

Returns:



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

def fields
  @fields
end

#interfacesObject

Returns the value of attribute interfaces.



25
26
27
# File 'lib/graphql/object_type.rb', line 25

def interfaces
  @interfaces
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/graphql/object_type.rb', line 25

def name
  @name
end

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



50
51
52
# File 'lib/graphql/object_type.rb', line 50

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)



45
46
47
# File 'lib/graphql/object_type.rb', line 45

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

#kindObject



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

def kind
  GraphQL::TypeKinds::OBJECT
end