Skip to content

Commit

Permalink
\LastDragon_ru\LaraASP\GraphQL\Builder\Directives\SchemaDirective w…
Browse files Browse the repository at this point in the history
…ill not merge directives for custom scalars because Lighthouse doing it.
  • Loading branch information
LastDragon-ru committed Nov 28, 2024
1 parent 42b2d9f commit 782b061
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
37 changes: 19 additions & 18 deletions packages/graphql/src/Builder/Directives/SchemaDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ public function __invoke(BuildSchemaString $event): string {

#[Override]
public function manipulateTypeDefinition(DocumentAST &$documentAST, TypeDefinitionNode &$typeDefinition): void {
// todo(graphql): Lighthouse since v6.34.0 merges directives from
// extension nodes except standard types. So the implementation can
// be simplified.

// Apply `extend scalar`.
$manipulator = $this->manipulatorFactory->create($documentAST);

Expand All @@ -92,32 +88,37 @@ public function manipulateTypeDefinition(DocumentAST &$documentAST, TypeDefiniti
continue;
}

// Extend
// Create node
// (it is required because Lighthouse cannot load custom scalars without `@scalar` directive)
// (Failed to find class XXX extends GraphQL\Type\Definition\ScalarType in namespaces [] for the scalar XXX.)
$targetNode = $manipulator->addTypeDefinition($this->getScalarDefinitionNode($targetType));

// Standard?
// (custom scalars will be handled by Lighthouse itself)
if (!$manipulator->isStandard($type)) {
continue;
}

// Extend
foreach ($extensions as $key => $extension) {
// Valid?
if (!($extension instanceof ScalarTypeExtensionNode)) {
throw new TypeDefinitionIsNotScalarExtension($targetType, $this->getExtensionNodeName($extension));
}

// Directives
if ($targetType === $type) {
$targetNode->directives = $targetNode->directives->merge($extension->directives);
} else {
// Only known directives will be copied for alias
foreach ($extension->directives as $index => $directive) {
if ($this->isDirective($directive->name->value)) {
$targetNode->directives[] = $directive;

unset($extension->directives[$index]);
}
}
// (only known directives will be copied for alias)
foreach ($extension->directives as $index => $directive) {
if ($this->isDirective($directive->name->value)) {
$targetNode->directives[] = $directive;

$extension->directives->reindex();
unset($extension->directives[$index]);
}
}

// Remove to avoid conflicts with future Lighthouse version
$extension->directives->reindex();

// Remove to avoid conflicts with the future Lighthouse version
unset($documentAST->typeExtensions[$type][$key]);

if (count($documentAST->typeExtensions[$type]) === 0) {
Expand Down
16 changes: 1 addition & 15 deletions packages/graphql/src/Builder/Directives/SchemaDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use GraphQL\Type\Definition\Type;
use GraphQL\Utils\AST;
use LastDragon_ru\LaraASP\Core\Utils\Cast;
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\TypeDefinitionIsNotScalarExtension;
use LastDragon_ru\LaraASP\GraphQL\Builder\ManipulatorFactory;
use LastDragon_ru\LaraASP\GraphQL\Builder\Scalars\Internal;
use LastDragon_ru\LaraASP\GraphQL\Exceptions\TypeDefinitionAlreadyDefined;
Expand Down Expand Up @@ -120,14 +119,13 @@ public static function dataProviderManipulateTypeDefinition(): array {
@scalar(
class: {$default}
)
@test
@a
GRAPHQL,
<<<'GRAPHQL'
type Query {
test: String! @mock
}
# Should be ignored because handled by Lighthouse.
extend scalar TestScalar
@test
Expand All @@ -136,18 +134,6 @@ class: {$default}
GRAPHQL,
'TestScalar',
],
'Not a scalar extension' => [
new TypeDefinitionIsNotScalarExtension('TestScalar', 'extend enum TestScalar'),
<<<'GRAPHQL'
type Query {
test: String! @mock
}
extend enum TestScalar
@test
GRAPHQL,
'TestScalar',
],
'Unsupported' => [
<<<'GRAPHQL'
union MyUnion =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ type Query {
input A {
a: Int!
}

extend scalar SortByOperatorsExtra
@sortByExtendOperators

extend scalar SortByOperatorsExtra
@sortByOperatorRandom

0 comments on commit 782b061

Please sign in to comment.