Just The Basics

The goal of this project is to get you up and running with GraphQL and Doctrine as easily as possible.

You will need a Doctrine object manager with entities configured with appropriate associations throughout. Support for ad-hoc joins between entities is not supported but you can use the EntityDefinition event in combination with the FilterQueryBuilder event to add a new field to a Type with such ad-hoc support. Your Doctrine metadata must contain all the associations. This requriement relates to the very basics of working in Doctrine.

There are some config options available but they are all optional.

The first step is to add attributes to your entities. Attributes are a feature of PHP 8.0 which act like annotations but are built into the language. Attributes are stored in the namespace ApiSkeletons\Doctrine\GraphQL\Attribute and there are attributes for Entity, Field, and Association. Use the appropriate attribute on each element you want to be queryable from GraphQL.

 1<?php
 2
 3use ApiSkeletons\Doctrine\GraphQL\Attribute as GraphQL;
 4
 5#[GraphQL\Entity]
 6class Artist
 7{
 8    #[GraphQL\Field]
 9    private $id;
10
11    #[GraphQL\Field]
12    private $name;
13}

That’s the minimum configuration requried. Next, create your driver using your entity manager

1<?php
2
3use ApiSkeletons\Doctrine\GraphQL\Driver;
4
5$driver = new Driver($entityManager);

The next step is configuring your GraphQL schema. In this section we’ll create types for the entity, the filter for the entity, and the resolver.

 1<?php
 2
 3use GraphQL\Type\Definition\ObjectType;
 4use GraphQL\Type\Definition\Type;
 5use GraphQL\Type\Schema;
 6
 7$schema = new Schema([
 8    'query' => new ObjectType([
 9        'name' => 'query',
10        'fields' => [
11            'artist' => [
12                'type' => $driver->connection($driver->type(Artist::class)),
13                'args' => [
14                    'filter' => $driver->filter(Artist::class),
15                    'pagination' => $driver->pagination(),
16                ],
17                'resolve' => $driver->resolve(Artist::class),
18            ],
19        ],
20    ]),
21]);

Now, using the schema, you can start making GraphqL queries

1<?php
2
3use GraphQL\GraphQL;
4
5$query = '{ artist { edges { node { id name } } } }';
6
7$result = GraphQL::executeQuery($schema, $query);

If you want to add an association you must set attributes on the target entity. In the following example the Artist entity has a one-to-many relationship with Performance and we want to make deeper queries from Artist to Performance.

 1<?php
 2
 3use ApiSkeletons\Doctrine\GraphQL\Attribute as GraphQL;
 4
 5#[GraphQL\Entity]
 6class Artist
 7{
 8    #[GraphQL\Field]
 9    private $id;
10
11    #[GraphQL\Field]
12    private $name;
13
14    #[GraphQL\Association]
15    private $performances;
16}
17
18#[GraphQL\Entity]
19class Performance
20{
21    #[GraphQL\Field]
22    private $id;
23
24    #[GraphQL\Field]
25    private $venue;
26}

Using the same Schema configuration as above, with the new Performance attributes, a query of performances is now possible:

1<?php
2
3use GraphQL\GraphQL;
4
5$query = '{ artist { edges { node { id name performances { edges { node { id venue } } } } } } }';
6
7$result = GraphQL::executeQuery($schema, $query);

Keep reading to learn how to create multiple attribute groups, extract entities by reference or by value, cache attribute metadata, enable custom types, add documentation to every attribute, and more.


This is documentation for API-Skeletons/doctrine-graphql. Please add your ★ star to the project.

Authored by API Skeletons.