1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL; |
4
|
|
|
|
5
|
|
|
use Drupal\graphql\GraphQL\Schema\Schema; |
6
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface; |
7
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaPluginInterface; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
9
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
10
|
|
|
|
11
|
|
|
trait CacheableEdgeTrait { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
abstract public function getPlugin(PluggableSchemaBuilderInterface $schemaBuilder); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function getSchemaCacheMetadata(AbstractSchema $schema) { |
22
|
|
|
return $this->getCacheMetadata($schema, function (TypeSystemPluginInterface $plugin) { |
23
|
|
|
return $plugin->getSchemaCacheMetadata(); |
24
|
|
|
}); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getResponseCacheMetadata(AbstractSchema $schema) { |
31
|
|
|
return $this->getCacheMetadata($schema, function (TypeSystemPluginInterface $plugin) { |
32
|
|
|
return $plugin->getResponseCacheMetadata(); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Helper function to load cache metadata from a schema's edges. |
38
|
|
|
* |
39
|
|
|
* @param \Youshido\GraphQL\Schema\AbstractSchema $schema |
40
|
|
|
* The schema. |
41
|
|
|
* @param callable $callback |
42
|
|
|
* Callback to return the cache metadata from an edge. |
43
|
|
|
* |
44
|
|
|
* @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
45
|
|
|
* The cache metadata. |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
protected function getCacheMetadata(AbstractSchema $schema, callable $callback) { |
|
|
|
|
48
|
|
|
if (!$schema instanceof Schema) { |
49
|
|
|
return NULL; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$schemaPlugin = $schema->getSchemaPlugin(); |
53
|
|
|
if (!$schemaPlugin instanceof PluggableSchemaPluginInterface) { |
54
|
|
|
return NULL; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface $plugin */ |
58
|
|
|
if ($plugin = $this->getPlugin($schemaPlugin->getSchemaBuilder())) { |
59
|
|
|
return $callback($plugin); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return NULL; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.