Skip to content

Commit 051766a

Browse files
authored
fix(resolver): Rewrite fromPath to use PropertyPath data producer (#1089)
1 parent 3315f5d commit 051766a

5 files changed

Lines changed: 76 additions & 37 deletions

File tree

src/GraphQL/ResolverBuilder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Drupal\graphql\GraphQL\Resolver\DefaultValue;
1212
use Drupal\graphql\GraphQL\Resolver\Map;
1313
use Drupal\graphql\GraphQL\Resolver\ParentValue;
14-
use Drupal\graphql\GraphQL\Resolver\Path;
1514
use Drupal\graphql\GraphQL\Resolver\SourceContext;
1615
use Drupal\graphql\GraphQL\Resolver\Tap;
1716
use Drupal\graphql\GraphQL\Resolver\Value;
@@ -94,10 +93,13 @@ public function cond(array $branches) {
9493
* @param string $path
9594
* @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface $value
9695
*
97-
* @return \Drupal\graphql\GraphQL\Resolver\Path
96+
* @return \Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerProxy
9897
*/
9998
public function fromPath($type, $path, ResolverInterface $value = NULL) {
100-
return new Path($type, $path, $value);
99+
return $this->produce('property_path')
100+
->map('type', $this->fromValue($type))
101+
->map('path', $this->fromValue($path))
102+
->map('value', $value ?: $this->fromParent());
101103
}
102104

103105
/**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type: module
2+
name: GraphQL Resolver Builder Test
3+
description: Tests the graphql resolver builder functionality.
4+
package: Testing
5+
core_version_requirement: ^8 || ^9
6+
hidden: TRUE
7+
dependencies:
8+
- graphql:graphql
9+
- drupal:typed_data
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Drupal\graphql_resolver_builder_test\Plugin\DataType;
4+
5+
use Drupal\Core\TypedData\Plugin\DataType\Map;
6+
7+
/**
8+
* A tree data type for testing.
9+
*
10+
* @DataType(
11+
* id = "tree",
12+
* label = @Translation("Tree"),
13+
* definition_class = "\Drupal\graphql_resolver_builder_test\TypedData\Definition\TreeDefinition"
14+
* )
15+
*/
16+
class Tree extends Map {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Drupal\graphql_resolver_builder_test\TypedData\Definition;
4+
5+
use Drupal\Core\TypedData\ComplexDataDefinitionBase;
6+
use Drupal\Core\TypedData\DataDefinition;
7+
8+
/**
9+
* Data definition for the Tree data type.
10+
*/
11+
class TreeDefinition extends ComplexDataDefinitionBase {
12+
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
public function getPropertyDefinitions() {
17+
if (!isset($this->propertyDefinitions)) {
18+
$info = &$this->propertyDefinitions;
19+
20+
$info['left'] = self::create('tree')
21+
->setLabel('Left branch');
22+
23+
$info['right'] = self::create('tree')
24+
->setLabel('Right branch');
25+
26+
$info['value'] = DataDefinition::create('string')
27+
->setLabel('Leaf value');
28+
}
29+
return $this->propertyDefinitions;
30+
}
31+
32+
}

tests/src/Kernel/ResolverBuilderTest.php

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
namespace Drupal\Tests\graphql\Kernel;
44

55
use GraphQL\Deferred;
6-
use Drupal\Core\TypedData\TypedDataManagerInterface;
7-
use Drupal\Core\TypedData\ComplexDataInterface;
8-
use Drupal\Core\TypedData\TypedDataInterface;
96
use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
107

118
/**
@@ -21,6 +18,7 @@ class ResolverBuilderTest extends GraphQLTestBase {
2118
public static $modules = [
2219
'graphql',
2320
'typed_data',
21+
'graphql_resolver_builder_test',
2422
];
2523

2624
/**
@@ -132,38 +130,20 @@ public function testFromArgument() {
132130
* @covers ::fromPath
133131
*/
134132
public function testFromPath() {
135-
$manager = $this->createMock(TypedDataManagerInterface::class);
136-
$manager->expects($this->any())
137-
->method('getDefinition')
138-
->will($this->returnValueMap([
139-
'tree_mock' => ['class' => '\Drupal\Core\TypedData\ComplexDataInterface'],
140-
]));
141-
142-
$this->container->set('typed_data_manager', $manager);
143-
144-
$uri = $this->prophesize(TypedDataInterface::class);
145-
$uri->getValue()->willReturn('<front>');
146-
147-
$path = $this->prophesize(ComplexDataInterface::class);
148-
$path->get('uri')->willReturn($uri);
149-
$path->getValue()->willReturn([]);
150-
151-
$tree = $this->prophesize(ComplexDataInterface::class);
152-
$tree->get('path')->willReturn($path);
153-
$tree->getValue()->willReturn([]);
154-
155-
$manager->expects($this->any())
156-
->method('create')
157-
->willReturn($tree->reveal());
158-
159-
$this->mockResolver('Query', 'tree', $this->builder->fromValue([
160-
'path' => [
161-
'uri' => '<front>',
162-
'path_name' => 'Front page',
163-
],
164-
]));
133+
$manager = $this->container->get('typed_data_manager');
134+
$tree_definition = $manager->createDataDefinition('tree');
135+
/** @var \Drupal\graphql_resolver_builder_test\Plugin\DataType\Tree $right */
136+
$right = $manager->create($tree_definition);
137+
$right->set('value', 'Front page');
138+
/** @var \Drupal\graphql_resolver_builder_test\Plugin\DataType\Tree $tree */
139+
$tree = $manager->create($tree_definition);
140+
$tree->set('left', [
141+
'value' => '<front>',
142+
'right' => $right,
143+
]);
165144

166-
$this->mockResolver('Tree', 'uri', $this->builder->fromPath('tree', 'path.uri'));
145+
$this->mockResolver('Query', 'tree', $this->builder->fromValue($tree));
146+
$this->mockResolver('Tree', 'uri', $this->builder->fromPath('tree', 'left.value'));
167147

168148
$query = <<<GQL
169149
query {

0 commit comments

Comments
 (0)