Skip to content

Optimization: we are not recreating fields in child class that are part of parent class #39

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 3 commits into from
Apr 10, 2019
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
16 changes: 16 additions & 0 deletions src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TheCodingMachine\GraphQLite;

use function array_merge;
use function get_parent_class;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
Expand All @@ -15,6 +16,7 @@
use phpDocumentor\Reflection\Types\Self_;
use Psr\Http\Message\UploadedFileInterface;
use ReflectionMethod;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\SourceFieldInterface;
use TheCodingMachine\GraphQLite\Hydrators\HydratorInterface;
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeExceptionInterface;
Expand Down Expand Up @@ -222,7 +224,21 @@ private function getFieldsByAnnotations($controller, string $annotationName, boo
$oldDeclaringClass = null;
$context = null;

$closestMatchingTypeClass = null;
if ($annotationName === Field::class) {
$parent = get_parent_class($refClass->getName());
if ($parent !== null) {
$closestMatchingTypeClass = $this->typeMapper->findClosestMatchingParent($parent);
}
}

foreach ($refClass->getMethods() as $refMethod) {
if ($closestMatchingTypeClass !== null && $closestMatchingTypeClass === $refMethod->getDeclaringClass()->getName()) {
// Optimisation: no need to fetch annotations from parent classes that are ALREADY GraphQL types.
// We will merge the fields anyway.
break;
}

// First, let's check the "Query" or "Mutation" or "Field" annotation
$queryAnnotation = $this->annotationReader->getRequestAnnotation($refMethod, $annotationName);

Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/RecursiveTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function mapClassToType(string $className, ?OutputType $subType): Mutable
* @param string $className
* @return string|null
*/
private function findClosestMatchingParent(string $className): ?string
public function findClosestMatchingParent(string $className): ?string
{
do {
if ($this->typeMapper->canMapClassToType($className)) {
Expand Down
8 changes: 8 additions & 0 deletions src/Mappers/RecursiveTypeMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ public function canMapNameToType(string $typeName): bool;
* @return Type&(InputType|OutputType)
*/
public function mapNameToType(string $typeName): Type;

/**
* Returns the closest parent that can be mapped, or null if nothing can be matched.
*
* @param string $className
* @return string|null
*/
public function findClosestMatchingParent(string $className): ?string;
}
1 change: 1 addition & 0 deletions src/Types/TypeAnnotatedObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function createFromAnnotatedClass(string $typeName, string $classN
$fields = $fieldProvider->getSelfFields($className);
}
if ($parentType !== null) {
// Note: with +=, the keys already present are not overloaded
$fields += $parentType->getFields();
}
return $fields;
Expand Down
24 changes: 24 additions & 0 deletions tests/Fixtures/Integration/Models/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTimeInterface;
use Psr\Http\Message\UploadedFileInterface;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

/**
Expand Down Expand Up @@ -33,6 +34,10 @@ class Contact
* @var DateTimeInterface
*/
private $birthDate;
/**
* @var string
*/
private $company;

public function __construct(string $name)
{
Expand Down Expand Up @@ -107,4 +112,23 @@ public function setBirthDate(DateTimeInterface $birthDate): void
{
$this->birthDate = $birthDate;
}

/**
* This getter will be overridden in the extend class.
*
* @Field()
* @return string
*/
public function getCompany(): string
{
return $this->company;
}

/**
* @param string $company
*/
public function setCompany(string $company): void
{
$this->company = $company;
}
}
13 changes: 12 additions & 1 deletion tests/Fixtures/Integration/Types/ExtendedContactType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ public function uppercaseName(Contact $contact): string
{
return strtoupper($contact->getName());
}
}

/**
* Here, we are testing overriding the field in the extend class.
*
* @Field()
* @return string
*/
public function company(Contact $contact): string
{
return $contact->getName().' Ltd';
}
}
5 changes: 5 additions & 0 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public function testEndToEnd()
query {
contacts {
name
company
uppercaseName
... on User {
email
Expand All @@ -216,10 +217,12 @@ public function testEndToEnd()
'contacts' => [
[
'name' => 'Joe',
'company' => 'Joe Ltd',
'uppercaseName' => 'JOE'
],
[
'name' => 'Bill',
'company' => 'Bill Ltd',
'uppercaseName' => 'BILL',
'email' => '[email protected]'
]
Expand All @@ -237,10 +240,12 @@ public function testEndToEnd()
'contacts' => [
[
'name' => 'Joe',
'company' => 'Joe Ltd',
'uppercaseName' => 'JOE'
],
[
'name' => 'Bill',
'company' => 'Bill Ltd',
'uppercaseName' => 'BILL',
'email' => '[email protected]'
]
Expand Down