Skip to content

Commit 6aecc80

Browse files
oprypkhantcspawnia
authored andcommitted
Call persistedQueryLoader even when query was passed
1 parent 5c0180d commit 6aecc80

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
### Changed
13+
14+
- Allow sending both `query` and `queryId`, let `persistedQueryLoader` handle it
15+
1216
## v15.3.0
1317

1418
### Added

docs/executing-queries.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,19 @@ PSR-7 is useful when you want to integrate the server into existing framework:
9696

9797
### Server configuration options
9898

99-
| Argument | Type | Notes |
100-
| -------------------- | --------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
101-
| schema | [`Schema`](class-reference.md#graphqltypeschema) | **Required.** Instance of your application [Schema](schema-definition.md) |
102-
| rootValue | `mixed` | Any value that represents a root of your data graph. It is passed as the 1st argument to field resolvers of [Query type](schema-definition.md#query-and-mutation-types). Can be omitted or set to null if actual root values are fetched by Query type itself. |
103-
| context | `mixed` | Any value that holds information shared between all field resolvers. Most often they use it to pass currently logged in user, locale details, etc.<br><br>It will be available as the 3rd argument in all field resolvers. (see section on [Field Definitions](type-definitions/object-types.md#field-configuration-options) for reference) **graphql-php** never modifies this value and passes it _as is_ to all underlying resolvers. |
104-
| fieldResolver | `callable` | A resolver function to use when one is not provided by the schema. If not provided, the [default field resolver is used](data-fetching.md#default-field-resolver). |
99+
| Argument | Type | Notes |
100+
| -------------------- | --------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
101+
| schema | [`Schema`](class-reference.md#graphqltypeschema) | **Required.** Instance of your application [Schema](schema-definition.md) |
102+
| rootValue | `mixed` | Any value that represents a root of your data graph. It is passed as the 1st argument to field resolvers of [Query type](schema-definition.md#query-and-mutation-types). Can be omitted or set to null if actual root values are fetched by Query type itself. |
103+
| context | `mixed` | Any value that holds information shared between all field resolvers. Most often they use it to pass currently logged in user, locale details, etc.<br><br>It will be available as the 3rd argument in all field resolvers. (see section on [Field Definitions](type-definitions/object-types.md#field-configuration-options) for reference) **graphql-php** never modifies this value and passes it _as is_ to all underlying resolvers. |
104+
| fieldResolver | `callable` | A resolver function to use when one is not provided by the schema. If not provided, the [default field resolver is used](data-fetching.md#default-field-resolver). |
105105
| validationRules | `array` or `callable` | A set of rules for query validation step. The default value is all available rules. The empty array would allow skipping query validation (may be convenient for persisted queries which are validated before persisting and assumed valid during execution).<br><br>Pass `callable` to return different validation rules for different queries (e.g. empty array for persisted query and a full list of rules for regular queries). When passed, it is expected to have the following signature: <br><br> **function ([OperationParams](class-reference.md#graphqlserveroperationparams) $params, DocumentNode $node, $operationType): array** |
106-
| queryBatching | `bool` | Flag indicating whether this server supports query batching ([apollo-style](https://www.apollographql.com/blog/apollo-client/performance/query-batching/)).<br><br> Defaults to **false** |
107-
| debugFlag | `int` | Debug flags. See [docs on error debugging](error-handling.md#debugging-tools) (flag values are the same). |
108-
| persistedQueryLoader | `callable` | A function which is called to fetch actual query when server encounters a **queryId** without a **query**.<br><br> The server does not implement persistence part (which you will have to build on your own), but it allows you to execute queries which were persisted previously.<br><br> Expected function signature:<br> **function ($queryId, [OperationParams](class-reference.md#graphqlserveroperationparams) $params)** <br><br>Function is expected to return query **string** or parsed **DocumentNode** <br><br> [Read more about persisted queries](https://www.apollographql.com/blog/apollo-client/persisted-graphql-queries). |
109-
| errorFormatter | `callable` | Custom error formatter. See [error handling docs](error-handling.md#custom-error-handling-and-formatting). |
110-
| errorsHandler | `callable` | Custom errors handler. See [error handling docs](error-handling.md#custom-error-handling-and-formatting). |
111-
| promiseAdapter | [`PromiseAdapter`](class-reference.md#graphqlexecutorpromisepromiseadapter) | Required for [Async PHP](data-fetching.md#async-php) only. |
106+
| queryBatching | `bool` | Flag indicating whether this server supports query batching ([apollo-style](https://www.apollographql.com/blog/apollo-client/performance/query-batching/)).<br><br> Defaults to **false** |
107+
| debugFlag | `int` | Debug flags. See [docs on error debugging](error-handling.md#debugging-tools) (flag values are the same). |
108+
| persistedQueryLoader | `callable` | A function which is called to fetch actual query when server encounters a **queryId**.<br><br> The server does not implement persistence part (which you will have to build on your own), but it allows you to execute queries which were persisted previously.<br><br> Expected function signature:<br> **function ($queryId, [OperationParams](class-reference.md#graphqlserveroperationparams) $params)** <br><br>Function is expected to return query **string** or parsed **DocumentNode** <br><br> [Read more about persisted queries](https://www.apollographql.com/blog/apollo-client/persisted-graphql-queries). |
109+
| errorFormatter | `callable` | Custom error formatter. See [error handling docs](error-handling.md#custom-error-handling-and-formatting). |
110+
| errorsHandler | `callable` | Custom errors handler. See [error handling docs](error-handling.md#custom-error-handling-and-formatting). |
111+
| promiseAdapter | [`PromiseAdapter`](class-reference.md#graphqlexecutorpromisepromiseadapter) | Required for [Async PHP](data-fetching.md#async-php) only. |
112112

113113
#### Using config class
114114

src/Server/Helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ protected function promiseToExecuteOperation(
255255
);
256256
}
257257

258-
$doc = $op->queryId !== null && $op->query === null
258+
$doc = $op->queryId !== null
259259
? $this->loadPersistedQuery($config, $op)
260260
: $op->query;
261261

tests/Server/QueryExecutionTest.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,22 @@ public function testAllowSkippingValidationForPersistedQueries(): void
455455
self::assertSame($expected, $result->toArray());
456456
}
457457

458-
public function testExecutesQueryWhenQueryAndQueryIdArePassed(): void
458+
public function testLoadsPersistedQueryWhenQueryAndQueryIdArePassed(): void
459459
{
460460
$query = /** @lang GraphQL */ '{ f1 }';
461461

462462
$expected = [
463-
'data' => ['f1' => 'f1'],
463+
'errors' => [
464+
[
465+
'message' => 'Cannot query field "invalid" on type "Query".',
466+
'locations' => [['line' => 1, 'column' => 3]],
467+
],
468+
],
464469
];
465-
$this->config->setPersistedQueryLoader(static function (): array {
466-
throw new \Exception('Should not be called since a query is also passed');
470+
$this->config->setPersistedQueryLoader(static function (string $queryId, OperationParams $params) use ($query): string {
471+
self::assertSame($query, $params->query);
472+
473+
return /** @lang GraphQL */ '{ invalid }';
467474
});
468475

469476
$this->assertQueryResultEquals($expected, $query, [], 'some-id');

0 commit comments

Comments
 (0)