From baaa1a2afee415fb977a928ed1faa34ff7ecb33d Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 20 Sep 2024 11:51:45 +0200 Subject: [PATCH] explain why errors are not cached --- src/GraphQL.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/GraphQL.php b/src/GraphQL.php index d5020973f..36e71f3e0 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -46,8 +46,6 @@ */ class GraphQL { - protected const CACHEABLE_RULES_ERROR_FREE_RESULT = true; - /** * Lazily initialized. * @@ -396,7 +394,7 @@ protected function parseQuery(string $query): DocumentNode * * @param array $validationRules * - * @return array + * @return array<\GraphQL\Error\Error> */ protected function validateCacheableRules( array $validationRules, @@ -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; } }