Skip to content

Commit

Permalink
v6.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Mar 31, 2023
1 parent 056cafc commit cbf40d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v6.5.0

### Added

- Add `Nuwave\Lighthouse\Schema\AST\ASTHelper::addDirectiveToNode()` to simplify dynamic addition of directives https://github.com/nuwave/lighthouse/pull/2369

## v6.4.0

### Added
Expand Down
13 changes: 5 additions & 8 deletions src/Schema/AST/ASTHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Language\AST\ValueNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\Directive as DirectiveDefinition;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\EnumValueDefinition;
use GraphQL\Type\Definition\Type;
Expand All @@ -42,8 +42,7 @@
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\Directives\ModelDirective;
use Nuwave\Lighthouse\Schema\Directives\NamespaceDirective;
use Nuwave\Lighthouse\Support\Contracts\ArgManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Contracts\Directive as DirectiveInterface;

class ASTHelper
{
Expand Down Expand Up @@ -308,7 +307,7 @@ public static function qualifiedArgType(
public static function deprecationReason(EnumValueDefinitionNode|FieldDefinitionNode $node): ?string
{
$deprecated = Values::getDirectiveValues(
Directive::deprecatedDirective(),
DirectiveDefinition::deprecatedDirective(),
$node,
);

Expand Down Expand Up @@ -392,11 +391,9 @@ public static function internalFieldName(FieldDefinitionNode $field): string
}

/**
* Adds a directive to a node.
*
* @api
* Adds a directive to a node, instantiates and maybe hydrates it and returns the instance.
*/
public static function addDirectiveToNode(string $directiveSource, ScalarTypeDefinitionNode|ScalarTypeExtensionNode|ObjectTypeDefinitionNode|ObjectTypeExtensionNode|InterfaceTypeDefinitionNode|InterfaceTypeExtensionNode|UnionTypeDefinitionNode|UnionTypeExtensionNode|EnumTypeDefinitionNode|EnumTypeExtensionNode|InputObjectTypeDefinitionNode|InputObjectTypeExtensionNode|FieldDefinitionNode|InputValueDefinitionNode|EnumValueDefinitionNode $node): \Nuwave\Lighthouse\Support\Contracts\Directive
public static function addDirectiveToNode(string $directiveSource, ScalarTypeDefinitionNode|ScalarTypeExtensionNode|ObjectTypeDefinitionNode|ObjectTypeExtensionNode|InterfaceTypeDefinitionNode|InterfaceTypeExtensionNode|UnionTypeDefinitionNode|UnionTypeExtensionNode|EnumTypeDefinitionNode|EnumTypeExtensionNode|InputObjectTypeDefinitionNode|InputObjectTypeExtensionNode|FieldDefinitionNode|InputValueDefinitionNode|EnumValueDefinitionNode $node): DirectiveInterface
{
$directiveNode = Parser::directive($directiveSource);
$node->directives[] = $directiveNode;
Expand Down
31 changes: 12 additions & 19 deletions tests/Unit/Schema/AST/ASTHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Nuwave\Lighthouse\Schema\RootType;
use Nuwave\Lighthouse\Support\Contracts\ArgManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Utils;
use Tests\TestCase;

final class ASTHelperTest extends TestCase
Expand Down Expand Up @@ -237,11 +236,11 @@ public function manipulateFieldDefinition(
DocumentAST &$documentAST,
FieldDefinitionNode &$fieldDefinition,
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType,
):void {
): void {
$fieldDefinition->type = Parser::namedType('Int');
}
};

$directiveLocator = $this->app->make(DirectiveLocator::class);
$directiveLocator->setResolved('foo', $directive::class);

Expand All @@ -255,16 +254,14 @@ public function manipulateFieldDefinition(
DocumentAST &$documentAST,
FieldDefinitionNode &$fieldDefinition,
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType,
):void {
): void {
$directiveInstance = ASTHelper::addDirectiveToNode('@foo', $fieldDefinition);

assert($directiveInstance instanceof FieldManipulator);

$directiveInstance->manipulateFieldDefinition($documentAST, $fieldDefinition, $parentType);
}
};

$directiveLocator = $this->app->make(DirectiveLocator::class);
$directiveLocator->setResolved('dynamic', $dynamicDirective::class);

$this->schema = /** @lang GraphQL */ '
Expand All @@ -283,13 +280,11 @@ public function manipulateFieldDefinition(
$typeType = $fieldType->type;
assert($typeType instanceof NamedTypeNode);

$this->assertEquals($typeType->name->value, 'Int');
$this->assertSame($typeType->name->value, 'Int');
}

public function testDynamicallyAddedArgManipulatorDirective(): void
{
$astBuilder = $this->app->make(ASTBuilder::class);

$directive = new class() extends BaseDirective implements ArgManipulator {
public static function definition(): string
{
Expand All @@ -301,12 +296,11 @@ public function manipulateArgDefinition(
InputValueDefinitionNode &$argDefinition,
FieldDefinitionNode &$parentField,
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType,
): void
{
): void {
$argDefinition->type = Parser::namedType('Int');
}
};

$directiveLocator = $this->app->make(DirectiveLocator::class);
$directiveLocator->setResolved('foo', $directive::class);

Expand All @@ -321,24 +315,23 @@ public function manipulateArgDefinition(
InputValueDefinitionNode &$argDefinition,
FieldDefinitionNode &$parentField,
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType,
): void
{
): void {
$directiveInstance = ASTHelper::addDirectiveToNode('@foo', $argDefinition);

assert($directiveInstance instanceof ArgManipulator);

$directiveInstance->manipulateArgDefinition($documentAST, $argDefinition, $parentField, $parentType);
}
};

$directiveLocator = $this->app->make(DirectiveLocator::class);
$directiveLocator->setResolved('dynamic', $dynamicDirective::class);

$this->schema = /** @lang GraphQL */ '
type Query {
foo( name: String @dynamic ): String
foo(name: String @dynamic): String
}
';
$astBuilder = $this->app->make(ASTBuilder::class);
$documentAST = $astBuilder->documentAST();

$queryType = $documentAST->types[RootType::QUERY];
Expand All @@ -353,6 +346,6 @@ public function manipulateArgDefinition(
$typeType = $argumentType->type;
assert($typeType instanceof NamedTypeNode);

$this->assertEquals($typeType->name->value, 'Int');
$this->assertSame($typeType->name->value, 'Int');
}
}

0 comments on commit cbf40d8

Please sign in to comment.