Skip to content

Commit

Permalink
feat(graphql/@sortBy)!: Ignored will not extend `Nuwave\Lighthous…
Browse files Browse the repository at this point in the history
…e\Support\Contracts\Directive` thus it can be used with any `Type`.
  • Loading branch information
LastDragon-ru committed Dec 24, 2022
1 parent 51a2429 commit 4fda12d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ As you can see in the example above you can use the special placeholder `_` inst
- with `@field` directive
- with `@sortByIgnored` directive
- with any directive that implements [`Ignored`](./src/SortBy/Contracts/Ignored.php)
- any `Type` that implements [`Ignored`](./src/SortBy/Contracts/Ignored.php)

# Scout

Expand Down
6 changes: 2 additions & 4 deletions packages/graphql/src/SortBy/Contracts/Ignored.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts;

use Nuwave\Lighthouse\Support\Contracts\Directive;

/**
* Marks that field should be excluded from sort.
* Marks that field/type should be excluded from sort.
*/
interface Ignored extends Directive {
interface Ignored {
// empty
}
17 changes: 17 additions & 0 deletions packages/graphql/src/SortBy/Directives/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\Client\ConditionTooManyProperties;
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\TypeDefinitionImpossibleToCreateType;
use LastDragon_ru\LaraASP\GraphQL\Package;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Ignored;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Operators\Extra\Random;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Types\Clause;
Expand Down Expand Up @@ -60,6 +61,17 @@ public function testManipulateArgDefinition(Closure $expected, string $graphql,
* @covers ::manipulateArgDefinition
*/
public function testManipulateArgDefinitionTypeRegistry(): void {
$i = new class([
'name' => 'I',
'fields' => [
[
'name' => 'name',
'type' => Type::string(),
],
],
]) extends InputObjectType implements Ignored {
// empty
};
$a = new InputObjectType([
'name' => 'A',
'fields' => [
Expand All @@ -71,6 +83,10 @@ public function testManipulateArgDefinitionTypeRegistry(): void {
'name' => 'flag',
'type' => Type::nonNull(Type::boolean()),
],
[
'name' => 'ignored',
'type' => Type::nonNull($i),
],
],
]);
$b = new InputObjectType([
Expand Down Expand Up @@ -118,6 +134,7 @@ public function testManipulateArgDefinitionTypeRegistry(): void {
$registry->register($b);
$registry->register($c);
$registry->register($d);
$registry->register($i);

self::assertGraphQLSchemaEquals(
$this->getTestData()->file('~registry-expected.graphql'),
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql/src/SortBy/Directives/Ignored.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Directives;

use LastDragon_ru\LaraASP\GraphQL\SortBy\Contracts\Ignored as IgnoredContract;
use Nuwave\Lighthouse\Support\Contracts\Directive;

class Ignored implements IgnoredContract {
class Ignored implements Directive, IgnoredContract {
public static function definition(): string {
return /** @lang GraphQL */ <<<'GRAPHQL'
"""
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/SortBy/Types/Clause.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function isConvertable(
}

// Ignored?
if ($manipulator->getNodeDirective($node, Ignored::class)) {
if ($node instanceof Ignored || $manipulator->getNodeDirective($node, Ignored::class) !== null) {
return false;
}

Expand Down

0 comments on commit 4fda12d

Please sign in to comment.