Typically our database is designed with some relationships. When retrieving data from our GraphQL, we would want them to present in a relationship hierarchy as well.
In this tutorial, we will learn how to build nested queries using Youshido/GraphQL.
In our blog example, there is a series of articles and each article belongs to an author. When retrieving articles' data, it makes more sense to fetch belonging author's data as well instead of "author_id".
Apparently, we need AuthorType in order to resolve the type.
Add src/Type/AuthorType.php
<?php
namespace StarTutorial\Type;
use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Object\AbstractObjectType;
use Youshido\GraphQL\Type\Scalar\IntType;
use Youshido\GraphQL\Type\Scalar\StringType;
class AuthorType extends AbstractObjectType
{
public function build($config)
{
$config->addFields([
'id' => new NonNullType(new IntType()),
'name' => new StringType()
]);
}
}
As we can see, it is a super simple type with an id(integer) and name(string) fields.
We will use AuthorField to resolve the request.
Add src/Field/AuthorField.php
<?php
namespace StarTutorial\Field;
use StarTutorial\Repository\AuthorsRepository;
use StarTutorial\Type\AuthorType;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Field\AbstractField;
class AuthorField extends AbstractField
{
public function getType()
{
return new AuthorType();
}
public function resolve($value, array $args, ResolveInfo $info)
{
$authorId = $value['author_id'];
$all = AuthorsRepository::findAll();
foreach ($all as $single) {
if ($single['id'] == $authorId) {
return $single;
}
}
return null;
}
}
Let's take a close look at the resolve method.
When getting the $authorId, we fetch it directly from the $value variable as shown below:
$authorId = $value['author_id'];
This is because $value is configured to contain values from the parent. Which is article is our case. And because we know article will return an author_id, we fetch it directly from $value variable.
The rest of the code is self-explanatory, we find the author that article belongs to using our fake repository and return it.
We are almost done. The last piece is to modify ArticleField.php to wire ArticleType and AuthorType up.
To do this, open src/Type/ArticleType.php. And change
'author_id' => new NonNullType(new IntType())
to
'author' => new AuthorField()
Our final src/Type/ArticleType.php file looks like this after the modification:
<?php
namespace StarTutorial\Type;
use StarTutorial\Field\AuthorField;
use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Object\AbstractObjectType;
use Youshido\GraphQL\Type\Scalar\DateTimeType;
use Youshido\GraphQL\Type\Scalar\IntType;
use Youshido\GraphQL\Type\Scalar\StringType;
class ArticleType extends AbstractObjectType
{
public function build($config)
{
$config->addFields([
'id' => new NonNullType(new IntType()),
'title' => new StringType(),
'content' => new StringType(),
'created' => new DateTimeType(),
'author' => new AuthorField()
]);
}
}
In the first tutorial, we are getting articles data using a query similar to:
{ article(id:2){id,title,author_id} }
Now we can get full author data using:
{ article(id:2){id,title,author{name}} }
The magic here is that, if you do not ask for author's data. The resolve method(AuthorField::resolve) will never be called. The Youshido/GraphQL library takes care of the job of figuring out which resolve methods to call.
Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.