diff --git a/src/Support/OperationParams.php b/src/Support/OperationParams.php index 1c8686c6..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); @@ -28,8 +25,8 @@ protected function init(BaseOperationParams $baseOperationParams): void $this->operation = $baseOperationParams->operation; $this->variables = $baseOperationParams->variables; $this->extensions = $baseOperationParams->extensions; - - $this->baseOperationParams = $baseOperationParams; + $this->originalInput = $baseOperationParams->originalInput ?? []; + $this->readOnly = $baseOperationParams->readOnly ?? false; } /** @@ -37,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 diff --git a/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php b/tests/Unit/ExecutionMiddlewareTest/ExecutionMiddlewareTest.php index 5e90b9b2..28455ed6 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); + } }