-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathInterfaceType.php
More file actions
124 lines (104 loc) · 3.77 KB
/
InterfaceType.php
File metadata and controls
124 lines (104 loc) · 3.77 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Utils\Utils;
/**
* @phpstan-import-type ResolveType from AbstractType
* @phpstan-import-type ResolveValue from AbstractType
* @phpstan-import-type FieldsConfig from FieldDefinition
*
* @phpstan-type InterfaceTypeReference InterfaceType|callable(): InterfaceType
* @phpstan-type InterfaceConfig array{
* name?: string|null,
* description?: string|null,
* fields: FieldsConfig,
* interfaces?: iterable<InterfaceTypeReference>|callable(): iterable<InterfaceTypeReference>,
* resolveType?: ResolveType|null,
* resolveValue?: ResolveValue|null,
* directives?: array<DirectiveNode>|null,
* astNode?: InterfaceTypeDefinitionNode|null,
* extensionASTNodes?: array<InterfaceTypeExtensionNode>|null
* }
*/
class InterfaceType extends Type implements AbstractType, OutputType, CompositeType, NullableType, HasFieldsType, NamedType, ImplementingType
{
use HasFieldsTypeImplementation;
use NamedTypeImplementation;
use ImplementingTypeImplementation;
public ?InterfaceTypeDefinitionNode $astNode;
/** @var array<InterfaceTypeExtensionNode> */
public array $extensionASTNodes;
/** @var array<DirectiveNode> */
public array $directives;
/** @phpstan-var InterfaceConfig */
public array $config;
/**
* @phpstan-param InterfaceConfig $config
*
* @throws InvariantViolation
*/
public function __construct(array $config)
{
$this->name = $config['name'] ?? $this->inferName();
$this->description = $config['description'] ?? null;
$this->astNode = $config['astNode'] ?? null;
$this->extensionASTNodes = $config['extensionASTNodes'] ?? [];
$this->directives = $config['directives'] ?? [];
$this->config = $config;
}
/**
* @param mixed $type
*
* @throws InvariantViolation
*/
public static function assertInterfaceType($type): self
{
if (! $type instanceof self) {
$notInterfaceType = Utils::printSafe($type);
throw new InvariantViolation("Expected {$notInterfaceType} to be a GraphQL Interface type.");
}
return $type;
}
public function resolveValue($objectValue, $context, ResolveInfo $info)
{
if (isset($this->config['resolveValue'])) {
return ($this->config['resolveValue'])($objectValue, $context, $info);
}
return $objectValue;
}
public function resolveType($objectValue, $context, ResolveInfo $info)
{
if (isset($this->config['resolveType'])) {
return ($this->config['resolveType'])($objectValue, $context, $info);
}
return null;
}
/**
* @throws Error
* @throws InvariantViolation
*/
public function assertValid(): void
{
Utils::assertValidName($this->name);
$resolveType = $this->config['resolveType'] ?? null;
// @phpstan-ignore-next-line unnecessary according to types, but can happen during runtime
if ($resolveType !== null && ! is_callable($resolveType)) {
$notCallable = Utils::printSafe($resolveType);
throw new InvariantViolation("{$this->name} must provide \"resolveType\" as null or a callable, but got: {$notCallable}.");
}
$this->assertValidInterfaces();
}
public function astNode(): ?InterfaceTypeDefinitionNode
{
return $this->astNode;
}
/** @return array<InterfaceTypeExtensionNode> */
public function extensionASTNodes(): array
{
return $this->extensionASTNodes;
}
}