|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Tests\Type; |
|
3
|
|
|
|
|
4
|
|
|
use GraphQL\Error\InvariantViolation; |
|
5
|
|
|
use GraphQL\Type\Definition\Directive; |
|
6
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
|
7
|
|
|
use GraphQL\Type\Definition\InterfaceType; |
|
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
9
|
|
|
use GraphQL\Type\Definition\Type; |
|
10
|
|
|
use GraphQL\Type\Schema; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
class SchemaTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private $interfaceType; |
|
16
|
|
|
|
|
17
|
|
|
private $implementingType; |
|
18
|
|
|
|
|
19
|
|
|
private $directiveInputType; |
|
20
|
|
|
|
|
21
|
|
|
private $wrappedDirectiveInputType; |
|
22
|
|
|
|
|
23
|
|
|
private $directive; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Schema */ |
|
26
|
|
|
private $schema; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->interfaceType = new InterfaceType([ |
|
31
|
|
|
'name' => 'Interface', |
|
32
|
|
|
'fields' => ['fieldName' => ['type' => Type::string()]], |
|
33
|
|
|
]); |
|
34
|
|
|
|
|
35
|
|
|
$this->implementingType = new ObjectType([ |
|
36
|
|
|
'name' => 'Object', |
|
37
|
|
|
'interfaces' => [$this->interfaceType], |
|
38
|
|
|
'fields' => ['fieldName' => ['type' => Type::string(), 'resolve' => function () { |
|
39
|
|
|
return ''; |
|
40
|
|
|
}]], |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$this->directiveInputType = new InputObjectType([ |
|
44
|
|
|
'name' => 'DirInput', |
|
45
|
|
|
'fields' => [ |
|
46
|
|
|
'field' => [ |
|
47
|
|
|
'type' => Type::string(), |
|
48
|
|
|
], |
|
49
|
|
|
], |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
$this->wrappedDirectiveInputType = new InputObjectType([ |
|
53
|
|
|
'name' => 'WrappedDirInput', |
|
54
|
|
|
'fields' => [ |
|
55
|
|
|
'field' => [ |
|
56
|
|
|
'type' => Type::string(), |
|
57
|
|
|
], |
|
58
|
|
|
], |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
$this->directive = new Directive([ |
|
62
|
|
|
'name' => 'dir', |
|
63
|
|
|
'locations' => ['OBJECT'], |
|
64
|
|
|
'args' => [ |
|
65
|
|
|
'arg' => [ |
|
66
|
|
|
'type' => $this->directiveInputType, |
|
67
|
|
|
], |
|
68
|
|
|
'argList' => [ |
|
69
|
|
|
'type' => Type::listOf($this->wrappedDirectiveInputType), |
|
70
|
|
|
], |
|
71
|
|
|
], |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
|
|
$this->schema = new Schema([ |
|
75
|
|
|
'query' => new ObjectType([ |
|
76
|
|
|
'name' => 'Query', |
|
77
|
|
|
'fields' => [ |
|
78
|
|
|
'getObject' => [ |
|
79
|
|
|
'type' => $this->interfaceType, |
|
80
|
|
|
'resolve' => function () { |
|
81
|
|
|
return []; |
|
82
|
|
|
}, |
|
83
|
|
|
], |
|
84
|
|
|
], |
|
85
|
|
|
]), |
|
86
|
|
|
'directives' => [$this->directive], |
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Type System: Schema |
|
91
|
|
|
// Getting possible types |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @it throws human-reable error if schema.types is not defined |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testThrowsHumanReableErrorIfSchemaTypesIsNotDefined() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->markTestSkipped("Can't check interface implementations without full schema scan"); |
|
99
|
|
|
|
|
100
|
|
|
$this->expectException(InvariantViolation::class); |
|
101
|
|
|
$this->expectExceptionMessage( |
|
102
|
|
|
'Could not find possible implementing types for Interface in schema. ' . |
|
103
|
|
|
'Check that schema.types is defined and is an array of all possible ' . |
|
104
|
|
|
'types in the schema.' |
|
105
|
|
|
); |
|
106
|
|
|
$this->schema->isPossibleType($this->interfaceType, $this->implementingType); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Type Map |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @it includes input types only used in directives |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testIncludesInputTypesOnlyUsedInDirectives() |
|
115
|
|
|
{ |
|
116
|
|
|
$typeMap = $this->schema->getTypeMap(); |
|
117
|
|
|
$this->assertArrayHasKey('DirInput', $typeMap); |
|
118
|
|
|
$this->assertArrayHasKey('WrappedDirInput', $typeMap); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|