Skip to content

Commit

Permalink
Also set queryId from sha256 hash if query is provided
Browse files Browse the repository at this point in the history
This is necessary to support automatic persisted queries
(https://www.apollographql.com/docs/apollo-server/performance/apq/)
in the way outlined by #1372. Without this fix, the Apollo-style
automatic query hash is not converted to a query id when a query
is also present. This means the persistedQueryLoader is still not
called to allow saving the query, even after the fix from #1372.
  • Loading branch information
aboks authored and spawnia committed Jul 24, 2023
1 parent 6d02e4f commit 3f30729
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Server/OperationParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public static function create(array $params, bool $readonly = false): OperationP
// Apollo server/client compatibility
if (
isset($instance->extensions['persistedQuery']['sha256Hash'])
&& $instance->query === null
&& $instance->queryId === null
) {
$instance->queryId = $instance->extensions['persistedQuery']['sha256Hash'];
Expand Down
25 changes: 25 additions & 0 deletions tests/Server/RequestParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,31 @@ public function testParsesApolloPersistedQueryJSONRequest(): void
}
}

public function testParsesApolloPersistedQueryJSONRequestIncludingQuery(): void
{
$query = '{my query}';
$queryId = 'my-query-id';
$extensions = ['persistedQuery' => ['sha256Hash' => $queryId]];
$variables = ['test' => 1, 'test2' => 2];
$operation = 'op';

$body = [
'query' => '{my query}',
'extensions' => $extensions,
'variables' => $variables,
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body, JSON_THROW_ON_ERROR)),
'psr' => $this->parsePsrRequest('application/json', json_encode($body, JSON_THROW_ON_ERROR)),
];
foreach ($parsed as $method => $parsedBody) {
self::assertInstanceOf(OperationParams::class, $parsedBody);
self::assertValidOperationParams($parsedBody, $query, $queryId, $variables, $operation, $extensions, $method);
self::assertFalse($parsedBody->readOnly, $method);
}
}

public function testParsesBatchJSONRequest(): void
{
$body = [
Expand Down

0 comments on commit 3f30729

Please sign in to comment.