Skip to content

Commit cc2f772

Browse files
committed
Renamings; merging fields inside fragments
1 parent c1795f9 commit cc2f772

File tree

2 files changed

+345
-28
lines changed

2 files changed

+345
-28
lines changed

src/Type/Definition/QueryPlan.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class QueryPlan
4242
private $fragments;
4343

4444
/** @var bool */
45-
private $withMetaFields;
45+
private $withTypeConditions;
4646

4747
/**
4848
* @param FieldNode[] $fieldNodes
@@ -52,10 +52,10 @@ class QueryPlan
5252
*/
5353
public function __construct(ObjectType $parentType, Schema $schema, iterable $fieldNodes, array $variableValues, array $fragments, array $options)
5454
{
55-
$this->schema = $schema;
56-
$this->variableValues = $variableValues;
57-
$this->fragments = $fragments;
58-
$this->withMetaFields = in_array('with-meta-fields', $options, true);
55+
$this->schema = $schema;
56+
$this->variableValues = $variableValues;
57+
$this->fragments = $fragments;
58+
$this->withTypeConditions = in_array('with-type-conditions', $options, true);
5959
$this->analyzeQueryPlan($parentType, $fieldNodes);
6060
}
6161

@@ -128,11 +128,11 @@ private function analyzeQueryPlan(ObjectType $parentType, iterable $fieldNodes)
128128
}
129129

130130
$selectionSet = $fieldNode->selectionSet;
131-
$data = $this->withMetaFields ? $this->analyzeSelectionSetWithMetaFields($selectionSet, $type) : $this->analyzeSelectionSet($selectionSet, $type);
131+
$data = $this->withTypeConditions ? $this->analyzeSelectionSetWithTypeConditions($selectionSet, $type) : $this->analyzeSelectionSet($selectionSet, $type);
132132

133133
$this->types[$type->name] = array_unique(array_merge(
134134
array_key_exists($type->name, $this->types) ? $this->types[$type->name] : [],
135-
array_keys($this->withMetaFields ? $data['fields'] : $data)
135+
array_keys($this->withTypeConditions ? $data['fields'] : $data)
136136
));
137137

138138
$queryPlan = array_merge_recursive(
@@ -151,11 +151,10 @@ private function analyzeQueryPlan(ObjectType $parentType, iterable $fieldNodes)
151151
*
152152
* @throws Error
153153
*/
154-
private function analyzeSelectionSetWithMetaFields(SelectionSetNode $selectionSet, Type $parentType) : array
154+
private function analyzeSelectionSetWithTypeConditions(SelectionSetNode $selectionSet, Type $parentType) : array
155155
{
156-
$fields = [];
157-
$fragments = [];
158-
$inlineFragments = [];
156+
$fields = [];
157+
$typeConditions = [];
159158

160159
foreach ($selectionSet->selections as $selectionNode) {
161160
if ($selectionNode instanceof FieldNode && ($parentType instanceof InterfaceType || $parentType instanceof ObjectType)) {
@@ -173,19 +172,23 @@ private function analyzeSelectionSetWithMetaFields(SelectionSetNode $selectionSe
173172
if ($fragment) {
174173
$type = $this->schema->getType($fragment->typeCondition->name->value);
175174

176-
$fragmentData = &$fragments[$spreadName];
177-
$fragmentData['type'] = $type;
178-
$fragmentData += $this->analyzeSubFields($type, $fragment->selectionSet);
175+
$typeConditions[$type->name] = $this->arrayMergeDeep(
176+
$this->analyzeSubFields($type, $fragment->selectionSet),
177+
$typeConditions[$type->name] ?? []
178+
);
179179
}
180180
} elseif ($selectionNode instanceof InlineFragmentNode) {
181181
$type = $this->schema->getType($selectionNode->typeCondition->name->value);
182182

183-
$inlineFragments[$type->name] = $this->analyzeSubFields($type, $selectionNode->selectionSet);
183+
$typeConditions[$type->name] = $this->arrayMergeDeep(
184+
$this->analyzeSubFields($type, $selectionNode->selectionSet),
185+
$typeConditions[$type->name] ?? []
186+
);
184187
}
185188
}
186-
unset($fieldData, $fragmentData);
189+
unset($fieldData);
187190

188-
return ['fields' => $fields] + array_filter(['fragments' => $fragments, 'inlineFragments' => $inlineFragments]);
191+
return ['fields' => $fields] + array_filter(['typeConditions' => $typeConditions]);
189192
}
190193

191194
/**
@@ -243,11 +246,15 @@ private function analyzeSubFields(Type $type, SelectionSetNode $selectionSet) :
243246
$type = $type->getWrappedType();
244247
}
245248

246-
$data = $this->withMetaFields ? $this->analyzeSelectionSetWithMetaFields($selectionSet, $type) : $this->analyzeSelectionSet($selectionSet, $type);
249+
if (! $this->withTypeConditions && ! $type instanceof ObjectType) {
250+
return [];
251+
}
252+
253+
$data = $this->withTypeConditions ? $this->analyzeSelectionSetWithTypeConditions($selectionSet, $type) : $this->analyzeSelectionSet($selectionSet, $type);
247254

248255
$this->types[$type->name] = array_unique(array_merge(
249256
array_key_exists($type->name, $this->types) ? $this->types[$type->name] : [],
250-
array_keys($this->withMetaFields ? $data['fields'] : $data)
257+
array_keys($this->withTypeConditions ? $data['fields'] : $data)
251258
));
252259

253260
return $data;

0 commit comments

Comments
 (0)