Skip to content

Commit

Permalink
Merge pull request #105 from moufmouf/external_disable_inheritance
Browse files Browse the repository at this point in the history
Adding 'external' and 'disableInheritance' attributes to @type
  • Loading branch information
moufmouf committed Jul 8, 2019
2 parents 71a1f49 + 1236aae commit 53e5c67
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
3 changes: 2 additions & 1 deletion docs/annotations_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Attribute | Compulsory | Type | Definition
class | *no* | string | The targeted class. If no class is passed, the type applies to the current class. The current class is assumed to be an entity. If the "class" attribute is passed, [the class annotated with `@Type` is a service](external_type_declaration.md).
name | *no* | string | The name of the GraphQL type generated. If not passed, the name of the class is used. If the class ends with "Type", the "Type" suffix is removed
default | *no* | bool | Defaults to *true*. Whether the targeted PHP class should be mapped by default to this type.

external | *no* | bool | Whether this is an [external type declaration](external_type_declaration.md) or not. You usually do not need to use this attribute since this value defaults to true if a "class" attribute is set. This is only useful if you are declaring a type with no PHP class mapping using the "name" attribute.
disableInheritance | *no* | bool | Out of the box, it a PHP class who is a type extends another PHP class who is a type, GraphQL inheritance is put in place. Use this attribute to remove the default behaviour. The type will not map any fields from the parent class AND will not implement the interface.

## @ExtendType annotation

Expand Down
24 changes: 23 additions & 1 deletion src/Annotations/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
* @Attributes({
* @Attribute("class", type = "string"),
* @Attribute("name", type = "string"),
* @Attribute("default", type = "bool")
* @Attribute("default", type = "bool"),
* @Attribute("external", type = "bool"),
* @Attribute("disableInheritance", type = "bool")
* })
*/
class Type
Expand All @@ -32,6 +34,9 @@ class Type
/** @var bool */
private $default;

/** @var bool */
private $disableInheritance;

/**
* Is the class having the annotation a GraphQL type itself?
*
Expand All @@ -44,6 +49,7 @@ class Type
*/
public function __construct(array $attributes = [])
{
$external = $attributes['external'] ?? null;
if (isset($attributes['class'])) {
$this->setClass($attributes['class']);
} else {
Expand All @@ -54,6 +60,14 @@ public function __construct(array $attributes = [])

// If no value is passed for default, "default" = true
$this->default = $attributes['default'] ?? true;

$this->disableInheritance = $attributes['disableInheritance'] ?? false;

if ($external === null) {
return;
}

$this->selfType = ! $external;
}

/**
Expand Down Expand Up @@ -96,4 +110,12 @@ public function isDefault(): bool
{
return $this->default;
}

/**
* Returns true if the parent type fields should be ignored.
*/
public function isInheritanceDisabled(): bool
{
return $this->disableInheritance;
}
}
2 changes: 1 addition & 1 deletion src/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function mapAnnotatedObject(string $annotatedObjectClassName): MutableObj
$annotatedObject = null;
}

return TypeAnnotatedObjectType::createFromAnnotatedClass($typeName, $typeField->getClass(), $annotatedObject, $this->fieldsBuilder, $this->recursiveTypeMapper, ! $typeField->isDefault());
return TypeAnnotatedObjectType::createFromAnnotatedClass($typeName, $typeField->getClass(), $annotatedObject, $this->fieldsBuilder, $this->recursiveTypeMapper, ! $typeField->isDefault(), $typeField->isInheritanceDisabled());

/*return new ObjectType([
'name' => $typeName,
Expand Down
10 changes: 5 additions & 5 deletions src/Types/TypeAnnotatedObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public function __construct(string $className, array $config)
parent::__construct($config, $className);
}

public static function createFromAnnotatedClass(string $typeName, string $className, ?object $annotatedObject, FieldsBuilder $fieldsBuilder, RecursiveTypeMapperInterface $recursiveTypeMapper, bool $doNotMapInterfaces): self
public static function createFromAnnotatedClass(string $typeName, string $className, ?object $annotatedObject, FieldsBuilder $fieldsBuilder, RecursiveTypeMapperInterface $recursiveTypeMapper, bool $doNotMapInterfaces, bool $disableInheritance): self
{
return new self($className, [
'name' => $typeName,
'fields' => static function () use ($annotatedObject, $recursiveTypeMapper, $className, $fieldsBuilder) {
'fields' => static function () use ($annotatedObject, $recursiveTypeMapper, $className, $fieldsBuilder, $disableInheritance) {
$parentClass = get_parent_class($className);
$parentType = null;
if ($parentClass !== false) {
if ($parentClass !== false && $disableInheritance === false) {
if ($recursiveTypeMapper->canMapClassToType($parentClass)) {
$parentType = $recursiveTypeMapper->mapClassToType($parentClass, null);
}
Expand All @@ -50,8 +50,8 @@ public static function createFromAnnotatedClass(string $typeName, string $classN

return $fields;
},
'interfaces' => static function () use ($className, $recursiveTypeMapper, $doNotMapInterfaces) {
if ($doNotMapInterfaces === true) {
'interfaces' => static function () use ($className, $recursiveTypeMapper, $doNotMapInterfaces, $disableInheritance) {
if ($doNotMapInterfaces === true || $disableInheritance === true) {
return [];
}

Expand Down
8 changes: 7 additions & 1 deletion tests/Annotations/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

class TypeTest extends TestCase
{
public function testException()
public function testException(): void
{
$type = new Type([]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Empty class for @Type annotation. You MUST create the Type annotation object using the GraphQLite AnnotationReader');
$type->getClass();
}

public function testExternal(): void
{
$type = new Type(['external'=>true]);
$this->assertSame(false, $type->isSelfType());
}
}

0 comments on commit 53e5c67

Please sign in to comment.