dev-master
9999999-devUse Doctrine entities as GraphQL types
The Requires
by Rahul Jayaraman
v0.1.0
0.1.0.0Use Doctrine entities as GraphQL types
The Requires
by Rahul Jayaraman
Wallogit.com
2017 © Pedro Peláez
Use Doctrine entities as GraphQL types
Builds GraphQL types out of doctrine entities, (*1)
$> curl -sS https://getcomposer.org/installer | php $> php composer.phar require rahuljayaraman/doctrine-graphql
PHP >=5.4, (*2)
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., (*3)
Here's an example. Consider the following schema, (*4)
Employee │ └───Company (getCompanies) | └───User (getUser)
We can extract the type for Employee by using, (*5)
Mapper::extractType(Employee::class)
Associations are recursively extracted as well. So $employee->getCompanies() would return ListOf Type Company & $employee->getUser() would return Type User, (*6)
Setup accepts 3 args. Doctrine's EntityManager, a setter method & a getter method to a type store (a data structure which stores types)., (*7)
//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, (*8)
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, (*9)
Mapper::extractType(Entity::class);
We could place it here if using with Folkloreatelier/laravel-graphql., (*10)
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., (*11)
For registering additional fields, one can use the RegisterField annotation., (*12)
RegisterField accepts name, type and args., (*13)
name accepts a string., (*14)
type accepts either an internal type or any of the extracted entities., (*15)
args accepts an array of tuples in the form of {{string, type}}, (*16)
Here's an example, (*17)
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., (*18)
use RahulJayaraman\DoctrineGraphQL\Annotations\BlacklistField; /** * @var string * @ORM\Column(type="string") * @BlacklistField() */ private $password;
Use Doctrine entities as GraphQL types
Use Doctrine entities as GraphQL types