Skip to content

Doctrine GraphQL generates types, queries and mutators for Doctrine entities to be used as a GraphQL API.

Notifications You must be signed in to change notification settings

ratehub/doctrine-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

doctrine-graphql

Build Status Total Downloads Latest Stable Version License

Generates all the necessary types, queries and mutators using the Doctrine model metadata. All standard data fetching uses the doctrine Array Hydrator. This provider will handle the Object hydration once all queries are finished and the data is ready to be returned.

Features

Whitelist/Blacklist

The provider can be setup to filter what objects and properties are included in the final graphql api.

  • Whitelisting will only include types/properties that are marked for inclusion
  • Blacklisting will only include types/properties that are not marked for exclusion

Authorization

An authorization provider can be implemented to limit access to types and fields via cred level permissions

Naming Overrides

Names and descriptions of types and methods can be overwritten via the GraphQLType and GraphQLProperty annotations. Field Names are not yet able to be overwritten.

Deferred Loading

Associations take advantage of deferred loading via the DeferredBuffer. This is used to significantly reduce the number of queries and solves the N+1 problem when loading data (https://secure.phabricator.com/book/phabcontrib/article/n_plus_one/)

Custom Resolvers

Any property can have it's own custom resolver instead of the standard one provided by the provider.

Pagination

The provider supports key and offset pagination for top level queries. Also supports limiting results returned in an n-to-Many relationship.

Sorting

A queries results can be sorted by one or more fields in either ascending or descending order.

Query Filters for Types

Core types such as Strings, DateTime, Int, and BigInt support filters such as

equals, in, less, greater, lessOrEqual, greaterOrEqual, between

Polymorphic Entities

Provider supports querying polymorphic entities and querying unique fields per type using the GraphQL inline fragments.

Getting Started

After initializing doctrine you'll need to register the graphql annotations:

use RateHub\GraphQL\Doctrine\AnnotationLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
 
AnnotationRegistry::registerLoader(array(new AnnotationLoader(), "load"));

The next step is to initialize the graphql schema:

use RateHub\GraphQL\Doctrine\DoctrineProvider;
use RateHub\GraphQL\Doctrine\DoctrineProviderOptions;
use RateHub\GraphQL\GraphContext;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Schema;

// Set the options including any extensions
$options = new DoctrineProviderOptions();
$options->em = $em; // EntityManager initialized within your as app as needed
$options->filter = 'blacklist'

// Initialize the Provider. With blacklist filtering, no
// annotations are needed unless something needs to be 
// excluded. The provider will generate all queries, 
// mutators and types needed.
$provider = new DoctrineProvider('default', $options);

// Initialize top level types
$context = new GraphContext();
$queryType = new ObjectType('query', $provider->getQueries());
$mutatorType = new ObjectType('mutator', $provider->getMutators());

// Initialize the schema
$schema = new Schema([
                      'query' => $queryType,
                      'mutation' => $mutatorType,
                      'types' => $provider->getTypes()
                    ]);

From here you can execute a query:

   $result = \GraphQL\GraphQL::execute(
       $schema,
       $params['query'], // Request parameter containing the graphql query
       null,
       $context,
       null
   );

About

Doctrine GraphQL generates types, queries and mutators for Doctrine entities to be used as a GraphQL API.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published