Skip to content

Commit 055d0b9

Browse files
authored
Cleaning up schema loader code. (#522)
1 parent 20b89ec commit 055d0b9

1 file changed

Lines changed: 28 additions & 32 deletions

File tree

src/GraphQL/Schema/SchemaLoader.php

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -119,47 +119,44 @@ public function __construct(
119119
* The generated GraphQL schema.
120120
*/
121121
public function getSchema($name) {
122-
if (array_key_exists($name, $this->schemas)) {
123-
return $this->schemas[$name];
122+
if (($schema = &$this->schemas[$name]) !== NULL) {
123+
return $schema;
124124
}
125125

126-
// The cache key is made up of all of the globally known cache contexts.
127-
if (!empty($this->config['schema_cache'])) {
128-
if (($contextCache = $this->metadataCache->get("$name:schema")) && $contextCache->data) {
129-
$cid = $this->getCacheIdentifier($name, $contextCache->data);
126+
$schemaCache = !empty($this->config['schema_cache']);
127+
if (!empty($schemaCache) && ($cache = $this->metadataCache->get("$name:schema")) && $cache->data) {
128+
$cid = $this->getCacheIdentifier($name, $cache->data);
130129

131-
if (($schema = $this->schemaCache->get($cid)) && $schema->data) {
132-
return $this->schemas[$name] = $schema->data;
133-
}
130+
if (($cache = $this->schemaCache->get($cid)) && $cache->data) {
131+
return $schema = $cache->data;
134132
}
135133
}
136134

137-
$this->schemas[$name] = $this->schemaManager->createInstance($name)->getSchema();
138-
// If the schema is not cacheable, just return it directly.
139-
if (empty($this->config['schema_cache'])) {
140-
return $this->schemas[$name];
135+
// Get the schema object from the plugin instance.
136+
$schema = $this->schemaManager->createInstance($name)->getSchema();
137+
if (empty($schemaCache)) {
138+
return $schema;
141139
}
142140

143-
// Compute the cache identifier, tag and expiry time.
144-
$schemaCacheMetadata = $this->getSchemaCacheMetadata($name);
145-
if ($schemaCacheMetadata->getCacheMaxAge() !== 0) {
146-
$tags = $schemaCacheMetadata->getCacheTags();
147-
$expire = $this->maxAgeToExpire($schemaCacheMetadata->getCacheMaxAge());
148-
$cid = $this->getCacheIdentifier($name, $schemaCacheMetadata);
141+
$metadata = $this->getSchemaCacheMetadata($name);
142+
if ($metadata->getCacheMaxAge() !== 0) {
143+
$tags = $metadata->getCacheTags();
144+
$expire = $this->maxAgeToExpire($metadata->getCacheMaxAge());
145+
$cid = $this->getCacheIdentifier($name, $metadata);
149146

150147
// Write the cache entry for the schema cache entries.
151-
$this->schemaCache->set($cid, $this->schemas[$name], $expire, $tags);
148+
$this->schemaCache->set($cid, $schema, $expire, $tags);
152149
}
153150

154-
return $this->schemas[$name];
151+
return $schema;
155152
}
156153

157154
/**
158155
* Retrieves the schema's cache metadata.
159156
*
160157
* @param string $name
161158
* The name of the schema.
162-
* @return \Drupal\Core\Cache\CacheableDependencyInterface
159+
* @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface
163160
* The cache metadata for the schema.
164161
*/
165162
public function getSchemaCacheMetadata($name) {
@@ -196,23 +193,22 @@ public function getResponseCacheMetadata($name) {
196193
* The cache metadata.
197194
*/
198195
protected function getCacheMetadata($name, $cid, callable $callback) {
199-
if (array_key_exists($cid, $this->metadata)) {
200-
return $this->metadata[$cid];
196+
if (($metadata = &$this->metadata[$cid]) !== NULL) {
197+
return $metadata;
201198
}
202199

203-
// The cache key is made up of all of the globally known cache contexts.
204-
if (!empty($this->config['schema_cache'])) {
205-
if (($metadataCache = $this->metadataCache->get($cid)) && $metadataCache->data) {
206-
return $this->metadata[$name] = $metadataCache->data;
207-
}
200+
$schemaCache = !empty($this->config['schema_cache']);
201+
if (!empty($schemaCache) && ($cache = $this->metadataCache->get($cid)) && $cache->data) {
202+
return $metadata = $cache->data;
208203
}
209204

210205
/** @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata */
211206
$schema = $this->getSchema($name);
212207
$metadata = $callback($schema);
213-
$this->metadata[$cid] = $metadata;
214-
if (empty($this->config['schema_cache'])) {
215-
return $this->metadata[$cid];
208+
209+
// Make the schema uncacheable if it is configured that way.
210+
if (empty($schemaCache)) {
211+
return $metadata->mergeCacheMaxAge(0);
216212
}
217213

218214
// Use the schema cache metadata to determine cache expiry and tags.

0 commit comments

Comments
 (0)