Queries

TODO: Introduction to Queries

We are going to create a single root query, but in your own applications it may make sense to add multiple. It's completely up to your own individual use case.

All queries return a type. We are going to create a Viewer query that will return a UserType that will return the authenticated user.

Query Class

Type

The type function tells GraphQL what registered Type the query will return. We haven't registered the Type with GraphQL yet but we will in the next section.

Args

Each query can have arguments passed in, however we won't need any for this particular query (since we're just going to return the authenticated user).

Resolve

The resolve function will return the requested type. For this query, we're just going to return the authenticated user.

Creating the Viewer Query

$ php artisan lighthouse:query ViewerQuery

ViewerQuery Class

// app/Http/GraphQL/Queries/ViewerQuery.php

namespace App\Http\GraphQL\Queries;

use GraphQL;
use GraphQL\Type\Definition\Type;
use Nuwave\Lighthouse\Support\Definition\GraphQLQuery;

class ViewerQuery extends GraphQLQuery
{
    /**
     * Type query returns.
     *
     * @return Type
     */
    public function type()
    {
        return GraphQL::type('user');
    }

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

    /**
     * Resolve the query.
     *
     * @param  mixed  $root
     * @param  array  $args
     * @return mixed
     */
    public function resolve($root, array $args)
    {
        return auth()->user();
    }
}

results matching ""

    No results matching ""