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 to add a custom type to an entity type). Your Doctrine metadata will map the associations in GraphQL.
There are some config options available but they are all optional.
The first step is to add attributes to your entities.
Attributes are stored in the namespace
ApiSkeletons\Doctrine\ORM\GraphQL\Attribute and there are attributes for
Entity, Field, Association, and ComputedField. Use the appropriate attribute on
each element you want to be queryable from GraphQL.
use ApiSkeletons\Doctrine\ORM\GraphQL\Attribute as GraphQL;
#[GraphQL\Entity]
class Artist
{
#[GraphQL\Field]
private $id;
#[GraphQL\Field]
private $name;
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
}
That’s the minimum configuration required. Each field is read with its
getter, as an entity is extracted by value by default; see the
extractByValue parameter of the Entity attribute. Next, create your driver using your
entity manager
use ApiSkeletons\Doctrine\ORM\GraphQL\Driver;
$driver = new Driver($entityManager);
Then configure your GraphQL schema. In this section we’ll create a connection for the entity, filters for the entity, and a resolver.
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'artists' => [
'type' => $driver->connection(Artist::class),
'args' => ['filter' => $driver->filter(Artist::class)] + $driver->pagination(),
'resolve' => $driver->resolve(Artist::class),
],
],
]),
]);
Now, using the schema, you can start making GraphQL queries
use GraphQL\GraphQL;
$query = '
{
artists {
edges {
node {
id
name
}
}
}
}
';
$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.
use ApiSkeletons\Doctrine\ORM\GraphQL\Attribute as GraphQL;
#[GraphQL\Entity]
class Artist
{
#[GraphQL\Field]
private $id;
#[GraphQL\Field]
private $name;
#[GraphQL\Association]
private $performances;
}
#[GraphQL\Entity]
class Performance
{
#[GraphQL\Field]
private $id;
#[GraphQL\Field]
private $venue;
}
Using the same Schema configuration as above, with the new Performance attributes, a query of performances is now possible:
use GraphQL\GraphQL;
$query = '
{
artists {
edges {
node {
id
name
performances {
edges {
node {
id
venue
}
}
}
}
}
}
}
';
$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, implement custom types, alias fields, and more.
This is documentation for API-Skeletons/doctrine-orm-graphql. Please add your ★ star to the project.