1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Execution; |
4
|
|
|
|
5
|
|
|
use Drupal\graphql\GraphQL\Execution\QueryVisitor\QueryVisitorInterface; |
6
|
|
|
use Drupal\graphql\GraphQL\Utility\TypeCollector; |
7
|
|
|
use Youshido\GraphQL\Execution\Request; |
8
|
|
|
use Youshido\GraphQL\Field\FieldInterface; |
9
|
|
|
use Youshido\GraphQL\Parser\Ast\AbstractAst; |
10
|
|
|
use Youshido\GraphQL\Parser\Ast\Field; |
11
|
|
|
use Youshido\GraphQL\Parser\Ast\Fragment; |
12
|
|
|
use Youshido\GraphQL\Parser\Ast\FragmentReference; |
13
|
|
|
use Youshido\GraphQL\Parser\Ast\Interfaces\FragmentInterface; |
14
|
|
|
use Youshido\GraphQL\Parser\Ast\Mutation; |
15
|
|
|
use Youshido\GraphQL\Parser\Ast\Query; |
16
|
|
|
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference; |
17
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
18
|
|
|
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
19
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
20
|
|
|
|
21
|
|
|
class QueryReducer { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Youshido\GraphQL\Execution\Request |
25
|
|
|
*/ |
26
|
|
|
protected $request; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Youshido\GraphQL\Schema\AbstractSchema |
30
|
|
|
*/ |
31
|
|
|
protected $schema; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Youshido\GraphQL\Type\TypeInterface[] |
35
|
|
|
*/ |
36
|
|
|
protected $types; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* QueryReducer constructor. |
40
|
|
|
* |
41
|
|
|
* @param \Youshido\GraphQL\Schema\AbstractSchema $schema |
42
|
|
|
* @param \Youshido\GraphQL\Execution\Request $request |
43
|
|
|
*/ |
44
|
|
|
public function __construct(AbstractSchema $schema, Request $request) { |
45
|
|
|
$this->schema = $schema; |
46
|
|
|
$this->request = $request; |
47
|
|
|
$this->types = TypeCollector::collectTypes($schema); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Drupal\graphql\GraphQL\Execution\QueryVisitor\QueryVisitorInterface $visitor |
52
|
|
|
* |
53
|
|
|
* @return mixed|\Youshido\GraphQL\Parser\Ast\Query |
54
|
|
|
*/ |
55
|
|
|
public function reduceQuery(QueryVisitorInterface $visitor) { |
56
|
|
|
$operations = $this->request->getAllOperations(); |
57
|
|
|
|
58
|
|
|
return $visitor->finish(array_reduce($operations, function ($carry, $current) use ($visitor) { |
59
|
|
|
$type = $current instanceof Mutation ? $this->schema->getMutationType() : $this->schema->getQueryType(); |
60
|
|
|
return $this->reduceOperation($current, $carry, $type, $visitor); |
61
|
|
|
}, $visitor->initial())); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \Youshido\GraphQL\Parser\Ast\Query $query |
66
|
|
|
* @param $carry |
67
|
|
|
* @param $current |
68
|
|
|
* @param \Drupal\graphql\GraphQL\Execution\QueryVisitor\QueryVisitorInterface $visitor |
69
|
|
|
* |
70
|
|
|
* @return mixed|void |
71
|
|
|
*/ |
72
|
|
|
protected function reduceOperation(Query $query, $carry, $current, QueryVisitorInterface $visitor) { |
73
|
|
|
if (!($current instanceof AbstractObjectType) || !$current->hasField($query->getName())) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (($name = $query->getName()) && $current->hasField($name)) { |
78
|
|
|
$operation = $current->getField($query->getName()); |
79
|
|
|
$walker = $this->walkQuery($query, $operation, $visitor); |
80
|
|
|
|
81
|
|
|
while ($walker->valid()) { |
82
|
|
|
/** @var \Youshido\GraphQL\Parser\Ast\Field $field */ |
83
|
|
|
/** @var \Youshido\GraphQL\Field\Field $ast */ |
84
|
|
|
list($field, $ast, $child) = $walker->current(); |
85
|
|
|
|
86
|
|
|
$args = $field->getKeyValueArguments(); |
87
|
|
|
$result = $visitor->visit($args, $ast, $child); |
88
|
|
|
$carry = $visitor->reduce($carry, $result); |
89
|
|
|
|
90
|
|
|
$walker->send($result); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $carry; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $node |
99
|
|
|
* @param \Youshido\GraphQL\Field\FieldInterface $current |
100
|
|
|
* @param \Drupal\graphql\GraphQL\Execution\QueryVisitor\QueryVisitorInterface $visitor |
101
|
|
|
* |
102
|
|
|
* @return \Generator |
103
|
|
|
*/ |
104
|
|
|
protected function walkQuery(AbstractAst $node, FieldInterface $current, QueryVisitorInterface $visitor) { |
105
|
|
|
$carry = $visitor->initial(); |
106
|
|
|
|
107
|
|
|
if (!($node instanceof Field)) { |
108
|
|
|
/** @var \Youshido\GraphQL\Parser\Ast\Field $field */ |
109
|
|
|
foreach ($node->getFields() as $field) { |
|
|
|
|
110
|
|
|
if ($field instanceof FragmentInterface) { |
111
|
|
|
if ($field instanceof FragmentReference) { |
112
|
|
|
$field = $this->request->getFragment($field->getName()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$walker = $this->walkQuery($field, $current, $visitor); |
|
|
|
|
116
|
|
|
$next = $walker->current(); |
117
|
|
|
|
118
|
|
View Code Duplication |
while ($walker->valid()) { |
|
|
|
|
119
|
|
|
$received = (yield $next); |
120
|
|
|
$carry = $visitor->reduce($carry, $received); |
121
|
|
|
$next = $walker->send($received); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
else { |
125
|
|
|
$type = $this->getType($node, $current); |
126
|
|
|
$name = $field->getName(); |
127
|
|
|
|
128
|
|
|
if (($type instanceof AbstractObjectType || $type instanceof AbstractInterfaceType) && $type->hasField($name)) { |
129
|
|
|
$ast = $type->getField($name); |
130
|
|
|
$walker = $this->walkQuery($field, $ast, $visitor); |
131
|
|
|
$next = $walker->current(); |
132
|
|
|
|
133
|
|
View Code Duplication |
while ($walker->valid()) { |
|
|
|
|
134
|
|
|
$received = (yield $next); |
135
|
|
|
$carry = $visitor->reduce($carry, $received); |
136
|
|
|
$next = $walker->send($received); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($node instanceof Query || $node instanceof Field) { |
144
|
|
|
yield [$node, $current, $carry]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param \Youshido\GraphQL\Parser\Ast\AbstractAst $node |
150
|
|
|
* @param \Youshido\GraphQL\Field\FieldInterface $current |
151
|
|
|
* |
152
|
|
|
* @return null|\Youshido\GraphQL\Type\AbstractType|\Youshido\GraphQL\Type\TypeInterface |
153
|
|
|
*/ |
154
|
|
|
protected function getType(AbstractAst $node, FieldInterface $current) { |
155
|
|
|
if ($node instanceof Fragment && $name = $node->getModel()) { |
156
|
|
|
return isset($this->types[$name]) ? $this->types[$name] : NULL; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($node instanceof TypedFragmentReference && $name = $node->getTypeName()) { |
160
|
|
|
return isset($this->types[$name]) ? $this->types[$name] : NULL; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $current->getType()->getNamedType(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: