Skip to content

Commit d5d0471

Browse files
committed
Add gql cache context to every response.
1 parent 885c5d4 commit d5d0471

8 files changed

Lines changed: 20 additions & 21 deletions

File tree

src/Annotation/GraphQLAnnotationBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function __construct($values) {
8585
*
8686
* @var array
8787
*/
88-
public $response_cache_contexts = ['gql', 'user'];
88+
public $response_cache_contexts = ['user'];
8989

9090
/**
9191
* The cache tags for caching theresponse.

src/Cache/Context/QueryCacheContext.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,20 @@ public function getContext() {
5757
$hash = '';
5858
if ($request->attributes->has('query')) {
5959
$hash = $this->contextCache[$request] = $this->getHash(
60-
$request->attributes->get('id') ?: '',
60+
$request->attributes->get('schema') ?: '',
6161
$request->attributes->get('query') ?: '',
62-
$request->attributes->get('variables') ?: [],
63-
$request->attributes->get('version') ?: ''
62+
$request->attributes->get('variables') ?: []
6463
);
6564
}
6665
else if ($request->attributes->has('queries')) {
6766
$queries = $request->attributes->get('queries');
67+
$schema = $request->attributes->get('schema') ?: '';
6868

69-
return hash('sha256', json_encode(array_map(function($item) {
69+
return hash('sha256', json_encode(array_map(function($item) use ($schema) {
7070
return $this->getHash(
71-
!empty($item['id']) ? $item['id'] : '',
71+
$schema,
7272
!empty($item['query']) ? $item['query'] : '',
73-
!empty($item['variables']) ? $item['variables'] : [],
74-
!empty($item['version']) ? $item['version'] : NULL
73+
!empty($item['variables']) ? $item['variables'] : []
7574
);
7675
}, $queries)));
7776
}
@@ -85,27 +84,24 @@ public function getContext() {
8584
* Sorts the variables by their key and eliminates whitespace from the query
8685
* to enable better reuse of the cache entries.
8786
*
88-
* @param string $id
89-
* The query id in case of a persisted query.
87+
* @param string $schema
88+
* The schema id.
9089
* @param string $query
9190
* The graphql query string.
9291
* @param array $variables
9392
* The graphql query variables.
94-
* @param string $version
95-
* The query map version in case of a persisted query.
9693
*
9794
* @return string
9895
* The hashed string containing.
9996
*/
100-
protected function getHash($id = '', $query = '', array $variables = [], $version = '') {
97+
protected function getHash($schema = '', $query = '', array $variables = []) {
10198
$query = preg_replace('/\s{2,}/', ' ', $query);
10299
ksort($variables);
103100

104101
return hash('sha256', json_encode([
105-
'id' => $id,
102+
'schema' => $schema,
106103
'query' => $query,
107104
'variables' => $variables,
108-
'version' => $version,
109105
]));
110106
}
111107

src/Plugin/GraphQL/Schemas/SchemaPluginBase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ protected function constructCacheMetadata(SchemaBuilderInterface $schemaBuilder)
6969
// schema config and all included types/fields/etc.
7070
$this->responseMetadata = new CacheableMetadata();
7171
$this->responseMetadata->setCacheMaxAge(Cache::PERMANENT);
72+
$this->responseMetadata->addCacheTags(['graphql_response', "graphql_response:{$this->getPluginId()}"]);
73+
$this->responseMetadata->addCacheContexts(['gql']);
74+
7275
$this->schemaMetadata = new CacheableMetadata();
7376
$this->schemaMetadata->setCacheMaxAge(Cache::PERMANENT);
77+
$this->schemaMetadata->addCacheTags(['graphql_schema', "graphql_schema:{$this->getPluginId()}"]);
7478

7579
foreach (TypeCollector::collectTypes($this) as $type) {
7680
if ($type instanceof TypeSystemPluginInterface) {

src/Plugin/GraphQL/Traits/CacheablePluginTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getSchemaCacheMetadata() {
4242
public function getResponseCacheMetadata() {
4343
$definition = $this->getPluginDefinition();
4444
$metadata = new CacheableMetadata();
45-
$metadata->addCacheContexts(isset($definition['response_cache_contexts']) ? $definition['response_cache_contexts'] : ['gql', 'user']);
45+
$metadata->addCacheContexts(isset($definition['response_cache_contexts']) ? $definition['response_cache_contexts'] : ['user']);
4646
$metadata->addCacheTags(isset($definition['response_cache_tags']) ? $definition['response_cache_tags'] : []);
4747
$metadata->setCacheMaxAge(isset($definition['response_cache_max_age']) ? $definition['response_cache_max_age'] : CacheBackendInterface::CACHE_PERMANENT);
4848
return $metadata;

tests/modules/graphql_cache_test/src/Plugin/GraphQL/Fields/A.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* type = "Int",
1919
* parents = {"Root", "Object"},
2020
* response_cache_tags = {"a", "graphql_response"},
21-
* response_cache_contexts = {"graphql_test", "gql", "user"}
21+
* response_cache_contexts = {"graphql_test", "user"}
2222
* )
2323
*/
2424
class A extends FieldPluginBase implements ContainerFactoryPluginInterface {

tests/modules/graphql_cache_test/src/Plugin/GraphQL/Fields/B.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* type = "Int",
1313
* parents = {"Root", "Object"},
1414
* response_cache_tags = {"b", "graphql_response"},
15-
* response_cache_contexts = {"graphql_test", "gql", "user"}
15+
* response_cache_contexts = {"graphql_test", "user"}
1616
* )
1717
*/
1818
class B extends A {

tests/modules/graphql_cache_test/src/Plugin/GraphQL/Fields/C.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* type = "Int",
1313
* parents = {"Root"},
1414
* response_cache_tags = {"c", "graphql_response"},
15-
* response_cache_contexts = {"graphql_test_root_field", "gql", "user"}
15+
* response_cache_contexts = {"graphql_test_root_field", "user"}
1616
* )
1717
*/
1818
class C extends A {

tests/modules/graphql_test/src/Plugin/GraphQL/Schemas/TestSchema.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ protected function constructCacheMetadata(SchemaBuilderInterface $schemaBuilder)
3131
parent::constructCacheMetadata($schemaBuilder);
3232

3333
$metadata = new CacheableMetadata();
34-
$metadata->addCacheContexts(['gql', 'user']);
35-
$metadata->addCacheTags(['graphql_response']);
34+
$metadata->addCacheContexts(['user']);
3635

3736
$this->responseMetadata->addCacheableDependency($metadata);
3837
}

0 commit comments

Comments
 (0)