|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Schema\Building; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Language\Node\DirectiveDefinitionNode; |
|
6
|
|
|
use Digia\GraphQL\Language\Node\DocumentNode; |
|
7
|
|
|
use Digia\GraphQL\Language\Node\SchemaDefinitionNode; |
|
8
|
|
|
use Digia\GraphQL\Language\Node\TypeDefinitionNodeInterface; |
|
9
|
|
|
use Digia\GraphQL\Schema\DefinitionBuilderInterface; |
|
10
|
|
|
use Digia\GraphQL\Schema\ResolverRegistryInterface; |
|
11
|
|
|
use Digia\GraphQL\Type\Definition\Directive; |
|
12
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
|
13
|
|
|
use function Digia\GraphQL\Util\arraySome; |
|
14
|
|
|
|
|
15
|
|
|
class BuildingContext implements BuildingContextInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ResolverRegistryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $resolverRegistry; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var DefinitionBuilderInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $definitionBuilder; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var BuildInfo |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $info; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* BuilderContext constructor. |
|
34
|
|
|
* @param ResolverRegistryInterface $resolverRegistry |
|
35
|
|
|
* @param DefinitionBuilderInterface $definitionBuilder |
|
36
|
|
|
* @param BuildInfo $info |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
ResolverRegistryInterface $resolverRegistry, |
|
40
|
|
|
DefinitionBuilderInterface $definitionBuilder, |
|
41
|
|
|
BuildInfo $info |
|
42
|
|
|
) { |
|
43
|
|
|
$this->resolverRegistry = $resolverRegistry; |
|
44
|
|
|
$this->definitionBuilder = $definitionBuilder; |
|
45
|
|
|
$this->info = $info; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return TypeInterface|null |
|
50
|
|
|
*/ |
|
51
|
|
|
public function buildQueryType(): ?TypeInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$definition = $this->info->getOperationTypeDefinition('query'); |
|
54
|
|
|
return null !== $definition ? $this->definitionBuilder->buildType($definition) : null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return TypeInterface|null |
|
59
|
|
|
*/ |
|
60
|
|
|
public function buildMutationType(): ?TypeInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$definition = $this->info->getOperationTypeDefinition('mutation'); |
|
63
|
|
|
return null !== $definition ? $this->definitionBuilder->buildType($definition) : null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return TypeInterface|null |
|
68
|
|
|
*/ |
|
69
|
|
|
public function buildSubscriptionType(): ?TypeInterface |
|
70
|
|
|
{ |
|
71
|
|
|
$definition = $this->info->getOperationTypeDefinition('subscription'); |
|
72
|
|
|
return null !== $definition ? $this->definitionBuilder->buildType($definition) : null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return TypeInterface[] |
|
77
|
|
|
*/ |
|
78
|
|
|
public function buildTypes(): array |
|
79
|
|
|
{ |
|
80
|
|
|
return \array_map(function (TypeDefinitionNodeInterface $definition) { |
|
81
|
|
|
return $this->definitionBuilder->buildType($definition); |
|
82
|
|
|
}, \array_values($this->info->getTypeDefinitionMap())); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return Directive[] |
|
87
|
|
|
*/ |
|
88
|
|
|
public function buildDirectives(): array |
|
89
|
|
|
{ |
|
90
|
|
|
$directives = \array_map(function (DirectiveDefinitionNode $definition) { |
|
91
|
|
|
return $this->definitionBuilder->buildDirective($definition); |
|
92
|
|
|
}, $this->info->getDirectiveDefinitions()); |
|
93
|
|
|
|
|
94
|
|
|
$specifiedDirectivesMap = [ |
|
95
|
|
|
'skip' => SkipDirective(), |
|
96
|
|
|
'include' => IncludeDirective(), |
|
97
|
|
|
'deprecated' => DeprecatedDirective(), |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
foreach ($specifiedDirectivesMap as $name => $directive) { |
|
101
|
|
|
if (!arraySome($directives, function (Directive $directive) use ($name) { |
|
102
|
|
|
return $directive->getName() === $name; |
|
103
|
|
|
})) { |
|
104
|
|
|
$directives[] = $directive; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $directives; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return SchemaDefinitionNode|null |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getSchemaDefinition(): ?SchemaDefinitionNode |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->info->getSchemaDefinition(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|