Skip to content

Grouping implementor fields for abstract types in QueryPlan #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 85 additions & 35 deletions src/Type/Definition/QueryPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@ class QueryPlan
/** @var FragmentDefinitionNode[] */
private $fragments;

/** @var bool */
private $withTypeConditions;

/**
* @param FieldNode[] $fieldNodes
* @param mixed[] $variableValues
* @param FragmentDefinitionNode[] $fragments
* @param string[] $options
*/
public function __construct(ObjectType $parentType, Schema $schema, iterable $fieldNodes, array $variableValues, array $fragments)
public function __construct(ObjectType $parentType, Schema $schema, iterable $fieldNodes, array $variableValues, array $fragments, array $options)
{
$this->schema = $schema;
$this->variableValues = $variableValues;
$this->fragments = $fragments;
$this->schema = $schema;
$this->variableValues = $variableValues;
$this->fragments = $fragments;
$this->withTypeConditions = in_array('with-type-conditions', $options, true);
$this->analyzeQueryPlan($parentType, $fieldNodes);
}

Expand All @@ -73,7 +78,7 @@ public function getReferencedTypes() : array
public function hasType(string $type) : bool
{
return count(array_filter($this->getReferencedTypes(), static function (string $referencedType) use ($type) {
return $type === $referencedType;
return $type === $referencedType;
})) > 0;
}

