GraphQL Types
Introduction to Type (TODO)
We want to expose each of our Eloquent models in our GraphQL server, so we just need to create a type for each one. Because we want our types to be Relay Compliant, each type will implement the RelayType interface.
Type Class Overview
Attributes
This is the description of the type which includes it's name and description (which can be used when querying the GraphQL server).
When implementing the
RelayType
, we do not need to add theid
field as Lighthouse will add it for us.
ResolveById
Because this is a relay compliant Type, we must provide a way to resolve the type by passing in the id.
Fields
These are the fields that are available on this type (in terms of Eloquent, they are just like model attributes).
Creating the Types
$ php artisan lighthouse:type UserType --relay
$ php artisan lighthouse:type JobType --relay
$ php artisan lighthouse:type TaskType --relay
UserType Class
For our application, we will just expose the's user's name and email address. We will be adding the relationships to the UserType later in the walkthrough.
// app/Http/GraphQL/Types/UserType.php
namespace App\Http\GraphQL\Types;
use GraphQL;
use GraphQL\Type\Definition\Type;
use Nuwave\Lighthouse\Support\Definition\GraphQLType;
use Nuwave\Lighthouse\Support\Interfaces\RelayType;
class UserType extends GraphQLType implements RelayType
{
/**
* Attributes of type.
*
* @var array
*/
protected $attributes = [
'name' => 'User',
'description' => 'Employee of Acme Job Management Inc.'
];
/**
* Get model by id.
*
* Note: When the root 'node' query is called, this method
* will be used to resolve the type by providing the id.
*
* @param mixed $id
* @return mixed
*/
public function resolveById($id)
{
return \App\User::find($id);
}
/**
* Type fields.
*
* @return array
*/
public function fields()
{
return [
'name' => [
'type' => Type::string(),
'description' => 'Name of the employee.',
],
'email' => [
'type' => Type::string(),
'description' => 'Email address of the employee.',
],
];
}
}
JobType Class
// app/Http/GraphQL/Types/JobType.php
namespace App\Http\GraphQL\Types;
use GraphQL;
use GraphQL\Type\Definition\Type;
use Nuwave\Lighthouse\Support\Definition\GraphQLType;
use Nuwave\Lighthouse\Support\Interfaces\RelayType;
class JobType extends GraphQLType implements RelayType
{
/**
* Attributes of type.
*
* @var array
*/
protected $attributes = [
'name' => 'Job',
'description' => 'A job created by an employee.',
];
/**
* Get model by id.
*
* Note: When the root 'node' query is called, this method
* will be used to resolve the type by providing the id.
*
* @param mixed $id
* @return mixed
*/
public function resolveById($id)
{
return \App\Job::first($id);
}
/**
* Type fields.
*
* @return array
*/
public function fields()
{
return [
'title' => [
'type' => Type::string(),
'description' => 'Title of the job.',
]
];
}
}
TaskType Class
// app/Http/GraphQL/Types/TaskType.php
namespace App\Http\GraphQL\Types;
use GraphQL;
use GraphQL\Type\Definition\Type;
use Nuwave\Lighthouse\Support\Definition\GraphQLType;
use Nuwave\Lighthouse\Support\Interfaces\RelayType;
class TaskType extends GraphQLType implements RelayType
{
/**
* Attributes of type.
*
* @var array
*/
protected $attributes = [
'name' => 'Task',
'description' => 'A task assigned to a job.'
];
/**
* Get model by id.
*
* Note: When the root 'node' query is called, this method
* will be used to resolve the type by providing the id.
*
* @param mixed $id
* @return mixed
*/
public function resolveById($id)
{
return \App\Task::find($id);
}
/**
* Type fields.
*
* @return array
*/
public function fields()
{
return [
'title' => [
'type' => Type::string(),
'description' => 'Title of the job task.',
],
];
}
}