From 63b4e3f0a4527c491f51e37bbda99169a47690f1 Mon Sep 17 00:00:00 2001 From: chriszarate Date: Sat, 1 Dec 2018 18:49:23 -0500 Subject: [PATCH 1/3] Apollo server/client compatibility. Look for queryid in extensions. --- src/Server/OperationParams.php | 5 +++++ tests/Server/RequestParsingTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Server/OperationParams.php b/src/Server/OperationParams.php index 8085d1e..a76f86e 100644 --- a/src/Server/OperationParams.php +++ b/src/Server/OperationParams.php @@ -95,6 +95,11 @@ class OperationParams $instance->variables = $params['variables']; $instance->readOnly = (bool) $readonly; + // Apollo server/client compatibility: look for the queryid in extensions + if (isset($params['extensions']['persistedQuery']['sha256Hash']) && empty($instance->query) && empty($instance->queryid)) { + $instance->queryId = $params['extensions']['persistedQuery']['sha256Hash']; + } + return $instance; } diff --git a/tests/Server/RequestParsingTest.php b/tests/Server/RequestParsingTest.php index b6c73d6..75412b1 100644 --- a/tests/Server/RequestParsingTest.php +++ b/tests/Server/RequestParsingTest.php @@ -333,6 +333,32 @@ class RequestParsingTest extends TestCase } } + public function testParsesApolloPersistedQueryJSONRequest() : void + { + $queryId = 'my-query-id'; + $extensions = [ + 'persistedQuery' => [ + 'sha256Hash' => $queryId, + ], + ]; + $variables = ['test' => 1, 'test2' => 2]; + $operation = 'op'; + + $body = [ + 'extensions' => $extensions, + 'variables' => $variables, + 'operationName' => $operation, + ]; + $parsed = [ + 'raw' => $this->parseRawRequest('application/json', json_encode($body)), + 'psr' => $this->parsePsrRequest('application/json', json_encode($body)), + ]; + foreach ($parsed as $method => $parsedBody) { + self::assertValidOperationParams($parsedBody, null, $queryId, $variables, $operation, $method); + self::assertFalse($parsedBody->isReadOnly(), $method); + } + } + public function testParsesBatchJSONRequest() : void { $body = [ From c33e41f2bf44b20b3d79b00d335c3a5ea5975116 Mon Sep 17 00:00:00 2001 From: chriszarate Date: Sat, 1 Dec 2018 19:43:51 -0500 Subject: [PATCH 2/3] Fix linting issue and typos. --- src/Server/OperationParams.php | 4 ++-- tests/Server/RequestParsingTest.php | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Server/OperationParams.php b/src/Server/OperationParams.php index a76f86e..65edfc8 100644 --- a/src/Server/OperationParams.php +++ b/src/Server/OperationParams.php @@ -96,8 +96,8 @@ class OperationParams $instance->readOnly = (bool) $readonly; // Apollo server/client compatibility: look for the queryid in extensions - if (isset($params['extensions']['persistedQuery']['sha256Hash']) && empty($instance->query) && empty($instance->queryid)) { - $instance->queryId = $params['extensions']['persistedQuery']['sha256Hash']; + if (isset($params['extensions']['persistedQuery']['sha256Hash']) && empty($instance->query) && empty($instance->queryId)) { + $instance->queryId = $params['extensions']['persistedQuery']['sha256Hash']; } return $instance; diff --git a/tests/Server/RequestParsingTest.php b/tests/Server/RequestParsingTest.php index 75412b1..f935281 100644 --- a/tests/Server/RequestParsingTest.php +++ b/tests/Server/RequestParsingTest.php @@ -335,12 +335,8 @@ class RequestParsingTest extends TestCase public function testParsesApolloPersistedQueryJSONRequest() : void { - $queryId = 'my-query-id'; - $extensions = [ - 'persistedQuery' => [ - 'sha256Hash' => $queryId, - ], - ]; + $queryId = 'my-query-id'; + $extensions = ['persistedQuery' => ['sha256Hash' => $queryId]]; $variables = ['test' => 1, 'test2' => 2]; $operation = 'op'; From f644c1a837890e4ca854d16371c0730bc32b9314 Mon Sep 17 00:00:00 2001 From: chriszarate Date: Sun, 2 Dec 2018 15:10:09 -0500 Subject: [PATCH 3/3] Add extensions to OperationParams instance. --- src/Server/OperationParams.php | 22 +++++++++++++++------- tests/Server/RequestParsingTest.php | 20 ++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/Server/OperationParams.php b/src/Server/OperationParams.php index 65edfc8..0d14614 100644 --- a/src/Server/OperationParams.php +++ b/src/Server/OperationParams.php @@ -46,6 +46,12 @@ class OperationParams */ public $variables; + /** + * @api + * @var mixed[]|null + */ + public $extensions; + /** @var mixed[] */ private $originalInput; @@ -76,6 +82,7 @@ class OperationParams 'id' => null, // alias to queryid 'operationname' => null, 'variables' => null, + 'extensions' => null, ]; if ($params['variables'] === '') { @@ -89,15 +96,16 @@ class OperationParams } } - $instance->query = $params['query']; - $instance->queryId = $params['queryid'] ?: $params['documentid'] ?: $params['id']; - $instance->operation = $params['operationname']; - $instance->variables = $params['variables']; - $instance->readOnly = (bool) $readonly; + $instance->query = $params['query']; + $instance->queryId = $params['queryid'] ?: $params['documentid'] ?: $params['id']; + $instance->operation = $params['operationname']; + $instance->variables = $params['variables']; + $instance->extensions = $params['extensions']; + $instance->readOnly = (bool) $readonly; // Apollo server/client compatibility: look for the queryid in extensions - if (isset($params['extensions']['persistedQuery']['sha256Hash']) && empty($instance->query) && empty($instance->queryId)) { - $instance->queryId = $params['extensions']['persistedQuery']['sha256Hash']; + if (isset($instance->extensions['persistedQuery']['sha256Hash']) && empty($instance->query) && empty($instance->queryId)) { + $instance->queryId = $instance->extensions['persistedQuery']['sha256Hash']; } return $instance; diff --git a/tests/Server/RequestParsingTest.php b/tests/Server/RequestParsingTest.php index f935281..cf2e9c5 100644 --- a/tests/Server/RequestParsingTest.php +++ b/tests/Server/RequestParsingTest.php @@ -25,7 +25,7 @@ class RequestParsingTest extends TestCase ]; foreach ($parsed as $source => $parsedBody) { - self::assertValidOperationParams($parsedBody, $query, null, null, null, $source); + self::assertValidOperationParams($parsedBody, $query, null, null, null, null, $source); self::assertFalse($parsedBody->isReadOnly(), $source); } } @@ -91,6 +91,7 @@ class RequestParsingTest extends TestCase $queryId = null, $variables = null, $operation = null, + $extensions = null, $message = '' ) { self::assertInstanceOf(OperationParams::class, $params, $message); @@ -99,6 +100,7 @@ class RequestParsingTest extends TestCase self::assertSame($queryId, $params->queryId, $message); self::assertSame($variables, $params->variables, $message); self::assertSame($operation, $params->operation, $message); + self::assertSame($extensions, $params->extensions, $message); } public function testParsesUrlencodedRequest() : void @@ -118,7 +120,7 @@ class RequestParsingTest extends TestCase ]; foreach ($parsed as $method => $parsedBody) { - self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method); + self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method); self::assertFalse($parsedBody->isReadOnly(), $method); } } @@ -175,7 +177,7 @@ class RequestParsingTest extends TestCase ]; foreach ($parsed as $method => $parsedBody) { - self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method); + self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method); self::assertTrue($parsedBody->isReadonly(), $method); } } @@ -230,7 +232,7 @@ class RequestParsingTest extends TestCase ]; foreach ($parsed as $method => $parsedBody) { - self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method); + self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method); self::assertFalse($parsedBody->isReadOnly(), $method); } } @@ -286,7 +288,7 @@ class RequestParsingTest extends TestCase 'psr' => $this->parsePsrRequest('application/json', json_encode($body)), ]; foreach ($parsed as $method => $parsedBody) { - self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method); + self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method); self::assertFalse($parsedBody->isReadOnly(), $method); } } @@ -307,7 +309,7 @@ class RequestParsingTest extends TestCase 'psr' => $this->parsePsrRequest('application/json', json_encode($body)), ]; foreach ($parsed as $method => $parsedBody) { - self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method); + self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method); self::assertFalse($parsedBody->isReadOnly(), $method); } } @@ -328,7 +330,7 @@ class RequestParsingTest extends TestCase 'psr' => $this->parsePsrRequest('application/json', json_encode($body)), ]; foreach ($parsed as $method => $parsedBody) { - self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method); + self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method); self::assertFalse($parsedBody->isReadOnly(), $method); } } @@ -350,7 +352,7 @@ class RequestParsingTest extends TestCase 'psr' => $this->parsePsrRequest('application/json', json_encode($body)), ]; foreach ($parsed as $method => $parsedBody) { - self::assertValidOperationParams($parsedBody, null, $queryId, $variables, $operation, $method); + self::assertValidOperationParams($parsedBody, null, $queryId, $variables, $operation, $extensions, $method); self::assertFalse($parsedBody->isReadOnly(), $method); } } @@ -382,6 +384,7 @@ class RequestParsingTest extends TestCase null, $body[0]['variables'], $body[0]['operationName'], + null, $method ); self::assertValidOperationParams( @@ -390,6 +393,7 @@ class RequestParsingTest extends TestCase $body[1]['queryId'], $body[1]['variables'], $body[1]['operationName'], + null, $method ); }