Skip to content

Annotations refactoring #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/annotations/annotations-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ This annotation applies on methods for classes tagged with the `@Provider` annot
The resulting field is added to the root Mutation type (defined in configuration at key `overblog_graphql.definitions.schema.mutation`).
The class exposing the mutation(s) must be declared as a [service](https://symfony.com/doc/current/service_container.html).

Optional attributes:

- **targetType** : The GraphQL type to attach the field to. It must be a mutation. (by default, it'll be the root Mutation type of the default schema).

Example:

This will add an `updateUserEmail` mutation, with as resolver `@=service('App\Graphql\MutationProvider').updateUserEmail(...)`.
Expand Down Expand Up @@ -432,7 +436,7 @@ The class exposing the query(ies) must be declared as a [service](https://symfon

Optional attributes:

- **targetType** : The GraphQL type to attach the field to (by default, it'll be the root Query type).
- **targetType** : The GraphQL type to attach the field to (by default, it'll be the root Query type of the default schema).

Example:

Expand Down Expand Up @@ -464,9 +468,10 @@ This annotation is used on _class_ to define a GraphQL Type.
Optional attributes:

- **name** : The GraphQL name of the type (default to the class name without namespace)
- **interfaces** : An array of GraphQL interface this type inherits from
- **interfaces** : An array of GraphQL interface this type inherits from (can be auto-guessed. See interface documentation).
- **isRelay** : Set to true to have a Relay compatible type (ie. A `clientMutationId` will be added).
- **builders**: An array of `@FieldsBuilder` annotations
- **builders** : An array of `@FieldsBuilder` annotations
- **isTypeOf** : Is type of resolver for interface implementation

```php
<?php
Expand Down Expand Up @@ -554,7 +559,7 @@ This annotation is used on a _class_ to define an union.

Required attributes:

- **types** : An array of GraphQL Type as string
- **types** : An array of GraphQL Type as string (can be auto-guessed. See union documenation).

Optional attributes:

Expand Down
2 changes: 2 additions & 0 deletions docs/definitions/type-system/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Droid:

## With Annotations

Note: With annotations, you can omit the `interfaces` option. If so, the system will try to guess the interfaces automatically by getting the GraphQL Interface associated with classes that the Class type extends or implements.

```php
<?php

Expand Down
2 changes: 2 additions & 0 deletions docs/definitions/type-system/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ HumanAndDroid:

## With annotations

Note: With annotations, you can omit the `types` parameter. If so, the system will try to detect GraphQL Type associated with classes that inherit or implement the Union class.

```php
<?php

Expand Down
6 changes: 6 additions & 0 deletions src/Annotation/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
*/
final class Mutation extends Field
{
/**
* The target type to attach this mutation to (usefull when multiple schemas are allowed).
*
* @var string
*/
public $targetType;
}
7 changes: 7 additions & 0 deletions src/Annotation/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ class Type implements Annotation
* @var array<\Overblog\GraphQLBundle\Annotation\FieldsBuilder>
*/
public $builders = [];

/**
* Expression to resolve type for interfaces.
*
* @var string
*/
public $isTypeOf;
}
2 changes: 0 additions & 2 deletions src/Annotation/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ final class Union implements Annotation
/**
* Union types.
*
* @required
*
* @var array<string>
*/
public $types;
Expand Down
90 changes: 90 additions & 0 deletions src/Config/Parser/Annotation/GraphClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Config\Parser\Annotation;

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use RuntimeException;
use function class_exists;

class GraphClass extends ReflectionClass
{
private static ?AnnotationReader $annotationReader = null;

protected array $annotations = [];

protected array $propertiesExtended = [];

/**
* @param mixed $className
*/
public function __construct($className)
{
parent::__construct($className);

$annotationReader = self::getAnnotationReader();
$this->annotations = $annotationReader->getClassAnnotations($this);

$reflection = $this;
do {
foreach ($reflection->getProperties() as $property) {
if (isset($this->propertiesExtended[$property->getName()])) {
continue;
}
$this->propertiesExtended[$property->getName()] = $property;
}
} while ($reflection = $reflection->getParentClass());
}

/**
* @return ReflectionProperty[]
*/
public function getPropertiesExtended()
{
return $this->propertiesExtended;
}

/**
* @param ReflectionMethod|ReflectionProperty|null $from
*
* @return array
*/
public function getAnnotations(object $from = null)
{
if (!$from) {
return $this->annotations;
}

if ($from instanceof ReflectionMethod) {
return self::getAnnotationReader()->getMethodAnnotations($from);
}

if ($from instanceof ReflectionProperty) {
return self::getAnnotationReader()->getPropertyAnnotations($from);
}

/** @phpstan-ignore-next-line */
throw new AnnotationException(sprintf('Unable to retrieve annotations from object of class "%s".', get_class($from)));
}

private static function getAnnotationReader(): AnnotationReader
{
if (null === self::$annotationReader) {
if (!class_exists(AnnotationReader::class) ||
!class_exists(AnnotationRegistry::class)) {
throw new RuntimeException('In order to use graphql annotation, you need to require doctrine annotations');
}

AnnotationRegistry::registerLoader('class_exists');
self::$annotationReader = new AnnotationReader();
}

return self::$annotationReader;
}
}
Loading