|
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 BatchedField($this, $this->isSecure(), $config); |
|
52
|
|
|
} |
|
53
|
|
|
else { |
|
54
|
|
|
$this->definition = new Field($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
|
|
|
return $this->resolveDeferred([$this, 'resolveValues'], $value, $args, $info); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function resolveDeferred(callable $callback, $value, array $args, ResolveInfo $info) { |
|
109
|
|
|
$result = $callback($value, $args, $info); |
|
110
|
|
|
if (is_callable($result)) { |
|
111
|
|
|
return new DeferredResolver(function () use ($result, $args, $info, $value) { |
|
112
|
|
|
return $this->resolveDeferred($result, $value, $args, $info); |
|
113
|
|
|
}); |
|
114
|
|
|
} |
|
115
|
|
|
return $this->cacheable(iterator_to_array($result), $value, $args); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Wrap the result in a CacheableValue. |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $result |
|
122
|
|
|
* The field result. |
|
123
|
|
|
* @param mixed $value |
|
124
|
|
|
* The parent value. |
|
125
|
|
|
* @param array $args |
|
126
|
|
|
* The field arguments. |
|
127
|
|
|
* |
|
128
|
|
|
* @return CacheableValue |
|
129
|
|
|
* The cacheable value. |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function cacheable($result, $value, array $args) { |
|
132
|
|
|
if ($this->getPluginDefinition()['multi']) { |
|
133
|
|
|
return new CacheableValue($result, $this->getCacheDependencies($result, $value, $args)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($result) { |
|
137
|
|
|
return new CacheableValue(reset($result), $this->getCacheDependencies($result, $value, $args)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return new CacheableValue(NULL, $this->getCacheDependencies($result, $value, $args)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Retrieve the list of cache dependencies for a given value and arguments. |
|
145
|
|
|
* |
|
146
|
|
|
* @param mixed $result |
|
147
|
|
|
* The result of the field. |
|
148
|
|
|
* @param mixed $parent |
|
149
|
|
|
* The parent value. |
|
150
|
|
|
* @param array $args |
|
151
|
|
|
* The arguments passed to the field. |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
* A list of cacheable dependencies. |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function getCacheDependencies($result, $parent, array $args) { |
|
157
|
|
|
// Default implementation just returns the value itself. |
|
158
|
|
|
return [$result]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Retrieve the list of field values. |
|
163
|
|
|
* |
|
164
|
|
|
* Always returns a list of field values. Even for single value fields. |
|
165
|
|
|
* Single/multi field handling is responsibility of the base class. |
|
166
|
|
|
* |
|
167
|
|
|
* @param mixed $value |
|
168
|
|
|
* The current object value. |
|
169
|
|
|
* @param array $args |
|
170
|
|
|
* Field arguments. |
|
171
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
|
172
|
|
|
* The resolve info object. |
|
173
|
|
|
* |
|
174
|
|
|
* @return \Generator |
|
175
|
|
|
* The value generator. |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
|
178
|
|
|
// Allow overriding this class without having to declare this method. |
|
179
|
|
|
yield NULL; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement 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