From e201632b0569b698b066150349284cde47489e77 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 27 Nov 2020 16:42:06 +0100 Subject: [PATCH] Add test to show that relations can be queried with conditions (#1560) Former-commit-id: 3a3e686cf110740947c37a421aea62d532ece9a0 --- src/Schema/Types/Scalars/Upload.php | 2 - .../Directives/HasManyDirectiveTest.php | 44 +++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/Schema/Types/Scalars/Upload.php b/src/Schema/Types/Scalars/Upload.php index 21fc157124..a1a3362961 100644 --- a/src/Schema/Types/Scalars/Upload.php +++ b/src/Schema/Types/Scalars/Upload.php @@ -13,7 +13,6 @@ class Upload extends ScalarType /** * This always throws, as the Upload scalar can only be used as an argument. * - * * @throws \GraphQL\Error\InvariantViolation */ public function serialize($value): void @@ -26,7 +25,6 @@ public function serialize($value): void /** * Parse a externally provided variable value into a Carbon instance. * - * * @throws \GraphQL\Error\Error */ public function parseValue($value): UploadedFile diff --git a/tests/Integration/Schema/Directives/HasManyDirectiveTest.php b/tests/Integration/Schema/Directives/HasManyDirectiveTest.php index 0e0dbfd697..15035be620 100644 --- a/tests/Integration/Schema/Directives/HasManyDirectiveTest.php +++ b/tests/Integration/Schema/Directives/HasManyDirectiveTest.php @@ -32,9 +32,9 @@ protected function setUp(): void parent::setUp(); $this->user = factory(User::class)->create(); - $this->tasks = factory(Task::class, 3)->create([ - 'user_id' => $this->user->getKey(), - ]); + $this->tasks = factory(Task::class, 3)->make(); + $this->user->tasks()->saveMany($this->tasks); + factory(Task::class)->create([ 'user_id' => $this->user->getKey(), // This task should be ignored via global scope on the Task model @@ -79,6 +79,44 @@ public function testCanQueryHasManyRelationship(): void ')->assertJsonCount(3, 'data.user.tasks'); } + public function testCanQueryHasManyWithCondition(): void + { + $this->schema = /** @lang GraphQL */ ' + type User { + tasks( + id: ID @eq + ): [Task!]! @hasMany + } + + type Task { + id: Int + foo: String + } + + type Query { + user: User @auth + } + '; + + /** @var Task $firstTask */ + $firstTask = $this->user->tasks->first(); + + // Ensure global scopes are respected here + $this + ->graphQL(/** @lang GraphQL */ ' + query ($id: ID){ + user { + tasks(id: $id) { + id + } + } + } + ', [ + 'id' => $firstTask->id, + ]) + ->assertJsonCount(1, 'data.user.tasks'); + } + public function testCallsScopeWithResolverArgs(): void { $this->assertCount(3, $this->user->tasks);