PHPackages                             guym4c/doctrine-graphql-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. guym4c/doctrine-graphql-helper

ActiveLibrary[Database &amp; ORM](/categories/database)

guym4c/doctrine-graphql-helper
==============================

A helper package for generating a GraphQL schema from Doctrine entities

v3.0.0(5y ago)031MITPHPPHP ^7.4CI failing

Since Apr 13Pushed 5y ago1 watchersCompare

[ Source](https://github.com/guym4c/doctrine-graphql-helper)[ Packagist](https://packagist.org/packages/guym4c/doctrine-graphql-helper)[ Docs](https://github.com/guym4c/doctrine-graphql-helper)[ RSS](/packages/guym4c-doctrine-graphql-helper/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (7)Dependencies (4)Versions (8)Used By (0)

doctrine-graphql-helper
=======================

[](#doctrine-graphql-helper)

Generate a PSR-7 GraphQL API server from an instance of the Doctrine ORM in just a few lines of PHP, with permissions and custom mutation resolvers available out-of-the-box.

Install
-------

[](#install)

Via Composer:

```
composer install guym4c/doctrine-graphql-helper
```

Usage
-----

[](#usage)

This package is a helper package for [`graphql-php`](https://github.com/webonyx/graphql-php) and [`graphql-doctrine`](https://github.com/ecodev/graphql-doctrine), which install automatically with it. Entities used with this package must be [`graphql-doctrine`](https://github.com/ecodev/graphql-doctrine) -compatible. Refer to these packages’ documentation for more details.

### Implementing `GraphQLEntity`

[](#implementing-graphqlentity)

Any entities which you wish to use in your API must extend `GraphQLEntity` and implement the required methods.

You probably won't want any methods you implement to be added to the schema by `graphql-doctrine`, and so you must annotate them as excluded:

```
use GraphQL\Doctrine\Annotation as API;

/**
 * @API\Exclude
 */
public function someMethod() {}
```

#### Entity constructors

[](#entity-constructors)

When an entity is created over GraphQL using a create mutator, it is constructed using the `buildFromJson()` static, which calls the constructor with no parameters. If your entity has a constructor with parameters, then you will need to override `buildFromJson()` in your entity class and call the constructor yourself.

After you've performed any tasks you need to, you may still use the inherited `hydrate()` call after this to fill out the object with the input data. You must unset any properties that you have already hydrated yourself from the input array `$data` before you make this call, or your existing properties will be overwritten.

#### Events

[](#events)

In addition to the events that Doctrine provides you with, the schema builder adds events that fire during the execution of some resolvers: `beforeUpdate()` and `beforeDelete()`. You may extend these from `GraphQLEntity`. Both fire immediately after the entity in its initial state is retrieved from the ORM, and before any operation is performed. (For `beforeDelete()` in particular, these means all fields, including generated values, are accessible.)

#### Permissions

[](#permissions)

You always need to implement `hasPermission()`, regardless of whether you intend to implement permissions at this level or not. You can find more details on implementing permissions below, or just stub it out with a `return true;` for the moment. For security reasons, the builder does not default to this.

### Building the schema

[](#building-the-schema)

Construct a schema builder on entities of your choice. You must provide an instance of the Doctrine entity manager, and an associative array where the key is the plural form of the entity's name, and the value is the fully-qualified class name of the entity definition. For example:

```
$builder = new EntitySchemaBuilder($em, [
    'owners' => Owner::class,
    'dogs'  => Dog::class,
    'cats'  => Cat::class,
]);
```

### Running queries

[](#running-queries)

You may use your built schema in a GraphQL server of your choice, or use the helper’s integration with `graphql-php` to retrieve a server object already set up with your schema by calling `getServer()`.

The server returned accepts a request object in its `executeRequest()` method. In some cases you may wish to run a raw JSON payload through the server. To do this, can parse the JSON to a format which the server will accept as a parameter to `executeRequest()` by calling `EntitySchemaBuilder::jsonToOperation($json)`.

### Using permissions

[](#using-permissions)

Permissions are managed using the handler you implemented when extending `GraphQLEntity`. The `hasPermission()` handler is passed 4 parameters to help you implement this:

```
abstract public function hasPermission(
    EntityManager $em, // an instance of the entity manager
    DoctrineUniqueInterface $user, // the user you passed to getServer()
    array $context, // array of additional context you optionally passed to getServer()
    string $method // action method verb
): bool;
```

The `method` corresponds to the action verb assigned to the currently executing query or mutation. The generated queries and mutators use pre-set CRUD-like verbs: `get`, `create`, `update` and `delete`, but you can use any verb you choose when writing your own mutators.

Using custom mutators
---------------------

[](#using-custom-mutators)

The schema generator exposes a simple API for adding your own mutators, and a class (`Mutation`). This wraps some advanced functionality of graphql-doctrine, and so reference to that package’s documentation may or will be required using this feature. You must instantiate `Mutation` by passing the name of the mutator to the factory method of an `EntitySchemaBuilder`. This associates the `Mutation` with the builder and will include it in any schema generated with it.

There are two methods of hydrating the new `Mutation` returned by the factory: using the chainable methods exposed by `Mutation`, or by providing all parameters at once to its `hydrate()` method. The examples below use method chaining, but the same principles apply to `hydrate()`.

**`setEntity()`:** Set the class name of the entity that this mutator operates on. This must be set, and is used to auto-generate the type if none was provided.

**`setType()`:** Set the GraphQL return type of the mutator. If not configured, this will be a non-empty list of the entity provided in `setEntity()`. The default type is compatible with the builder’s `resolveQuery()` method, if called in your resolver function (see below).

**`setDescription()`:** Set a description returned by the server in introspection queries (optional).

**`setArgs()`:** Set the arguments that may be given when this mutator is queried. By default, this is a non-null (required) ID type, allowing you to retrieve the entity that the ID refers to using the entity manager.

**`setMethod()`:** Set the action verb that this mutator uses for permissions purposes.

**`setResolver()`:** You must provide a callable here that takes two arguments – an array of your mutator’s args and the API user making the request. You must return the data that you wish to be returned with the response to the query, and that data must of the correct type – methods that can assist with this are provided in `EntitySchemaBuilder`, and it is suggested that you define this callable in a variable scope where you have access to it and the entity manager. Failure to resolve data of the correct type will result in the server returning an error.

Methods exposed by the builder
------------------------------

[](#methods-exposed-by-the-builder)

The schema builder exposes a variety of methods which may be of use when writing resolver functions. You may wish to consult the documentation of `graphql-php` and `graphql-doctrine` for more information on the values that some of these methods return.

**`listOfType()`:** When given an entity’s class name, returns a GraphQL return type of a list of the entity.

**`resolveQuery()`:** Resolves a query using the entity manager. Requires the args array given with the query, and the class name of the root entity being queried. You may also pass in an instance of the entity as the third parameter to fully resolve and then return this entity.

**`getMutator()`:** Generates a mutator (in its array form, not a `Mutation`) from the provided type, args and resolver.

**`setUserEntity()`:** Changes the user from that given during instantiation.

**`getTypes()`:** Retrieve the types that have been generated for use in the schema.

**`isPermitted()`:** Resolves the permission level of a query, given its args, query context and entity class name.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~75 days

Recently: every ~105 days

Total

7

Last Release

2137d ago

Major Versions

v0.1 → v1.02019-04-30

v1.0.2 → v2.0.02019-11-27

v2.0.1 → v3.0.02020-07-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/9db8acda212740c63090beab8438300c1e158e824a9642fa49babef315136c4c?d=identicon)[guym4c](/maintainers/guym4c)

---

Top Contributors

[![guym4c](https://avatars.githubusercontent.com/u/2103489?v=4)](https://github.com/guym4c "guym4c (56 commits)")

---

Tags

graphqldoctrine

### Embed Badge

![Health badge](/badges/guym4c-doctrine-graphql-helper/health.svg)

```
[![Health](https://phpackages.com/badges/guym4c-doctrine-graphql-helper/health.svg)](https://phpackages.com/packages/guym4c-doctrine-graphql-helper)
```

###  Alternatives

[doctrine/common

PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, proxies and much more.

5.8k363.3M1.2k](/packages/doctrine-common)[gedmo/doctrine-extensions

Doctrine behavioral extensions

4.1k118.8M366](/packages/gedmo-doctrine-extensions)[symfony/property-info

Extracts information about PHP class' properties using metadata of popular sources

2.2k256.7M854](/packages/symfony-property-info)[beberlei/doctrineextensions

A set of extensions to Doctrine 2 that add support for additional query functions available in MySQL, Oracle, PostgreSQL and SQLite.

2.1k75.1M146](/packages/beberlei-doctrineextensions)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[carbonphp/carbon-doctrine-types

Types to use Carbon in Doctrine

213220.4M8](/packages/carbonphp-carbon-doctrine-types)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
