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)); |
|
|
|
|
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
|
|
|
|
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
Change visible in outer-scope