doctrine-graphql
Doctrine ORM to PHP GraphQL bridge.
How it works
This library provides a DoctrineGraphQL builder class to build all Doctrine entities as GraphQL types. It also will for every GraphQL type. For the mapping to works, this library made some assumptions:
- Entity's fully qualified class name will be used as GraphQL type without the
\
character (e.g.Some\Namespace\Entity\User
will be mapped toSomeNamespaceEntityUser
GraphQL type. - Associated fields will be available as GraphQL type property when that entity is the association owner.
- For every entity that were mapped as GraphQL type, this library can create default CRUD queries & mutations (more on this later).
Installation
composer require ncrypthic/doctrine-graphql
Usage
use LLA\DoctrineGraphQL\DoctrineGraphQL;
use GraphQL\Server\Helper;
/* @var EntityManager $em */
// Get Doctrine's entity manager
$builder = new DoctrineGraphQL();
$builder
->buildTypes($em)
->buildQueries($em)
->buildMutations($em);
$schema = $builder->toGraphQLSchema();
$config = ['schema'=>$schema];
$helper = new Helper();
$req = $helper->parseHttpRequest();
$res = is_array($req)
? $helper->executeBatch($config, $req)
: $helper->executeOperation($config, $req);
Queries
For every Doctrine entity, DoctrineGraphQL will generate 2 graphql queries:
- To get a single entity record
get<GraphQLTypeName>
- To get paginated list of entity records
get<GraphQLTypeName>Page
See more detail on Queries section.
Mutations
For every Doctrine entity, DoctrineGraphQL will generate 3 graphql mutations:
- To insert a single entity record
create<GraphQLTypeName>
- To update a single entity record
update<GraphQLTypeName>
- To delete a single entity record
delete<GraphQLTypeName>
See more detail on Mutations section.