|
| 1 | +<? |
| 2 | + |
| 3 | +namespace App\GraphQL\Boilerplate; |
| 4 | + |
| 5 | +use App\GraphQL\Boilerplate\CacheManager; |
| 6 | + |
| 7 | +use App\GraphQL\Exception\PersistedQueryNotSupportedException; |
| 8 | +use App\GraphQL\Exception\PersistedQueryNotFoundException; |
| 9 | + |
| 10 | +/** |
| 11 | + * Improve network performance by sending smaller requests |
| 12 | + * |
| 13 | + * With Automatic Persisted Queries, the ID is a deterministic hash |
| 14 | + * of the input query, so we don't need a complex build step to share the ID between clients and servers. |
| 15 | + * If a server doesn't know about a given hash, the client can expand the query for it; |
| 16 | + * The server caches that mapping. |
| 17 | + * |
| 18 | + * @see https://www.apollographql.com/docs/apollo-server/performance/apq/ |
| 19 | + */ |
| 20 | +class AutomaticPersistedQueries |
| 21 | +{ |
| 22 | + |
| 23 | + protected static $extensionName = 'persistedQuery'; |
| 24 | + protected static $cacheKeyPrefix = 'graphQL_APQ'; |
| 25 | + public $isEnabled; |
| 26 | + |
| 27 | + public function __construct($cacheTTL = 300) |
| 28 | + { |
| 29 | + $this->isEnabled = true; // @TODO: optionize |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + /** |
| 34 | + * @return bool|string Boolean indicates if is cached, string is the gql query |
| 35 | + */ |
| 36 | + public function onRequestRecieved ($query = null, $extensions = null, $variables = null) |
| 37 | + { |
| 38 | + if (!$this->hasCurrentExtension($extensions)) { |
| 39 | + return $query; |
| 40 | + } |
| 41 | + elseif (!$this->isEnabled) { |
| 42 | + throw new PersistedQueryNotSupportedException(); |
| 43 | + } |
| 44 | + |
| 45 | + $queryHash = $this->getQueryHash($extensions); |
| 46 | + if ($queryHash === null) { |
| 47 | + return $query; |
| 48 | + } |
| 49 | + |
| 50 | + if ($query) { |
| 51 | + // A GQL Query is available |
| 52 | + return $this->setQueryCacheByHash($queryHash, $query); |
| 53 | + } |
| 54 | + |
| 55 | + $persistedQuery = $this->lookupQueryCacheByHash($queryHash); |
| 56 | + if ($persistedQuery === null) { |
| 57 | + throw new PersistedQueryNotFoundException($extensions, $queryHash); |
| 58 | + } |
| 59 | + return $persistedQuery; |
| 60 | + } |
| 61 | + |
| 62 | + private function hasCurrentExtension (array $extensions) |
| 63 | + { |
| 64 | + return in_array(self::$extensionName, $extensions); |
| 65 | + } |
| 66 | + |
| 67 | + private function hasQueryHash (array $extensions) |
| 68 | + { |
| 69 | + return ($this->hasCurrentExtension($extensions) && isset($extensions[self::$extensionName]['sha256Hash'])); |
| 70 | + } |
| 71 | + |
| 72 | + private function getQueryHash (array $extensions) |
| 73 | + { |
| 74 | + if ($this->hasQueryHash($extensions)) { |
| 75 | + return $extensions[self::$extensionName]['sha256Hash']; |
| 76 | + } |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + private function generateCacheKey (string $queryHash) |
| 81 | + { |
| 82 | + return '${self::$cacheKeyPrefix}_${queryHash}'; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param String queryHash |
| 87 | + * @param String query (gql) |
| 88 | + * @param Boolean force when true, set the cache even if it already exist. |
| 89 | + * @return Boolean acknowledgement |
| 90 | + */ |
| 91 | + private function setQueryCacheByHash (string $queryHash, string $query, bool $force = true) |
| 92 | + { |
| 93 | + try { |
| 94 | + $lookupKey = $this->generateCacheKey($queryHash); |
| 95 | + } |
| 96 | + catch (\Exception $ex) { |
| 97 | + // @TODO: Improve logs on failure |
| 98 | + return false; |
| 99 | + } |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + private function lookupQueryCacheByHash (string $queryHash) |
| 104 | + { |
| 105 | + // @TODO: Improve logs on failure |
| 106 | + $lookupKey = $this->generateCacheKey($queryHash); |
| 107 | + $cache = CacheManager::getInstance(); |
| 108 | + $cacheItem = $cache->getItem($lookupKey); |
| 109 | + if ($cacheItem->isHit()) { |
| 110 | + // lookupKey item exists in the cache |
| 111 | + return $cacheItem->get(); |
| 112 | + } |
| 113 | + return null; |
| 114 | + } |
| 115 | +} |
0 commit comments