PHP code example of api-skeletons / doctrine-graphql
1. Go to this page and download the library: Download api-skeletons/doctrine-graphql library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
api-skeletons / doctrine-graphql example snippets
use ApiSkeletons\Doctrine\GraphQL\Attribute as GraphQL;
#[GraphQL\Entity]
class Artist
{
#[GraphQL\Field]
public $id;
#[GraphQL\Field]
public $name;
#[GraphQL\Association]
public $performances;
}
#[GraphQL\Entity]
class Performance
{
#[GraphQL\Field]
public $id;
#[GraphQL\Field]
public $venue;
/**
* Not all fields need attributes.
* Only add attribues to fields you want available in GraphQL
*/
public $city;
}
use ApiSkeletons\Doctrine\GraphQL\Driver;
use Doctrine\ORM\EntityManager;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
$driver = new Driver($entityManager);
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'artists' => [
'type' => $driver->connection($driver->type(Artist::class)),
'args' => [
'filter' => $driver->filter(Artist::class),
'pagination' => $driver->pagination(),
],
'resolve' => $driver->resolve(Artist::class),
],
],
]),
'mutation' => new ObjectType([
'name' => 'mutation',
'fields' => [
'artistUpdateName' => [
'type' => $driver->type(Artist::class),
'args' => [
'id' => Type::nonNull(Type::id()),
'input' => Type::nonNull($driver->input(Artist::class, ['name'])),
],
'resolve' => function ($root, $args) use ($driver): Artist {
$artist = $driver->get(EntityManager::class)
->getRepository(Artist::class)
->find($args['id']);
$artist->setName($args['input']['name']);
$driver->get(EntityManager::class)->flush();
return $artist;
},
],
],
]),
]);
use ApiSkeletons\Doctrine\GraphQL\Event\FilterQueryBuilder;
use App\ORM\Entity\Artist;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Schema;
use League\Event\EventDispatcher;
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'artists' => [
'type' => $driver->connection($driver->type(Artist::class)),
'args' => [
'filter' => $driver->filter(Artist::class),
'pagination' => $driver->pagination(),
],
'resolve' => $driver->resolve(Artist::class, Artist::class . '.filterQueryBuilder'),
],
],
]),
]);
$driver->get(EventDispatcher::class)->subscribeTo(Artist::class . '.filterQueryBuilder',
function(FilterQueryBuilder $event) {
$event->getQueryBuilder()
->innerJoin('entity.user', 'user')
->andWhere($event->getQueryBuilder()->expr()->eq('user.id', ':userId'))
->setParameter('userId', currentUser()->getId())
;
}
);
use ApiSkeletons\Doctrine\GraphQL\Attribute as GraphQL;
use ApiSkeletons\Doctrine\GraphQL\Event\FilterCriteria;
use App\ORM\Entity\Artist;
use League\Event\EventDispatcher;
#[GraphQL\Entity]
class Artist
{
#[GraphQL\Field]
public $id;
#[GraphQL\Field]
public $name;
#[GraphQL\Association(filterCriteriaEventName: self::class . '.performances.filterCriteria')]
public $performances;
}
// Add a listener to your driver
$driver->get(EventDispatcher::class)->subscribeTo(
Artist::class . '.performances.filterCriteria',
function (FilterCriteria $event): void {
$event->getCriteria()->andWhere(
$event->getCriteria()->expr()->eq('isDeleted', false)
);
},
);
use ApiSkeletons\Doctrine\GraphQL\Driver;
use ApiSkeletons\Doctrine\GraphQL\Event\EntityDefinition;
use App\ORM\Entity\Artist;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use League\Event\EventDispatcher;
$driver = new Driver($entityManager);
$driver->get(EventDispatcher::class)->subscribeTo(
Artist::class . '.definition',
static function (EntityDefinition $event): void {
$definition = $event->getDefinition();
// In order to modify the fields you must resovle the closure
$fields = $definition['fields']();
// Add a custom field to show the name without a prefix of 'The'
$fields['nameUnprefix'] = [
'type' => Type::string(),
'description' => 'A computed dynamically added field',
'resolve' => static function ($objectValue, array $args, $context, ResolveInfo $info): mixed {
return trim(str_replace('The', '', $objectValue->getName()));
},
];
$definition['fields'] = $fields;
}
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.