rahuljayaraman / doctrine-graphql
Use Doctrine entities as GraphQL types
Installs: 6 619
Dependents: 0
Suggesters: 0
Security: 0
Stars: 13
Watchers: 5
Forks: 1
Open Issues: 6
pkg:composer/rahuljayaraman/doctrine-graphql
Requires
- php: >=5.3.0
- doctrine/common: >=2.0
- doctrine/orm: >=2.0
- webonyx/graphql-php: ^0.9
This package is not auto-updated.
Last update: 2025-10-11 03:41:50 UTC
README
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
Doctrine GraphQL Mapper
Builds GraphQL types out of doctrine entities
Installation
$> curl -sS https://getcomposer.org/installer | php
$> php composer.phar require rahuljayaraman/doctrine-graphql
Requirements
PHP >=5.4
Overview
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.
TODO
Usage
Setup
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); } );
Extract type
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); }
Default resolver
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.
Register additional fields
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 ... }
Blacklist fields
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;