Skip to content

Commit

Permalink
Merge pull request API-Skeletons#82 from TomHAnderson/feature/beteen-…
Browse files Browse the repository at this point in the history
…names

Name between filters after the type name to  avoid many generated Bet…
  • Loading branch information
TomHAnderson authored Mar 26, 2024
2 parents b6d9f95 + 092402e commit f7ce308
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Filter/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static function ($value) use ($fieldExcludeFilters) {

$fields[$fieldName] = [
'name' => $fieldName,
'type' => new Field($typeName, $fieldName, $graphQLType, $allowedFilters),
'type' => new Field($this->typeManager, $typeName, $fieldName, $graphQLType, $allowedFilters),
'description' => 'Filters for ' . $fieldName,
];
}
Expand Down Expand Up @@ -179,7 +179,7 @@ protected function addAssociations(Entity $targetEntity, string $typeName, array
// eq filter is for association id from parent entity
$fields[$associationName] = [
'name' => $associationName,
'type' => new Association($typeName, $associationName, Type::id(), [Filters::EQ]),
'type' => new Association($this->typeManager, $typeName, $associationName, Type::id(), [Filters::EQ]),
'description' => 'Filters for ' . $associationName,
];
}
Expand Down
4 changes: 3 additions & 1 deletion src/Filter/InputObjectType/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class Between extends InputObjectType
{
public function __construct(ScalarType|ListOfType $type)
{
$name = $type instanceof ScalarType ? $type->name() : uniqid();

parent::__construct([
'name' => 'Between_' . uniqid(),
'name' => 'Between_' . $name,
'description' => 'Between `from` and `to`',
'fields' => [
'from' => new InputObjectField([
Expand Down
21 changes: 21 additions & 0 deletions src/Filter/InputObjectType/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ApiSkeletons\Doctrine\ORM\GraphQL\Filter\InputObjectType;

use ApiSkeletons\Doctrine\ORM\GraphQL\Filter\Filters;
use ApiSkeletons\Doctrine\ORM\GraphQL\Type\TypeManager;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\ScalarType;
Expand All @@ -17,6 +18,7 @@ class Field extends InputObjectType
{
/** @param Filters[] $allowedFilters */
public function __construct(
TypeManager $typeManager,
string $typeName,
string $fieldName,
ScalarType|ListOfType $type,
Expand All @@ -31,6 +33,25 @@ public function __construct(
'type' => $filter->type($type),
'description' => $filter->description(),
];

if (! $type instanceof ScalarType) {
continue;
}

if (! ($fields[$filter->value]['type'] instanceof Between)) {
continue;
}

// Between is a special case filter.
// To avoid creating a new Between type for each field
// we check if the Between type exists and reuse it.
if ($typeManager->has('Between_' . $type->name)) {
$fields[$filter->value]['type'] = $typeManager->get('Between_' . $type->name);
} else {
$betweenType = new Between($type);
$typeManager->set('Between_' . $type->name, $betweenType);
$fields[$filter->value]['type'] = $betweenType;
}
}

parent::__construct([
Expand Down

0 comments on commit f7ce308

Please sign in to comment.