Skip to content

Commit 06492d5

Browse files
committed
Optimization: we are not recreating fields in child class that are part of parent class
The FieldsBuilder is not stopping analyzing annotations if the annotations are part of a parent type.
1 parent c1da565 commit 06492d5

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

src/FieldsBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TheCodingMachine\GraphQLite;
55

66
use function array_merge;
7+
use function get_parent_class;
78
use GraphQL\Type\Definition\InputType;
89
use GraphQL\Type\Definition\ListOfType;
910
use GraphQL\Type\Definition\NonNull;
@@ -15,6 +16,7 @@
1516
use phpDocumentor\Reflection\Types\Self_;
1617
use Psr\Http\Message\UploadedFileInterface;
1718
use ReflectionMethod;
19+
use TheCodingMachine\GraphQLite\Annotations\Field;
1820
use TheCodingMachine\GraphQLite\Annotations\SourceFieldInterface;
1921
use TheCodingMachine\GraphQLite\Hydrators\HydratorInterface;
2022
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface;
@@ -222,7 +224,21 @@ private function getFieldsByAnnotations($controller, string $annotationName, boo
222224
$oldDeclaringClass = null;
223225
$context = null;
224226

227+
$closestMatchingTypeClass = null;
228+
if ($annotationName === Field::class) {
229+
$parent = get_parent_class($refClass->getName());
230+
if ($parent !== null) {
231+
$closestMatchingTypeClass = $this->typeMapper->findClosestMatchingParent($parent);
232+
}
233+
}
234+
225235
foreach ($refClass->getMethods() as $refMethod) {
236+
if ($closestMatchingTypeClass !== null && $closestMatchingTypeClass === $refMethod->getDeclaringClass()->getName()) {
237+
// Optimisation: no need to fetch annotations from parent classes that are ALREADY GraphQL types.
238+
// We will merge the fields anyway.
239+
break;
240+
}
241+
226242
// First, let's check the "Query" or "Mutation" or "Field" annotation
227243
$queryAnnotation = $this->annotationReader->getRequestAnnotation($refMethod, $annotationName);
228244

src/Mappers/RecursiveTypeMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function mapClassToType(string $className, ?OutputType $subType): Mutable
148148
* @param string $className
149149
* @return string|null
150150
*/
151-
private function findClosestMatchingParent(string $className): ?string
151+
public function findClosestMatchingParent(string $className): ?string
152152
{
153153
do {
154154
if ($this->typeMapper->canMapClassToType($className)) {

src/Mappers/RecursiveTypeMapperInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,12 @@ public function canMapNameToType(string $typeName): bool;
9494
* @return Type&(InputType|OutputType)
9595
*/
9696
public function mapNameToType(string $typeName): Type;
97+
98+
/**
99+
* Returns the closest parent that can be mapped, or null if nothing can be matched.
100+
*
101+
* @param string $className
102+
* @return string|null
103+
*/
104+
public function findClosestMatchingParent(string $className): ?string;
97105
}

src/Types/TypeAnnotatedObjectType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static function createFromAnnotatedClass(string $typeName, string $classN
4343
$fields = $fieldProvider->getSelfFields($className);
4444
}
4545
if ($parentType !== null) {
46+
// Note: with +=, the keys already present are not overloaded
4647
$fields += $parentType->getFields();
4748
}
4849
return $fields;

tests/Fixtures/Integration/Models/Contact.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function setBirthDate(DateTimeInterface $birthDate): void
114114
}
115115

116116
/**
117-
* Here, we are testing overriding the field in the extend class.
117+
* This getter will be overridden in the extend class.
118118
*
119119
* @Field()
120120
* @return string

0 commit comments

Comments
 (0)