Completed
Push — 8.x-3.x ( e67c0e...2e01b0 )
by Philipp
02:27
created

FieldPluginBase::getDefinition()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 1
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Fields;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\GraphQL\Batching\BatchedFieldInterface;
7
use Drupal\graphql\GraphQL\Cache\CacheableValue;
8
use Drupal\graphql\GraphQL\Field\BatchedField;
9
use Drupal\graphql\GraphQL\Field\Field;
10
use Drupal\graphql\GraphQL\SecureFieldInterface;
11
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
12
use Drupal\graphql\Plugin\GraphQL\Traits\ArgumentAwarePluginTrait;
13
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
14
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait;
15
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
16
use Youshido\GraphQL\Execution\DeferredResolver;
17
use Youshido\GraphQL\Execution\ResolveInfo;
18
19
/**
20
 * Base class for field plugins.
21
 */
22
abstract class FieldPluginBase extends PluginBase implements TypeSystemPluginInterface, SecureFieldInterface {
23
  use CacheablePluginTrait;
24
  use NamedPluginTrait;
25
  use ArgumentAwarePluginTrait;
26
27
  /**
28
   * The field instance.
29
   *
30
   * @var \Drupal\graphql\GraphQL\Field\Field
31
   */
32
  protected $definition;
33
34
  /**
35
   * {@inheritdoc}
36
   */
37
  public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
38
    if (!isset($this->definition)) {
39
      $definition = $this->getPluginDefinition();
40
41
      $config = [
42
        'name' => $this->buildName(),
43
        'description' => $this->buildDescription(),
44
        'type' => $this->buildType($schemaBuilder),
45
        'args' => $this->buildArguments($schemaBuilder),
46
        'isDeprecated' => !empty($definition['deprecated']),
47
        'deprecationReason' => !empty($definition['deprecated']) ? !empty($definition['deprecated']) : '',
48
      ];
49
50
      if ($this instanceof BatchedFieldInterface) {
51
        $this->definition = new Field($this, $this->isSecure(), $config);
52
      }
53
      else {
54
        $this->definition = new BatchedField($this, $this->isSecure(), $config);
55
      }
56
    }
57
58
    return $this->definition;
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function isSecure() {
65
    return isset($this->getPluginDefinition()['secure']) && $this->getPluginDefinition()['secure'];
66
  }
67
68
  /**
69
   * Dummy implementation for `getBatchId` in `BatchedFieldInterface`.
70
   *
71
   * This provides an empty implementation of `getBatchId` in case the subclass
72
   * implements `BatchedFieldInterface`. In may cases this will suffice since
73
   * the batches are already grouped by the class implementing `resolveBatch`.
74
   * `getBatchId` is only necessary for cases where batch grouping depends on
75
   * runtime arguments.
76
   *
77
   * @param mixed $parent
78
   *   The parent value in the result tree.
79
   * @param array $arguments
80
   *   The list of arguments.
81
   * @param ResolveInfo $info
82
   *   The graphql resolve info object.
83
   *
84
   * @return string
85
   *   The batch key.
86
   */
87
  public function getBatchId($parent, array $arguments, ResolveInfo $info) {
88
    return '';
89
  }
90
91
  /**
92
   * {@inheritdoc}
93
   */
94
  public function resolve($value, array $args, ResolveInfo $info) {
95
    if ($this instanceof BatchedFieldInterface) {
96
      $result = $this->getBatchedFieldResolver($value, $args, $info)->add($this, $value, $args, $info);
97
      return new DeferredResolver(function() use ($result, $args, $info, $value) {
98
        $result = iterator_to_array($this->resolveValues($result(), $args, $info));
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $result, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
99
        return $this->cacheable($result, $value, $args);
100
      });
101
    }
102
103
    $result = iterator_to_array($this->resolveValues($value, $args, $info));
104
    return $this->cacheable($result, $value, $args);
105
  }
106
107
  /**
108
   * Wrap the result in a CacheableValue.
109
   *
110
   * @param mixed $result
111
   *   The field result.
112
   * @param mixed $value
113
   *   The parent value.
114
   * @param array $args
115
   *   The field arguments.
116
   *
117
   * @return CacheableValue
118
   *   The cacheable value.
119
   */
120
  protected function cacheable($result, $value, array $args) {
121
    if ($this->getPluginDefinition()['multi']) {
122
      return new CacheableValue($result, $this->getCacheDependencies($result, $value, $args));
123
    }
124
125
    if ($result) {
126
      return new CacheableValue(reset($result), $this->getCacheDependencies($result, $value, $args));
127
    }
128
129
    return new CacheableValue(NULL, $this->getCacheDependencies($result, $value, $args));
130
  }
131
132
  /**
133
   * Retrieve the list of cache dependencies for a given value and arguments.
134
   *
135
   * @param mixed $result
136
   *   The result of the field.
137
   * @param mixed $parent
138
   *   The parent value.
139
   * @param array $args
140
   *   The arguments passed to the field.
141
   *
142
   * @return array
143
   *   A list of cacheable dependencies.
144
   */
145
  protected function getCacheDependencies($result, $parent, array $args) {
146
    // Default implementation just returns the value itself.
147
    return [$result];
148
  }
149
150
  /**
151
   * Retrieve the list of field values.
152
   *
153
   * Always returns a list of field values. Even for single value fields.
154
   * Single/multi field handling is responsibility of the base class.
155
   *
156
   * @param mixed $value
157
   *   The current object value.
158
   * @param array $args
159
   *   Field arguments.
160
   * @param \Youshido\GraphQL\Execution\ResolveInfo $info
161
   *   The resolve info object.
162
   *
163
   * @return \Generator
164
   *   The value generator.
165
   */
166
  protected function resolveValues($value, array $args, ResolveInfo $info) {
167
    // Allow overriding this class without having to declare this method.
168
    yield NULL;
169
  }
170
171
}
172