WARNING: This repo is just an experiment & is not maintained. Please use at your own risk. Suggest using https://github.com/Ecodev/graphql-doctrine instead
Builds GraphQL types out of doctrine entities
$> curl -sS https://getcomposer.org/installer | php
$> php composer.phar require rahuljayaraman/doctrine-graphql
PHP >=5.4
The mapper builds GraphQL types out of doctrine entities. It's built on top of webonyx/graphql-php and maps doctrine entities to an ObjectType graph at runtime.
Here's an example. Consider the following schema
Employee
│
└───Company (getCompanies)
|
└───User (getUser)
We can extract the type for Employee by using
Mapper::extractType(Employee::class)Associations are recursively extracted as well. So $employee->getCompanies() would return ListOf Type Company & $employee->getUser() would return Type User
NOTE: This library is not responsible for any of your GraphQL setup like setting up routes, root nodes etc.
Setup accepts 3 args. Doctrine's EntityManager, a setter method & a getter method to a type store (a data structure which stores types).
//Setup code, I use this in a laravel service provider
use RahulJayaraman\DoctrineGraphQL\Mapper;
use Doctrine\ORM\EntityManager;
Mapper::setup(
app(EntityManager::class),
function ($typeName, $type) {
Cache::add($type, $typeName);
},
function ($typeName) {
return Cache::get($typeName);
}
);Cache above could be replaced by any store.
Eg. using Folkloreatelier/laravel-graphql's store
use Folklore\GraphQL\Support\Facades\GraphQL;
Mapper::setup(
app(EntityManager::class),
function ($typeName, $type) {
GraphQL::addType($type, $typeName);
},
function ($typeName) {
return GraphQL::type($typeName);
}
);To extract the type
Mapper::extractType(Entity::class);We could place it here if using with Folkloreatelier/laravel-graphql.
public function type()
{
return Mapper::extractType(Entity::class);
}For now, given a field name, say fieldName, the mapper will look for a getFieldName getter method on the entity. There are plans to allow customization here.
For registering additional fields, one can use the RegisterField annotation.
RegisterField accepts name, type and args.
name accepts a string.
type accepts either an internal type or any of the extracted entities.
args accepts an array of tuples in the form of {{string, type}}
Here's an example
use RahulJayaraman\DoctrineGraphQL\Annotations\RegisterField;
/**
* getEmployee
*
* @RegisterField(name="CustomName" type="Employee", args={{"slug", "string"}})
*/
public function getEmployee($slug)
{
return ...
}Fields can be blacklisted using the BlacklistField annotation. Here's an example.
use RahulJayaraman\DoctrineGraphQL\Annotations\BlacklistField;
/**
* @var string
* @ORM\Column(type="string")
* @BlacklistField()
*/
private $password;