Skip to content

Commit bfec5c2

Browse files
authored
Use original sorting order in entity reference query. (#743)
1 parent a8bc457 commit bfec5c2

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

modules/graphql_core/src/Plugin/GraphQL/Fields/EntityQuery/EntityQueryEntities.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,18 @@ public function resolveValues($value, array $args, ResolveContext $context, Reso
109109
$result = $value->execute();
110110
$metadata = $value->getMetaData('graphql_context');
111111

112+
if (isset($metadata['ids']) && $sorting = $metadata['ids']) {
113+
$sorting = array_flip(array_values($sorting));
114+
uasort($result, function ($a, $b) use ($sorting) {
115+
return $sorting[$a] - $sorting[$b];
116+
});
117+
}
118+
112119
if ($value->hasTag('revisions')) {
120+
// If this is a revision query, the version ids are the array keys.
113121
return $this->resolveFromRevisionIds($type, array_keys($result), $metadata, $args, $context, $info);
114122
}
115123

116-
// If this is a revision query, the version ids are the array keys.
117124
return $this->resolveFromEntityIds($type, array_values($result), $metadata, $args, $context, $info);
118125
}
119126
}

modules/graphql_core/src/Plugin/GraphQL/Fields/EntityReference/EntityReferenceQuery.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,37 @@ public function getBaseQuery($value, array $args, ResolveContext $context, Resol
4040
if ($value instanceof ContentEntityInterface) {
4141
$query = parent::getBaseQuery($value, $args, $context, $info);
4242

43-
// Add the target field condition to the query.
44-
$definition = $this->getPluginDefinition();
45-
$key = $definition['entity_key'];
46-
$field = $definition['field'];
47-
$ids = array_map(function ($item) {
48-
return $item['target_id'];
49-
}, $value->get($field)->getValue());
50-
43+
$metadata = $query->getMetadata('graphql_context');
44+
$ids = $metadata['ids'];
5145
if (empty($ids)) {
5246
return NULL;
5347
}
5448

49+
$definition = $this->getPluginDefinition();
50+
$key = $definition['entity_key'];
5551
$operator = is_array($ids) ? 'IN' : '=';
5652
$query->condition($key, $ids, $operator);
5753

5854
return $query;
5955
}
6056
}
6157

58+
/**
59+
* {@inheritdoc}
60+
*/
61+
protected function getQueryContext($value, array $args, ResolveContext $context, ResolveInfo $info) {
62+
$context = parent::getQueryContext($value, $args, $context, $info);
63+
64+
// Add the target field condition to the query.
65+
$definition = $this->getPluginDefinition();
66+
$field = $definition['field'];
67+
$ids = array_map(function ($item) {
68+
return $item['target_id'];
69+
}, $value->get($field)->getValue());
70+
71+
return [
72+
'ids' => $ids,
73+
] + $context;
74+
}
75+
6276
}

0 commit comments

Comments
 (0)