Completed
Pull Request — 8.x-3.x (#404)
by Sebastian
03:56 queued 01:36
created

SchemaPluginBase::buildConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Schemas;
4
5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheableMetadata;
7
use Drupal\graphql\GraphQL\Utility\TypeCollector;
8
use Drupal\graphql\Plugin\GraphQL\SchemaBuilderInterface;
9
use Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface;
10
use Drupal\graphql\Plugin\GraphQL\Traits\PluginTrait;
11
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
12
use Youshido\GraphQL\Config\Schema\SchemaConfig;
13
use Youshido\GraphQL\Schema\AbstractSchema;
14
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType;
15
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
16
use Youshido\GraphQL\Type\Object\AbstractObjectType;
17
18
abstract class SchemaPluginBase extends AbstractSchema implements SchemaPluginInterface {
19
20
  use PluginTrait;
21
22
  /**
23
   * The response cache metadata object.
24
   *
25
   * @var \Drupal\Core\Cache\CacheableMetadata
26
   */
27
  protected $responseMetadata;
28
29
  /**
30
   * The schema cache metadata object.
31
   *
32
   * @var \Drupal\Core\Cache\CacheableMetadata
33
   */
34
  protected $schemaMetadata;
35
36
  /**
37
   * {@inheritdoc}
38
   */
39
  public function __construct($configuration, $pluginId, $pluginDefinition) {
40
    $this->constructPlugin($configuration, $pluginId, $pluginDefinition);
41
  }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public function buildConfig(SchemaBuilderInterface $schemaBuilder) {
47
    $this->constructSchema($schemaBuilder);
48
    $this->constructCacheMetadata($schemaBuilder);
49
  }
50
51
  /**
52
   * Constructs the schema configuration.
53
   *
54
   * @param \Drupal\graphql\Plugin\GraphQL\SchemaBuilderInterface $schemaBuilder
55
   *   The schema builder.
56
   */
57
  protected function constructSchema(SchemaBuilderInterface $schemaBuilder) {
58
    $this->config = $schemaBuilder->getSchemaConfig();
59
  }
60
61
  /**
62
   * Constructs the schema cache metadata.
63
   *
64
   * @param \Drupal\graphql\Plugin\GraphQL\SchemaBuilderInterface $schemaBuilder
65
   *   The schema builder.
66
   */
67
  protected function constructCacheMetadata(SchemaBuilderInterface $schemaBuilder) {
68
    // Build the schema and response metadata objects based on the provided
69
    // schema config and all included types/fields/etc.
70
    $this->responseMetadata = new CacheableMetadata();
71
    $this->responseMetadata->setCacheMaxAge(Cache::PERMANENT);
72
    $this->schemaMetadata = new CacheableMetadata();
73
    $this->schemaMetadata->setCacheMaxAge(Cache::PERMANENT);
74
75
    foreach (TypeCollector::collectTypes($this) as $type) {
76
      if ($type instanceof TypeSystemPluginInterface) {
77
        $this->schemaMetadata->addCacheableDependency($type->getSchemaCacheMetadata());
78
        $this->responseMetadata->addCacheableDependency($type->getResponseCacheMetadata());
79
      }
80
81
      if ($type instanceof AbstractObjectType || $type instanceof AbstractInputObjectType || $type instanceof AbstractInterfaceType) {
82
        foreach ($type->getFields() as $field) {
83
          if ($field instanceof TypeSystemPluginInterface) {
84
            $this->schemaMetadata->addCacheableDependency($field->getSchemaCacheMetadata());
85
            $this->responseMetadata->addCacheableDependency($field->getResponseCacheMetadata());
86
          }
87
        }
88
      }
89
    }
90
91
    // Merge the schema cache metadata into the response cache metadata.
92
    $this->responseMetadata->addCacheableDependency($this->schemaMetadata);
93
  }
94
95
  /**
96
   * Collects schema cache metadata from all types registered with the schema.
97
   *
98
   * The cache metadata is statically cached. This means that the schema may not
99
   * be modified after this method has been called.
100
   *
101
   * @return \Drupal\Core\Cache\CacheableMetadata
102
   *   The cache metadata collected from the schema's types.
103
   */
104
  public function getSchemaCacheMetadata() {
105
    return $this->schemaMetadata;
106
  }
107
108
  /**
109
   * Collects result cache metadata from all types registered with the schema.
110
   *
111
   * The cache metadata is statically cached. This means that the schema may not
112
   * be modified after this method has been called.
113
   *
114
   * @return \Drupal\Core\Cache\CacheableMetadata
115
   *   The cache metadata collected from the schema's types.
116
   */
117
  public function getResponseCacheMetadata() {
118
    return $this->responseMetadata;
119
  }
120
121
  /**
122
   * {@inheritdoc}
123
   */
124
  public function build(SchemaConfig $config) {
125
    // Not needed anymore.
126
  }
127
}
128