Expand Down Expand Up @@ -110,6 +115,7 @@ public function subFields(string $typename) : array
private function analyzeQueryPlan(ObjectType $parentType, iterable $fieldNodes) : void
{
$queryPlan = [];

/** @var FieldNode $fieldNode */
foreach ($fieldNodes as $fieldNode) {
if (! $fieldNode->selectionSet) {
Expand All @@ -121,16 +127,17 @@ private function analyzeQueryPlan(ObjectType $parentType, iterable $fieldNodes)
$type = $type->getWrappedType();
}

$subfields = $this->analyzeSelectionSet($fieldNode->selectionSet, $type);
$selectionSet = $fieldNode->selectionSet;
$data = $this->withTypeConditions ? $this->analyzeSelectionSetWithTypeConditions($selectionSet, $type) : $this->analyzeSelectionSet($selectionSet, $type);

$this->types[$type->name] = array_unique(array_merge(
array_key_exists($type->name, $this->types) ? $this->types[$type->name] : [],
array_keys($subfields)
array_keys($this->withTypeConditions ? $data['fields'] : $data)
));

$queryPlan = array_merge_recursive(
$queryPlan,
$subfields
$data
);
}

Expand All @@ -144,47 +151,88 @@ private function analyzeQueryPlan(ObjectType $parentType, iterable $fieldNodes)
*
* @throws Error
*/
private function analyzeSelectionSet(SelectionSetNode $selectionSet, Type $parentType) : array
private function analyzeSelectionSetWithTypeConditions(SelectionSetNode $selectionSet, Type $parentType) : array
{
$fields = [];
$fields = [];
$typeConditions = [];

foreach ($selectionSet->selections as $selectionNode) {
if ($selectionNode instanceof FieldNode) {
if ($selectionNode instanceof FieldNode && ($parentType instanceof InterfaceType || $parentType instanceof ObjectType)) {
$fieldName = $selectionNode->name->value;
$type = $parentType->getField($fieldName);
$selectionType = $type->getType();

$subfields = [];
if ($selectionNode->selectionSet) {
$subfields = $this->analyzeSubFields($selectionType, $selectionNode->selectionSet);
$fieldData = & $fields[$fieldName];
$fieldData['type'] = $selectionType;
$fieldData['args'] = Values::getArgumentValues($type, $selectionNode, $this->variableValues);
$fieldData += $selectionNode->selectionSet ? $this->analyzeSubFields($selectionType, $selectionNode->selectionSet) : ['fields' => []];
} elseif ($selectionNode instanceof FragmentSpreadNode) {
$spreadName = $selectionNode->name->value;
$fragment = $this->fragments[$spreadName] ?? null;
if ($fragment) {
$type = $this->schema->getType($fragment->typeCondition->name->value);

$typeConditions[$type->name] = $this->arrayMergeDeep(
$typeConditions[$type->name] ?? [],
$this->analyzeSubFields($type, $fragment->selectionSet)
);
}
} elseif ($selectionNode instanceof InlineFragmentNode) {
$type = $this->schema->getType($selectionNode->typeCondition->name->value);

$typeConditions[$type->name] = $this->arrayMergeDeep(
$typeConditions[$type->name] ?? [],
$this->analyzeSubFields($type, $selectionNode->selectionSet)
);
}
}
unset($fieldData);

return ['fields' => $fields] + array_filter(['typeConditions' => $typeConditions]);
}

/**
* @param InterfaceType|ObjectType $parentType
*
* @return mixed[]
*
* @throws Error
*/
private function analyzeSelectionSet(SelectionSetNode $selectionSet, Type $parentType) : array
{
$fields = [];

$fields[$fieldName] = [
'type' => $selectionType,
'fields' => $subfields ?? [],
'args' => Values::getArgumentValues($type, $selectionNode, $this->variableValues),
];
foreach ($selectionSet->selections as $selectionNode) {
if ($selectionNode instanceof FieldNode && ($parentType instanceof InterfaceType || $parentType instanceof ObjectType)) {
$fieldName = $selectionNode->name->value;
$type = $parentType->getField($fieldName);
$selectionType = $type->getType();

$fieldData = & $fields[$fieldName];
$fieldData['type'] = $selectionType;
$fieldData['args'] = Values::getArgumentValues($type, $selectionNode, $this->variableValues);
$fieldData['fields'] = $selectionNode->selectionSet ? $this->analyzeSubFields($selectionType, $selectionNode->selectionSet) : [];
} elseif ($selectionNode instanceof FragmentSpreadNode) {
$spreadName = $selectionNode->name->value;
if (isset($this->fragments[$spreadName])) {
$fragment = $this->fragments[$spreadName];
$type = $this->schema->getType($fragment->typeCondition->name->value);
$subfields = $this->analyzeSubFields($type, $fragment->selectionSet);
$fragment = $this->fragments[$spreadName] ?? null;
if ($fragment) {
$type = $this->schema->getType($fragment->typeCondition->name->value);

$fields = $this->arrayMergeDeep(
$subfields,
$this->analyzeSubFields($type, $fragment->selectionSet),
$fields
);
}
} elseif ($selectionNode instanceof InlineFragmentNode) {
$type = $this->schema->getType($selectionNode->typeCondition->name->value);
$subfields = $this->analyzeSubFields($type, $selectionNode->selectionSet);
$type = $this->schema->getType($selectionNode->typeCondition->name->value);

$fields = $this->arrayMergeDeep(
$subfields,
$this->analyzeSubFields($type, $selectionNode->selectionSet),
$fields
);
}
}
unset($fieldData);

return $fields;
}
Expand All @@ -198,16 +246,18 @@ private function analyzeSubFields(Type $type, SelectionSetNode $selectionSet) :
$type = $type->getWrappedType();
}

$subfields = [];
if ($type instanceof ObjectType) {
$subfields = $this->analyzeSelectionSet($selectionSet, $type);
$this->types[$type->name] = array_unique(array_merge(
array_key_exists($type->name, $this->types) ? $this->types[$type->name] : [],
array_keys($subfields)
));
if (! $this->withTypeConditions && ! $type instanceof ObjectType) {
return [];
}

return $subfields;
$data = $this->withTypeConditions ? $this->analyzeSelectionSetWithTypeConditions($selectionSet, $type) : $this->analyzeSelectionSet($selectionSet, $type);

$this->types[$type->name] = array_unique(array_merge(
array_key_exists($type->name, $this->types) ? $this->types[$type->name] : [],
array_keys($this->withTypeConditions ? $data['fields'] : $data)
));

return $data;
}

/**
Expand Down
24 changes: 9 additions & 15 deletions src/Type/Definition/ResolveInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ class ResolveInfo
*/
public $variableValues = [];

/** @var QueryPlan */
private $queryPlan;

/**
* @param FieldNode[] $fieldNodes
* @param ScalarType|ObjectType|InterfaceType|UnionType|EnumType|ListOfType|NonNull $returnType
Expand Down Expand Up @@ -187,19 +184,16 @@ public function getFieldSelection($depth = 0)
return $fields;
}

public function lookAhead() : QueryPlan
public function lookAhead(string ...$options) : QueryPlan
{
if ($this->queryPlan === null) {
$this->queryPlan = new QueryPlan(
$this->parentType,
$this->schema,
$this->fieldNodes,
$this->variableValues,
$this->fragments
);
}

return $this->queryPlan;
return new QueryPlan(
$this->parentType,
$this->schema,
$this->fieldNodes,
$this->variableValues,
$this->fragments,
$options
);
}

/**
Expand Down
Loading