Skip to content

Commit

Permalink
fix(graphql/@sortBy)!: NullsFirst/NullsLast operators will chec…
Browse files Browse the repository at this point in the history
…k that Nulls supported.

Issue: #110
  • Loading branch information
LastDragon-ru committed Feb 3, 2024
1 parent abef051 commit c6dcd94
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 58 deletions.
17 changes: 12 additions & 5 deletions packages/graphql/src/Builder/Traits/BuilderHelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,22 @@ private function addHelper(string $builder, string $helper): static {
return $this;
}

private function getHelper(object $builder): ?object {
if (!array_key_exists($builder::class, $this->instances)) {
$class = $this->getHelperClass($builder::class);
$this->instances[$builder::class] = $class
/**
* @param object|class-string $builder
*/
private function getHelper(object|string $builder): ?object {
if (is_object($builder)) {
$builder = $builder::class;
}

if (!array_key_exists($builder, $this->instances)) {
$class = $this->getHelperClass($builder);
$this->instances[$builder] = $class
? Container::getInstance()->make($class)
: null;
}

return $this->instances[$builder::class];
return $this->instances[$builder];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/SortBy/Contracts/SorterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ interface SorterFactory {
public function isSupported(object|string $builder): bool;

/**
* @param TBuilder $builder
* @param TBuilder|class-string<TBuilder> $builder
*
* @return ?Sorter<TBuilder>
*/
public function create(object $builder): ?Sorter;
public function create(object|string $builder): ?Sorter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,18 +516,6 @@ input SortByScoutClauseNested {
nested: SortByScoutClauseNested
@sortByOperatorFieldObject

"""
NULLs first
"""
nullsFirst: SortByScoutClauseNested
@sortByOperatorNullsFirst

"""
NULLs last
"""
nullsLast: SortByScoutClauseNested
@sortByOperatorNullsLast

"""
Field clause.
"""
Expand Down Expand Up @@ -608,18 +596,6 @@ input SortByScoutClauseProperties {
nestedNotNull: SortByScoutClauseNested
@sortByOperatorFieldObject

"""
NULLs first
"""
nullsFirst: SortByScoutClauseProperties
@sortByOperatorNullsFirst

"""
NULLs last
"""
nullsLast: SortByScoutClauseProperties
@sortByOperatorNullsLast

"""
Field clause.
"""
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/SortBy/Operators/Extra/NullsFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getFieldDescription(): string {

#[Override]
public function isAvailable(string $builder, Context $context): bool {
return $this->factory->isSupported($builder);
return (bool) $this->factory->create($builder)?->isNullsSupported();
}

#[Override]
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/SortBy/Operators/Extra/NullsLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getFieldDescription(): string {

#[Override]
public function isAvailable(string $builder, Context $context): bool {
return $this->factory->isSupported($builder);
return (bool) $this->factory->create($builder)?->isNullsSupported();
}

#[Override]
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/SortBy/SorterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function isSupported(string|object $builder): bool {
}

#[Override]
public function create(object $builder): ?SorterContract {
public function create(object|string $builder): ?SorterContract {
$helper = $this->getHelper($builder);

assert($helper instanceof SorterContract);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ on
| INPUT_FIELD_DEFINITION
| SCALAR

directive @sortByOperatorNullsFirst
on
| ENUM
| INPUT_FIELD_DEFINITION
| SCALAR

directive @sortByOperatorNullsLast
on
| ENUM
| INPUT_FIELD_DEFINITION
| SCALAR

"""
Splits list of items into the chunks and returns one chunk specified
by an offset or a cursor.
Expand Down Expand Up @@ -151,18 +139,6 @@ input SortByScoutClauseTestObject {
"""
id: SortByTypeDirection
@sortByOperatorField

"""
NULLs first
"""
nullsFirst: SortByScoutClauseTestObject
@sortByOperatorNullsFirst

"""
NULLs last
"""
nullsLast: SortByScoutClauseTestObject
@sortByOperatorNullsLast
}

"""
Expand Down

0 comments on commit c6dcd94

Please sign in to comment.