From 21d6b761bb6d92d79c314a14dcc524a26972b767 Mon Sep 17 00:00:00 2001 From: Talles Borges Date: Fri, 9 Aug 2024 14:33:41 -0300 Subject: [PATCH 1/5] set original input --- src/Support/OperationParams.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Support/OperationParams.php b/src/Support/OperationParams.php index 1c8686c6..34dd34bb 100644 --- a/src/Support/OperationParams.php +++ b/src/Support/OperationParams.php @@ -28,6 +28,7 @@ protected function init(BaseOperationParams $baseOperationParams): void $this->operation = $baseOperationParams->operation; $this->variables = $baseOperationParams->variables; $this->extensions = $baseOperationParams->extensions; + $this->originalInput = $baseOperationParams->originalInput; $this->baseOperationParams = $baseOperationParams; } From d33b51c51f7822d9c02dc2d7ed9b2feda7256076 Mon Sep 17 00:00:00 2001 From: Talles Borges Date: Fri, 9 Aug 2024 14:33:51 -0300 Subject: [PATCH 2/5] add test --- .../ExecutionMiddlewareTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php b/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php index 5e90b9b2..f972cb24 100644 --- a/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php +++ b/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php @@ -5,6 +5,7 @@ use Illuminate\Auth\GenericUser; use Rebing\GraphQL\Support\ExecutionMiddleware\AddAuthUserContextValueMiddleware; +use Rebing\GraphQL\Support\ExecutionMiddleware\ValidateOperationParamsMiddleware; use Rebing\GraphQL\Tests\TestCase; class ExecutionMiddlewareTest extends TestCase @@ -171,4 +172,30 @@ public function testAddAuthUserContextValueMiddleware(): void ]; self::assertSame($expected, $result); } + + public function testValidateOperationParamsMiddlewareWithInvalidParams(): void + { + $this->app['config']->set('graphql.execution_middleware', [ + ValidateOperationParamsMiddleware::class, + ]); + + $result = $this->httpGraphql($this->queries['examples'], [ + 'expectErrors' => true, + 'variables' => [ + [] + ], + ]); + + $expected = [ + 'errors' => [ + [ + 'message' => 'GraphQL Request parameter "variables" must be object or JSON string parsed to object, but got [[]]', + 'extensions' => [ + ], + ], + ], + ]; + + self::assertEquals($expected, $result); + } } From c4f3bcad08b88dec52a37c2c40d3033b35ee56ee Mon Sep 17 00:00:00 2001 From: Talles Borges Date: Fri, 9 Aug 2024 14:38:51 -0300 Subject: [PATCH 3/5] fix style --- tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php b/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php index f972cb24..28455ed6 100644 --- a/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php +++ b/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php @@ -182,7 +182,7 @@ public function testValidateOperationParamsMiddlewareWithInvalidParams(): void $result = $this->httpGraphql($this->queries['examples'], [ 'expectErrors' => true, 'variables' => [ - [] + [], ], ]); From e0fe6f1ee52a36d02214f04dba9f390b72574a0d Mon Sep 17 00:00:00 2001 From: Talles Borges Date: Fri, 9 Aug 2024 20:17:26 -0300 Subject: [PATCH 4/5] Fix possible null value in originalInput initialization Ensure that `originalInput` is initialized to an empty array if the value is null. This prevents potential null reference errors during operations. --- src/Support/OperationParams.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/OperationParams.php b/src/Support/OperationParams.php index 34dd34bb..98b89733 100644 --- a/src/Support/OperationParams.php +++ b/src/Support/OperationParams.php @@ -28,7 +28,7 @@ protected function init(BaseOperationParams $baseOperationParams): void $this->operation = $baseOperationParams->operation; $this->variables = $baseOperationParams->variables; $this->extensions = $baseOperationParams->extensions; - $this->originalInput = $baseOperationParams->originalInput; + $this->originalInput = $baseOperationParams->originalInput ?? []; $this->baseOperationParams = $baseOperationParams; } From 4fb0d555cd4a82128aadbfee074eb8dd19aa57f5 Mon Sep 17 00:00:00 2001 From: Talles Borges Date: Sat, 10 Aug 2024 11:07:10 -0300 Subject: [PATCH 5/5] Refactor OperationParams to simplify access to properties Removed redundant baseOperationParams property and replaced it with direct internal mappings. This reduces complexity and enhances code readability by accessing variables and read-only flag directly within OperationParams. --- src/Support/OperationParams.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Support/OperationParams.php b/src/Support/OperationParams.php index 98b89733..defe59c4 100644 --- a/src/Support/OperationParams.php +++ b/src/Support/OperationParams.php @@ -13,9 +13,6 @@ class OperationParams extends BaseOperationParams /** @var DocumentNode|null */ protected $parsedQuery; - /** @var BaseOperationParams */ - protected $baseOperationParams; - public function __construct(BaseOperationParams $baseOperationParams) { $this->init($baseOperationParams); @@ -29,8 +26,7 @@ protected function init(BaseOperationParams $baseOperationParams): void $this->variables = $baseOperationParams->variables; $this->extensions = $baseOperationParams->extensions; $this->originalInput = $baseOperationParams->originalInput ?? []; - - $this->baseOperationParams = $baseOperationParams; + $this->readOnly = $baseOperationParams->readOnly ?? false; } /** @@ -38,12 +34,12 @@ protected function init(BaseOperationParams $baseOperationParams): void */ public function getOriginalInput(string $key) { - return $this->baseOperationParams->originalInput[$key] ?? null; + return $this->originalInput[$key] ?? null; } public function isReadOnly(): bool { - return $this->baseOperationParams->readOnly; + return $this->readOnly; } public function getParsedQuery(): DocumentNode