Skip to content

Commit

Permalink
feat(graphql): Support of extend scalar/etc X to add operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Feb 13, 2024
1 parent 7a43a09 commit a9dd684
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ input SearchByScalarMixed {
"""
equal: Mixed
@searchByOperatorEqual

"""
Not Equal (`!=`).
"""
notEqual: Mixed
@searchByOperatorNotEqual
}

scalar Date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ scalar SearchByExtra
scalar Mixed
@scalar(class: "GraphQL\\Type\\Definition\\StringType")
@searchByOperatorEqual

extend scalar Mixed
@searchByOperatorNotEqual
28 changes: 25 additions & 3 deletions packages/graphql/src/Utils/AstManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@
use Nuwave\Lighthouse\Support\Contracts\Directive;

use function array_merge;
use function array_udiff;
use function assert;
use function is_string;
use function json_encode;
use function mb_strlen;
use function mb_substr;
use function spl_object_id;
use function sprintf;
use function trim;

Expand Down Expand Up @@ -420,15 +422,28 @@ public function getDirectives(
$directives = [];

if ($node instanceof NamedType) {
// From Node itself (it will also include directive from extension nodes)
if ($node->astNode()) {
$directives = $this->getDirectives($node->astNode(), $class, $callback);
}

foreach ($node->extensionASTNodes() as $extensionNode) {
// Form Node's extensions but without already added
$typeExtensionNodes = $node->extensionASTNodes();
$astExtensionNodes = $this->getDocument()->typeExtensions[$node->name()] ?? [];
$extensionNodes = array_udiff(
$typeExtensionNodes,
$astExtensionNodes,
static function (object $a, object $b): int {
return spl_object_id($a) <=> spl_object_id($b);
},
);

foreach ($extensionNodes as $extensionNode) {
$directives = array_merge($directives, $this->getDirectives($extensionNode, $class, $callback));
}
} elseif ($node instanceof Node) {
$associated = $this->getDirectiveLocator()->associated($node)->all();
// From Node itself
$associated = $this->getDirectiveLocator()->associated($node);

if ($class !== null || $callback !== null) {
foreach ($associated as $directive) {
Expand All @@ -446,7 +461,14 @@ public function getDirectives(
$directives[] = $directive;
}
} else {
$directives = $associated;
$directives = $associated->all();
}

// From extensions ("extend scalar ..."/etc)
if ($node instanceof TypeDefinitionNode) {
foreach ($this->getDocument()->typeExtensions[$node->getName()->value] ?? [] as $extensionNode) {
$directives = array_merge($directives, $this->getDirectives($extensionNode, $class, $callback));
}
}
} elseif ($node instanceof InputObjectField || $node instanceof FieldDefinition || $node instanceof Argument) {
if ($node->astNode) {
Expand Down
1 change: 1 addition & 0 deletions packages/graphql/src/Utils/AstManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public function testGetDirectives(): void {
[
AstManipulatorTest_BDirective::class,
AstManipulatorTest_CDirective::class,
AstManipulatorTest_ADirective::class,
],
array_map(
$map,
Expand Down

0 comments on commit a9dd684

Please sign in to comment.