Skip to content

Commit 962268c

Browse files
authored
Resolving Interface/Type naming conflicts. (#556)
* Resolving Interface/Type naming conflicts. * Only rename on type difference.
1 parent c6659f2 commit 962268c

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

modules/graphql_core/src/Plugin/Deriver/Types/EntityBundleDeriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function getDerivativeDefinitions($basePluginDefinition) {
6868
}
6969

7070
// Only create a bundle type for entity types that support bundles.
71-
if (!$type->hasKey('bundle')) {
71+
if (!$type->hasKey('bundle') || !array_key_exists($typeId, $bundles)) {
7272
continue;
7373
}
7474

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Drupal\Tests\graphql_core\Kernel\Entity;
4+
5+
use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase;
6+
7+
/**
8+
* Test entity property naming conflict resolution.
9+
*
10+
* @group graphql_core
11+
*/
12+
class EntityPropertyConflict extends GraphQLContentTestBase {
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public static $modules = [
18+
'comment',
19+
];
20+
21+
/**
22+
* Check proper behavior in case of a naming conflict.
23+
*/
24+
public function testNamingConflict() {
25+
$this->assertGraphQLFields([
26+
['Comment', 'entityId', 'String'],
27+
['Comment', 'entityIdOfComment', 'FieldCommentEntityId'],
28+
]);
29+
}
30+
31+
}

src/Plugin/Deriver/PluggableSchemaDeriver.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ protected function buildTypeReferenceMap(array $types) {
217217
*/
218218
protected function buildFieldAssociationMap(FieldPluginManager $manager, $types) {
219219
$definitions = $manager->getDefinitions();
220-
$fields = array_reduce(array_keys($definitions), function ($carry, $id) use ($definitions) {
220+
$fields = array_reduce(array_keys($definitions), function ($carry, $id) use ($definitions, $types) {
221221
$current = $definitions[$id];
222222
$parents = $current['parents'] ?: ['Root'];
223223

224-
return array_reduce($parents, function ($carry, $parent) use ($current, $id) {
224+
return array_reduce($parents, function ($carry, $parent) use ($current, $id, $types) {
225225
// Allow plugins to define a different name for each parent.
226226
if (strpos($parent, ':') !== FALSE) {
227227
list($parent, $name) = explode(':', $parent);
@@ -239,6 +239,28 @@ protected function buildFieldAssociationMap(FieldPluginManager $manager, $types)
239239
}, $carry);
240240
}, []);
241241

242+
$rename = [];
243+
244+
foreach ($fields as $parent => $fieldList) {
245+
foreach ($fieldList as $field => $info) {
246+
if (!array_key_exists($parent, $types)) {
247+
continue;
248+
}
249+
foreach ($types[$parent]['definition']['interfaces'] as $interface) {
250+
if (isset($fields[$interface][$field]) && $definitions[$fields[$interface][$field]['id']]['type'] != $definitions[$info['id']]['type']) {
251+
$rename[$parent][$field] = TRUE;
252+
}
253+
}
254+
}
255+
}
256+
257+
foreach ($rename as $parent => $names) {
258+
foreach (array_keys($names) as $name) {
259+
$fields[$parent][$name . 'Of' . $parent] = $fields[$parent][$name];
260+
unset($fields[$parent][$name]);
261+
}
262+
}
263+
242264
// Only return fields for types that are actually fieldable.
243265
$fieldable = [GRAPHQL_TYPE_PLUGIN, GRAPHQL_INTERFACE_PLUGIN];
244266
$fields = array_intersect_key($fields, array_filter($types, function ($type) use ($fieldable) {

0 commit comments

Comments
 (0)