1. Go to this page and download the library: Download ecodev/graphql-doctrine 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/ */
ecodev / graphql-doctrine example snippets
use GraphQLTests\Doctrine\Blog\Model\Post;
use GraphQLTests\Doctrine\Blog\Types\DateTimeType;
use GraphQLTests\Doctrine\Blog\Types\PostStatusType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Doctrine\DefaultFieldResolver;
use GraphQL\Doctrine\Types;
use Laminas\ServiceManager\ServiceManager;
// Define custom types with a PSR-11 container
$customTypes = new ServiceManager([
'invokables' => [
DateTimeImmutable::class => DateTimeType::class,
'PostStatus' => PostStatusType::class,
],
'aliases' => [
'datetime_immutable' => DateTimeImmutable::class, // Declare alias for Doctrine type to be used for filters
],
]);
// Configure the type registry
$types = new Types($entityManager, $customTypes);
// Configure default field resolver to be able to use getters
GraphQL::setDefaultFieldResolver(new DefaultFieldResolver());
// Build your Schema
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'posts' => [
'type' => Type::listOf($types->getOutput(Post::class)), // Use automated ObjectType for output
'args' => [
[
'name' => 'filter',
'type' => $types->getFilter(Post::class), // Use automated filtering options
],
[
'name' => 'sorting',
'type' => $types->getSorting(Post::class), // Use automated sorting options
],
],
'resolve' => function ($root, $args) use ($types): void {
$queryBuilder = $types->createFilteredQueryBuilder(Post::class, $args['filter'] ?? [], $args['sorting'] ?? []);
// execute query...
},
],
],
]),
'mutation' => new ObjectType([
'name' => 'mutation',
'fields' => [
'createPost' => [
'type' => Type::nonNull($types->getOutput(Post::class)),
'args' => [
'input' => Type::nonNull($types->getInput(Post::class)), // Use automated InputObjectType for input
],
'resolve' => function ($root, $args): void {
// create new post and flush...
},
],
'updatePost' => [
'type' => Type::nonNull($types->getOutput(Post::class)),
'args' => [
'id' => Type::nonNull(Type::id()), // Use standard API when needed
'input' => $types->getPartialInput(Post::class), // Use automated InputObjectType for partial input for updates
],
'resolve' => function ($root, $args): void {
// update existing post and flush...
},
],
],
]),
]);
use GraphQL\Doctrine\Attribute as API;
/**
* Returns the hashed password
*
* @return string
*/
#[API\Exclude]
public function getPassword(): string
{
return $this->password;
}
use GraphQL\Doctrine\Attribute as API;
#[ORM\Column(name: 'password', type: 'string', length: 255)]
#[API\Exclude]
private string $password = '';
use GraphQL\Doctrine\Attribute as API;
use GraphQLTests\Doctrine\Blog\Types\PostStatusType;
/**
* Get status
*
* @return string
*/
#[API\Field(type: PostStatusType::class)]
public function getStatus(): string
{
return $this->status;
}
use GraphQL\Doctrine\Attribute as API;
/**
* Returns all posts of the specified status
*
* @param string $status the status of posts as defined in \GraphQLTests\Doctrine\Blog\Model\Post
*
* @return Collection
*/
public function getPosts(
#[API\Argument(type: '?GraphQLTests\Doctrine\Blog\Types\PostStatusType')]
?string $status = Post::STATUS_PUBLIC
): Collection
{
// ...
}
use GraphQL\Doctrine\Attribute as API;
use GraphQLTests\Doctrine\Blog\Types\PostStatusType;
/**
* Set status
*
* @param string $status
*/
#[API\Input(type: PostStatusType::class)]
public function setStatus(string $status = self::STATUS_PUBLIC): void
{
$this->status = $status;
}
use GraphQL\Doctrine\Attribute as API;
#[API\FilterGroupCondition(type: '?GraphQLTests\Doctrine\Blog\Types\PostStatusType')]
#[ORM\Column(type: 'string', options: ['default' => self::STATUS_PRIVATE])]
private string $status = self::STATUS_PRIVATE;
$mutations = [
'updatePosts' => [
'type' => Type::nonNull(Type::listOf(Type::nonNull($types->get(Post::class)))),
'args' => [
'ids' => Type::nonNull(Type::listOf(Type::nonNull(Type::id()))),
'input' => $types->getPartialInput(Post::class), // Use automated InputObjectType for partial input for updates
],
'resolve' => function ($root, $args) {
// update existing posts and flush...
}
],
];
#[ORM\Column(type: 'string']
private $name = 'jane';
public function setName(string $name = 'john'): void
{
$this->name = $name;
}
public function setFoo(string $foo = 'defaultFoo'): void
{
// do something
}
public function setBar(string $bar): void
{
// do something
}
use Doctrine\ORM\Mapping as ORM;
use GraphQL\Doctrine\Attribute as API;
use GraphQLTests\Doctrine\Blog\Filtering\SearchOperatorType;
/**
* A blog post with title and body
*/
#[ORM\Entity]
#[API\Filter(field: 'custom', operator: SearchOperatorType::class, type: 'string')]
final class Post extends AbstractModel
use Doctrine\ORM\Mapping as ORM;
use GraphQL\Doctrine\Attribute as API;
use GraphQLTests\Doctrine\Blog\Sorting\UserName;
/**
* A blog post with title and body
*/
#[ORM\Entity]
#[API\Sorting([UserName::class])]
final class Post extends AbstractModel
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.