Skip to content

Type Definitions

graphql-php represents a type as a class instance from the GraphQL\Type\Definition namespace:

Input vs. Output Types

All types in GraphQL are of two categories: input and output.

Obviously, NonNull and List types belong to both categories depending on their inner type.

Definition styles

Several styles of type definitions are supported depending on your preferences.

Inline definitions

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

$myType = new ObjectType([
    'name' => 'MyType',
    'fields' => [
        'id' => Type::id()
    ]
]);

Class per type

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

class MyType extends ObjectType
{
    public function __construct()
    {
        $config = [
            // Note: 'name' is not needed in this form:
            // it will be inferred from class name by omitting namespace and dropping "Type" suffix
            'fields' => [
                'id' => Type::id()
            ]
        ];
        parent::__construct($config);
    }
}

Schema definition language

schema {
  query: Query
  mutation: Mutation
}

type Query {
  greetings(input: HelloInput!): String!
}

input HelloInput {
  firstName: String!
  lastName: String
}

Read more about building an executable schema using schema definition language.