Documentation

ObjectType extends TypeWithFields
in package
implements OutputType, CompositeType, NullableType, NamedType, ImplementingType

Object Type Definition

Almost all of the GraphQL types you define will be object types. Object types have a name, but most importantly describe their fields.

Example:

$AddressType = new ObjectType([
  'name' => 'Address',
  'fields' => [
    'street' => [ 'type' => GraphQL\Type\Definition\Type::string() ],
    'number' => [ 'type' => GraphQL\Type\Definition\Type::int() ],
    'formatted' => [
      'type' => GraphQL\Type\Definition\Type::string(),
      'resolve' => function($obj) {
        return $obj->number . ' ' . $obj->street;
      }
    ]
  ]
]);

When two types need to refer to each other, or a type needs to refer to itself in a field, you can use a function expression (aka a closure or a thunk) to supply the fields lazily.

Example:

$PersonType = null;
$PersonType = new ObjectType([
  'name' => 'Person',
  'fields' => function() use (&$PersonType) {
     return [
         'name' => ['type' => GraphQL\Type\Definition\Type::string() ],
         'bestFriend' => [ 'type' => $PersonType ],
     ];
   }
]);

Interfaces, Classes, Traits and Enums

OutputType
CompositeType
NullableType
NamedType
export type NamedType = | ScalarType | ObjectType | InterfaceType | UnionType | EnumType | InputObjectType;
ImplementingType
export type GraphQLImplementingType = GraphQLObjectType | GraphQLInterfaceType;

Table of Contents

BOOLEAN  = 'Boolean'
FLOAT  = 'Float'
ID  = 'ID'
INT  = 'Int'
STRING  = 'String'
$astNode  : ObjectTypeDefinitionNode|null
$config  : array<string|int, mixed>
$description  : string|null
$extensionASTNodes  : array<string|int, ObjectTypeExtensionNode>
$name  : string
$resolveFieldFn  : callable|null
$standardTypes  : array<string, ScalarType>
$builtInTypes  : array<string|int, Type>
$fields  : array<string, FieldDefinition>
Lazily initialized.
$interfaceMap  : array<string, InterfaceType>
Lazily initialized.
$interfaces  : array<int, InterfaceType>
Lazily initialized.
__construct()  : mixed
__toString()  : string
assertObjectType()  : $this
assertType()  : Type
assertValid()  : void
Validates type config and throws if one of type options is invalid.
boolean()  : ScalarType
findField()  : FieldDefinition|null
float()  : ScalarType
getAllBuiltInTypes()  : array<string|int, Type>
Returns all builtin in types including base scalar and introspection types
getField()  : FieldDefinition
getFieldNames()  : array<int, string>
getFields()  : array<string, FieldDefinition>
getInterfaces()  : array<int, InterfaceType>
getInternalTypes()  : array<string|int, Type>
getNamedType()  : Type|null
getNullableType()  : Type
getStandardTypes()  : array<string|int, ScalarType>
Returns all builtin scalar types
hasField()  : bool
id()  : ScalarType
implementsInterface()  : bool
int()  : ScalarType
isAbstractType()  : bool
isBuiltInType()  : bool
Checks if the type is a builtin type
isCompositeType()  : bool
isInputType()  : bool
isLeafType()  : bool
isOutputType()  : bool
isTypeOf()  : bool|Deferred|null
jsonSerialize()  : string
listOf()  : ListOfType
nonNull()  : NonNull
overrideStandardTypes()  : mixed
string()  : ScalarType
toString()  : string
tryInferName()  : string|null
initializeFields()  : void

Constants

BOOLEAN

public mixed BOOLEAN = 'Boolean'

FLOAT

public mixed FLOAT = 'Float'

ID

public mixed ID = 'ID'

INT

public mixed INT = 'Int'

STRING

public mixed STRING = 'String'

Properties

$config

public array<string|int, mixed> $config

$description

public string|null $description

$name

public string $name

$resolveFieldFn

public callable|null $resolveFieldFn

$builtInTypes

private static array<string|int, Type> $builtInTypes

$fields

Lazily initialized.

private array<string, FieldDefinition> $fields

Methods

__construct()

public __construct(array<string|int, mixed> $config) : mixed
Parameters
$config : array<string|int, mixed>
Return values
mixed

__toString()

public __toString() : string
Return values
string

assertObjectType()

public static assertObjectType(mixed $type) : $this
Parameters
$type : mixed
Tags
throws
InvariantViolation
Return values
$this

assertType()

public static assertType(mixed $type) : Type
Parameters
$type : mixed
Return values
Type

assertValid()

Validates type config and throws if one of type options is invalid.

public assertValid() : void

Note: this method is shallow, it won't validate object fields and their arguments.

Tags
throws
InvariantViolation
Return values
void

findField()

public findField(string $name) : FieldDefinition|null
Parameters
$name : string
Return values
FieldDefinition|null

getAllBuiltInTypes()

Returns all builtin in types including base scalar and introspection types

public static getAllBuiltInTypes() : array<string|int, Type>
Return values
array<string|int, Type>

getField()

public getField(string $name) : FieldDefinition
Parameters
$name : string
Return values
FieldDefinition

getFieldNames()

public getFieldNames() : array<int, string>
Tags
inheritDoc
Return values
array<int, string>

getFields()

public getFields() : array<string, FieldDefinition>
Tags
inheritDoc
Return values
array<string, FieldDefinition>

getInternalTypes()

public static getInternalTypes() : array<string|int, Type>
Tags
deprecated

Use method getStandardTypes() instead

codeCoverageIgnore
Return values
array<string|int, Type>

getNamedType()

public static getNamedType(Type $type) : Type|null
Parameters
$type : Type
Return values
Type|null

getNullableType()

public static getNullableType(Type $type) : Type
Parameters
$type : Type
Return values
Type

getStandardTypes()

Returns all builtin scalar types

public static getStandardTypes() : array<string|int, ScalarType>
Return values
array<string|int, ScalarType>

hasField()

public hasField(string $name) : bool
Parameters
$name : string
Return values
bool

isAbstractType()

public static isAbstractType(Type $type) : bool
Parameters
$type : Type
Return values
bool

isBuiltInType()

Checks if the type is a builtin type

public static isBuiltInType(Type $type) : bool
Parameters
$type : Type
Return values
bool

isCompositeType()

public static isCompositeType(Type $type) : bool
Parameters
$type : Type
Return values
bool

isInputType()

public static isInputType(Type $type) : bool
Parameters
$type : Type
Return values
bool

isLeafType()

public static isLeafType(Type $type) : bool
Parameters
$type : Type
Return values
bool

isOutputType()

public static isOutputType(Type $type) : bool
Parameters
$type : Type
Return values
bool

isTypeOf()

public isTypeOf(mixed $value, mixed $context, ResolveInfo $info) : bool|Deferred|null
Parameters
$value : mixed
$context : mixed
$info : ResolveInfo
Return values
bool|Deferred|null

jsonSerialize()

public jsonSerialize() : string
Return values
string

listOf()

public static listOf(Type $wrappedType) : ListOfType
Parameters
$wrappedType : Type
Return values
ListOfType

nonNull()

public static nonNull(callable|NullableType $wrappedType) : NonNull
Parameters
$wrappedType : callable|NullableType
Return values
NonNull

overrideStandardTypes()

public static overrideStandardTypes(array<string, ScalarType$types) : mixed
Parameters
$types : array<string, ScalarType>
Return values
mixed

toString()

public toString() : string
Return values
string

tryInferName()

protected tryInferName() : string|null
Return values
string|null

initializeFields()

private initializeFields() : void
Return values
void

Search results