Remaining Relationships
Okay, we now have our user type connected to the related jobs. So let's finish up the rest of our relationships.
Create Connections
Let's create a connection for Job -> Tasks and Tasks -> Assigned Users.
$ php artisan lighthouse:connection JobTasksConnection
$ php artisan lighthouse:connection TaskUsersConnection
JobTasksConnection Class
// app/Http/GraphQL/Connections/JobTasksConnection.php
namespace App\Http\GraphQL\Connections;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Interfaces\Connection;
class JobTasksConnection implements Connection
{
/**
* Get the name of the connection.
* Note: Connection names must be unique
*
* @return string
*/
public function name()
{
return 'JobTasksConnection';
}
/**
* Get name of connection.
*
* @return string
*/
public function type()
{
return 'task';
}
/**
* Available connection arguments.
*
* @return array
*/
public function args()
{
return [];
}
/**
* Resolve connection.
*
* @param \App\Job $parent
* @param array $args
* @param mixed $context
* @param ResolveInfo $info
* @return mixed
*/
public function resolve($parent, array $args, $context, ResolveInfo $info)
{
return $parent->tasks()->getConnection($args);
}
}
TaskUsersConnection Class
// app/Http/GraphQL/Connections/JobTasksConnection.php
namespace App\Http\GraphQL\Connections;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Interfaces\Connection;
class TaskUsersConnection implements Connection
{
/**
* Get the name of the connection.
* Note: Connection names must be unique
*
* @return string
*/
public function name()
{
return 'TaskUsersConnection';
}
/**
* Get name of connection.
*
* @return string
*/
public function type()
{
return 'user';
}
/**
* Available connection arguments.
*
* @return array
*/
public function args()
{
return [];
}
/**
* Resolve connection.
*
* @param \App\Task $parent
* @param array $args
* @param mixed $context
* @param ResolveInfo $info
* @return mixed
*/
public function resolve($parent, array $args, $context, ResolveInfo $info)
{
return $parent->users()->getConnection($args);
}
}
Update JobType Fields
// app/Http/GraphQL/Types/JobType.php
use App\Http\GraphQL\Connections\JobTasksConnection;
class JobType extends GraphQLType implements RelayType
{
// ...
/**
* Type fields.
*
* @return array
*/
public function fields()
{
return [
'title' => [
'type' => Type::string(),
'description' => 'Title of the job.',
],
'user' => [
'type' => GraphQL::type('user'),
'description' => 'User who created the job.',
'resolve' => function ($parent, array $args) {
return $parent->user;
}
],
'tasks' => GraphQL::connection(new JobTasksConnection)->field(),
];
}
}
Update TaskType Fields
// app/Http/GraphQL/Types/TaskType.php
use App\Http\GraphQL\Connections\TaskUsersConnection;
class TaskType extends GraphQLType implements RelayType
{
// ...
/**
* Type fields.
*
* @return array
*/
public function fields()
{
return [
'title' => [
'type' => Type::string(),
'description' => 'Title of the job task.',
],
'users' => GraphQL::connection(new TaskUsersConnection)->field(),
];
}
}