Skip to content

Commit

Permalink
explain why errors are not cached
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Sep 20, 2024
1 parent 7df6e21 commit baaa1a2
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
*/
class GraphQL
{
protected const CACHEABLE_RULES_ERROR_FREE_RESULT = true;

/**
* Lazily initialized.
*
Expand Down Expand Up @@ -396,7 +394,7 @@ protected function parseQuery(string $query): DocumentNode
*
* @param array<string, \GraphQL\Validator\Rules\ValidationRule> $validationRules
*
* @return array<Error>
* @return array<\GraphQL\Error\Error>
*/
protected function validateCacheableRules(
array $validationRules,
Expand Down Expand Up @@ -427,17 +425,22 @@ protected function validateCacheableRules(
assert($cacheFactory instanceof CacheFactory);

$store = $cacheFactory->store($cacheConfig['store']);
if ($store->get($cacheKey) === self::CACHEABLE_RULES_ERROR_FREE_RESULT) {
return [];
$cachedResult = $store->get($cacheKey);
if ($cachedResult !== null) {
return $cachedResult;
}

$result = DocumentValidator::validate($schema, $query, $validationRules);

// If there are any errors, we return them without caching them.
// As of webonyx/graphql-php 15.14.0, GraphQL\Error\Error is not serializable.
// We would have to figure out how to serialize them properly to cache them.
if ($result !== []) {
return $result;
}

$store->put($cacheKey, self::CACHEABLE_RULES_ERROR_FREE_RESULT, $cacheConfig['ttl']);
$store->put($cacheKey, $result, $cacheConfig['ttl']);

return [];
return $result;
}
}

0 comments on commit baaa1a2

Please sign in to comment.