-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataSourceResolver.php
More file actions
69 lines (55 loc) · 1.81 KB
/
DataSourceResolver.php
File metadata and controls
69 lines (55 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Garlic\Wrapper\Service\GraphQL\DataSource;
use Youshido\GraphQL\Execution\DeferredResolver;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Parser\Ast\Field;
class DataSourceResolver
{
/**
* @var DataSourceInterface|null
*/
protected $dataSource;
/**
* @var ResolveInfo
*/
protected $resolverInfo;
protected $value;
/**
* @var DataSourceRelationInterface
*/
private $relation;
public static function build(string $dataSourceAlias, string $relationAlias): callable
{
return function ($value, $args, ResolveInfo $resolveInfo) use ($dataSourceAlias, $relationAlias) {
$container = $resolveInfo->getContainer();
return (new static($container->get($dataSourceAlias), $container->get($relationAlias)))(...func_get_args());
};
}
/**
* DataSourceResolver constructor.
*/
public function __construct(DataSourceInterface $dataSource, DataSourceRelationInterface $relation)
{
$this->dataSource = $dataSource;
$this->relation = $relation;
}
public function __invoke($value, $args, ResolveInfo $resolveInfo): DeferredResolver
{
$this->resolverInfo = $resolveInfo;
$this->value = $value;
$this->dataSource->enqueue($this->handleRelationCallback());
return new DeferredResolver(function () {
return $this->dataSource->resolve($this->handleRelationCallback(), $this->getFields());
});
}
protected function handleRelationCallback(): array
{
return $this->relation->relation($this->value);
}
protected function getFields(): array
{
return array_map(function (Field $field) {
return $field->getName();
}, $this->resolverInfo->getFieldASTList());
}
}