Skip to content

Commit 7d13cde

Browse files
committed
Minor API & docs changes
1 parent 0b336a9 commit 7d13cde

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# doctrine-graphql-helper
22

3-
Easily create a GraphQL schema from Doctrine entities.
3+
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.
44

55
## Install
66
Via Composer:
@@ -9,7 +9,7 @@ composer install guym4c/doctrine-graphql-helper
99
```
1010

1111
## Usage
12-
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-compatible. Refer to these packages’ documentation for more details.
12+
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`-compatible. Refer to these packages’ documentation for more details.
1313

1414
### `GraphQLEntity`
1515
Any entities which you wish to use in your API must extend `GraphQLEntity` and implement the required methods.
@@ -46,6 +46,8 @@ If you wish to use permissions, you may also provide the EntitySchemaBuilder con
4646
### Running queries
4747
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 and any permissions settings you have defined by calling `getServer()`.
4848

49+
The server object returned accepts a request object in its `executeRequest()` method. In some cases you may wish to run a JSON payload through the server - to do this you can parse the JSON to a format which the server will accept as a parameter to `executeRequest()` by calling `EntitySchemaBuilder::jsonToOperation($json)`.
50+
4951
### Using permissions
5052
If you have set the schema builder’s permissions during instantiation, provide the permitted scopes (as an array) and the user’s identifier to the `getServer()` method to execute the query with permissions enabled.
5153
The schema generator generates four queries for each provided entity, which have parallels to the HTTP request methods used in REST: a simple `GET` query, and `POST` (create), `UPDATE` and `DELETE` mutators. You may define the permissions at method-level granularity using the scopes array, provided to the builder’s constructor.
@@ -85,7 +87,7 @@ An asterisk (\*) is a wildcard, indicating full permissions are given for this s
8587
* **All:** Accessible to all users with this scope
8688
* **None:** Not accessible to users with this scope
8789
* **Permissive:** Users permissions with this scope are resolved in the entity’s `hasPermission()` method. If you don’t wish to use permissive, but are running the server with permissions enabled, simply implement the method with a return true.
88-
The `hasPermission()` static is called for all methods that are defined as permissive, and you are passed an instance of the Doctrine entity manager, an instance of your API user class as `ApiUserInterface`, and the ID of the entity that is being queried by the user. You are not given an instantiated version of the entity class being called: if you wish, you must retrieve this from the entity manager manually using the provided entity ID.
90+
The `hasPermission()` method is called for all methods that are defined as permissive, and you are passed an instance of the Doctrine entity manager and an instance of your API user class as `ApiUserInterface`.
8991

9092
## Using custom mutators
9193
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.
@@ -108,7 +110,7 @@ There are two methods of hydrating the new `Mutation` returned by the factory: u
108110
## Methods exposed by the builder
109111
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.
110112

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

113115
**`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.
114116

src/ApiUserInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
namespace GraphQL\Doctrine\Helper;
44

5-
interface ApiUserInterface extends DoctrineUniqueInterface {
6-
7-
}
5+
interface ApiUserInterface extends DoctrineUniqueInterface {}

src/EntitySchemaBuilder.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function build(array $entities): self {
9494
'mutation' => new ObjectType([
9595
'name' => 'mutation',
9696
'fields' => array_merge(
97-
$this->getAllMutators(array_values($entities)),
97+
$this->generateMutatorsForEntities(array_values($entities)),
9898
$parsedMutators),
9999
]),
100100
]);
@@ -111,7 +111,7 @@ public function build(array $entities): self {
111111
private function getAllQueries(array $entities) {
112112
$queries = [];
113113
foreach ($entities as $key => $entity) {
114-
$queries[$key] = $this->listOf($entity);
114+
$queries[$key] = $this->listOfQuery($entity);
115115
}
116116
return $queries;
117117
}
@@ -127,9 +127,9 @@ private function getAllQueries(array $entities) {
127127
*
128128
* @return array The field entry within ObjectType.fields
129129
*/
130-
private function listOf(string $entity): array {
130+
private function listOfQuery(string $entity): array {
131131
return [
132-
'type' => Type::listOf($this->types->getOutput($entity)),
132+
'type' => $this->listOfType($entity),
133133
'args' => [
134134
[
135135
'name' => 'filter',
@@ -161,7 +161,7 @@ private function listOf(string $entity): array {
161161
];
162162
}
163163

164-
public function immutableListOf(string $entity): Type {
164+
public function listOfType(string $entity): Type {
165165
return Type::listOf($this->types->getOutput($entity));
166166
}
167167

@@ -218,7 +218,7 @@ public function resolveQuery(array $args, string $entity, ?DoctrineUniqueInterfa
218218
* @param string $entity The entity type class name that the mutators should act upon.
219219
* @return array A list of mutator types
220220
*/
221-
private function getMutators(string $entity): array {
221+
private function generateMutatorsForEntity(string $entity): array {
222222

223223
try {
224224
$entityName = (new ReflectionClass($entity))->getShortName();
@@ -254,10 +254,10 @@ private function getMutators(string $entity): array {
254254
* @return array A list of mutator types
255255
* @see self::getMutators()
256256
*/
257-
private function getAllMutators(array $entities): array {
257+
private function generateMutatorsForEntities(array $entities): array {
258258
$mutators = [];
259259
foreach ($entities as $entity) {
260-
$mutators = array_merge($mutators, $this->getMutators($entity));
260+
$mutators = array_merge($mutators, $this->generateMutatorsForEntity($entity));
261261
}
262262
return $mutators;
263263
}
@@ -310,9 +310,12 @@ public function isPermitted(array $args, array $context, string $entity, string
310310
break;
311311

312312
case PermissionLevel::PERMISSIVE:
313-
$permitted = call_user_func($entity . '::hasPermission', $this->em,
314-
$this->em->getRepository($this->userEntity)->find($context['user']),
315-
$this->em->getRepository($entity)->find($args['id']));
313+
/** @var GraphQLEntity $entityObject */
314+
$entityObject = $this->em->getRepository($entity)->find($args['id']);
315+
/** @var ApiUserInterface $user */
316+
$user = $this->em->getRepository($this->userEntity)->find($context['user']);
317+
318+
$permitted = $entityObject->hasPermission($this->em, $user);
316319
break;
317320

318321
case PermissionLevel::NONE:
@@ -340,9 +343,7 @@ public function isPermitted(array $args, array $context, string $entity, string
340343
*/
341344
private function mutationResolver(array $args, array $context, string $entity, string $method) {
342345

343-
$permitted = $this->isPermitted($args, $context, $entity, $method);
344-
345-
if (!$permitted) {
346+
if (!$this->isPermitted($args, $context, $entity, $method)) {
346347
return [403];
347348
}
348349

@@ -464,10 +465,10 @@ public function mutation(string $name): Mutation {
464465
return $mutation;
465466
}
466467

467-
public static function createServerOperation(array $json): OperationParams {
468+
public static function jsonToOperation(array $json): OperationParams {
468469
return OperationParams::create([
469470
'query' => $json['query'],
470-
'variables' => $json['variabes'] ?? null,
471+
'variables' => $json['variables'] ?? null,
471472
]);
472473
}
473474
}

src/GraphQLEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function buildFromJson(EntityManager $em, array $input) {
6868
return $entity;
6969
}
7070

71-
public static function hasPermission(EntityManager $em, ApiUserInterface $user, string $entityId): bool {
71+
public function hasPermission(EntityManager $em, ApiUserInterface $user): bool {
7272
return true;
7373
}
7474

0 commit comments

Comments
 (0)