Skip to content

Commit

Permalink
fix(graphql): AstManipulator::addTypeDefinition() will convert `Sca…
Browse files Browse the repository at this point in the history
…larType` into `scalar` and will add it into `Document` instead of `TypeRegistry`.

For other `Type`s the `NotImplemented` will be thrown (to indicate that the type will be lost if the Schema is cached).

(cherry picked from commit 3e23e8e)
  • Loading branch information
LastDragon-ru committed Sep 8, 2023
1 parent f09e251 commit 3647e1a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/graphql/src/Utils/AstManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldDefinition;
Expand All @@ -34,6 +35,7 @@
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Definition\WrappingType;
use LastDragon_ru\LaraASP\GraphQL\Exceptions\NotImplemented;
use LastDragon_ru\LaraASP\GraphQL\Exceptions\TypeDefinitionAlreadyDefined;
use LastDragon_ru\LaraASP\GraphQL\Exceptions\TypeDefinitionUnknown;
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
Expand All @@ -45,9 +47,12 @@

use function array_merge;
use function assert;
use function json_encode;
use function reset;
use function trim;

use const JSON_THROW_ON_ERROR;

// @phpcs:disable Generic.Files.LineLength.TooLong

class AstManipulator {
Expand Down Expand Up @@ -196,8 +201,19 @@ public function addTypeDefinition(TypeDefinitionNode|Type $definition): TypeDefi

if ($definition instanceof TypeDefinitionNode && $definition instanceof Node) {
$this->getDocument()->setTypeDefinition($definition);
} elseif ($definition instanceof ScalarType) {
$class = json_encode($definition::class, JSON_THROW_ON_ERROR);
$scalar = Parser::scalarTypeDefinition(
<<<GRAPHQL
scalar {$name} @scalar(class: {$class})
GRAPHQL,
);

$this->getDocument()->setTypeDefinition($scalar);
} elseif ($definition instanceof Type) {
$this->getTypes()->register($definition);
// Types added while AST transformation will be lost if the Schema
// is cached. Not yet sure how to solve it... Any ideas?
throw new NotImplemented('`Type` registration');
} else {
// empty
}
Expand Down

0 comments on commit 3647e1a

Please sign in to comment.