Adding a Connection

Our ViewerQuery returns a UserType, which is mapped to a User Eloquent Model. Our user model has a hasMany relationship with Jobs, so let's create a jobs connection on our UserType.

Adding Types to Schema

Before creating a new connection, let's add our JobType and TaskType to our schema file.

// routes/graphql.php

GraphQL::schema()->group(['namespace' => 'App\\Http\\GraphQL'], function () {
    GraphQL::schema()->type('job', 'Types\\JobType');
    GraphQL::schema()->type('task', 'Types\\TaskType');
    // ...
});

Creating a Connection Class

To create a new connection, we'll use Lighthouse's built in generator.

$ php artisan lighthouse:connection UserJobsConnection

Connection Class

Name

The name of the connection that will be registered with GraphQL.

Type

The Type that the connection will resolve to

Since we named our JobType "job" in the schema above, that is the name we will use here.

Args

Any arguments you wish to include to query the relationship.

Lighthouse will automatically include Relay arguments by default (i.e., first, after, before, last)

Resolve

In this connection the $parent is the User, so we can query the relationship on it to return a collection of jobs the user has created. Notice in the resolve function below we're using a built in helper called getConnection and passing in the $args. Because we added the RelayConnection trait to our models, Lighthouse will take care of limiting and offsetting the query for us.

// app/Http/GraphQL/Connections/UserJobsConnection.php

namespace App\Http\GraphQL\Connections;

use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Interfaces\Connection;

class UserJobsConnection implements Connection
{

    /**
     * Get the name of the connection.
     * Note: Connection names must be unique
     *
     * @return string
     */
    public function name()
    {
        return 'UserJobsConnection';
    }

    /**
     * Get name of connection.
     *
     * @return string
     */
    public function type()
    {
        return 'job';
    }

    /**
     * Available connection arguments.
     *
     * @return array
     */
    public function args()
    {
        return [];
    }

    /**
     * Resolve connection.
     *
     * @param  \App\User  $parent
     * @param  array  $args
     * @param  mixed  $context
     * @param  ResolveInfo $info
     * @return mixed
     */
    public function resolve($parent, array $args, $context, ResolveInfo $info)
    {
        return $parent->jobs()->getConnection($args);
    }
}

Querying a Connection

Alright, we should be all set! Let's send the following query to the server and see what we get back in the response.

{
  viewer {
    name
    email
    jobs(first: 2) {
      edges {
        node {
          id
          title
        }
      }
    }
  }
}

Nice! We asked the server for the name and email of our authenticated user as well as the first two jobs associated with it and that's exactly what GraphQL returns!

results matching ""

    No results matching ""