From c09194823252738ee41f00af7ebe5bbeb65e6c8c Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Thu, 25 Jul 2024 11:49:22 +0200 Subject: [PATCH] Improve validation message when using output types as inputs --- CHANGELOG.md | 6 ++++ src/Schema/Factories/ArgumentFactory.php | 2 -- tests/Integration/Schema/ValidatorTest.php | 28 +++++++++++++++++++ .../Http/Middleware/LogGraphQLQueriesTest.php | 11 ++++---- 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 tests/Integration/Schema/ValidatorTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e8cf1928c..822d530d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co ## Unreleased +## v6.42.1 + +### Changed + +- Improve validation message when using output types as inputs https://github.com/nuwave/lighthouse/pull/2594 + ## v6.42.0 ### Added diff --git a/src/Schema/Factories/ArgumentFactory.php b/src/Schema/Factories/ArgumentFactory.php index 39ab3dceb9..b27ea469c6 100644 --- a/src/Schema/Factories/ArgumentFactory.php +++ b/src/Schema/Factories/ArgumentFactory.php @@ -3,7 +3,6 @@ namespace Nuwave\Lighthouse\Schema\Factories; use GraphQL\Language\AST\InputValueDefinitionNode; -use GraphQL\Type\Definition\InputType; use GraphQL\Type\Definition\Type; use Illuminate\Container\Container; use Nuwave\Lighthouse\Schema\AST\ASTHelper; @@ -46,7 +45,6 @@ public function convert(InputValueDefinitionNode $definitionNode): array { $definitionNodeConverter = Container::getInstance()->make(ExecutableTypeNodeConverter::class); $type = $definitionNodeConverter->convert($definitionNode->type); - assert($type instanceof Type && $type instanceof InputType); $config = [ 'name' => $definitionNode->name->value, diff --git a/tests/Integration/Schema/ValidatorTest.php b/tests/Integration/Schema/ValidatorTest.php new file mode 100644 index 0000000000..c75969b25b --- /dev/null +++ b/tests/Integration/Schema/ValidatorTest.php @@ -0,0 +1,28 @@ +schema = /** @lang GraphQL */ ' + type Query { + foo(foo: Foo): Int + } + + type Foo { + foo: Int + } + '; + + $schemaValidator = $this->app->make(SchemaValidator::class); + + $this->expectExceptionObject(new InvariantViolation('The type of Query.foo(foo:) must be Input Type but got: Foo.')); + $schemaValidator->validate(); + } +} diff --git a/tests/Unit/Http/Middleware/LogGraphQLQueriesTest.php b/tests/Unit/Http/Middleware/LogGraphQLQueriesTest.php index 9c6bea956a..e25ce3a8dd 100644 --- a/tests/Unit/Http/Middleware/LogGraphQLQueriesTest.php +++ b/tests/Unit/Http/Middleware/LogGraphQLQueriesTest.php @@ -4,13 +4,14 @@ use Illuminate\Contracts\Config\Repository as ConfigRepository; use Nuwave\Lighthouse\Http\Middleware\LogGraphQLQueries; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Tests\TestCase; final class LogGraphQLQueriesTest extends TestCase { /** @var \PHPUnit\Framework\MockObject\MockObject&\Psr\Log\LoggerInterface */ - protected $logger; + protected MockObject $logger; protected function getEnvironmentSetUp($app): void { @@ -28,10 +29,10 @@ protected function getEnvironmentSetUp($app): void public function testLogsEveryQuery(): void { $query = /** @lang GraphQL */ <<<'GRAPHQL' -{ - foo -} -GRAPHQL; + { + foo + } + GRAPHQL; $this->logger ->expects($this->once())