/
QueryAnalyzerTest.php
158 lines (116 loc) · 3.38 KB
/
QueryAnalyzerTest.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
class QueryAnalyzerTest extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
public $post_id;
public function _setUp():void {
parent::_setUp();
$this->post_id = self::factory()->post->create([
'post_status' => 'publish',
'post_title' => 'test post'
]);
WPGraphQL::clear_schema();
}
public function _tearDown():void {
wp_delete_post( $this->post_id, true );
parent::_tearDown();
}
public function testListTypes() {
$query = '{ posts { nodes { id, title } } }';
$request = graphql([
'query' => $query,
], true);
// before execution, this should be null
$this->assertEmpty( $request->get_query_analyzer()->get_list_types() );
// execute the query
$request->execute();
// get the list types that were generated during execution
$list_types = $request->get_query_analyzer()->get_list_types();
// Assert the expected list types are returned
$this->assertEqualSets( [ 'list:post' ], $list_types );
}
public function testListModels() {
$query = '{ posts { nodes { id, title } } }';
$request = graphql([
'query' => $query,
], true);
// before execution, this should be null (no nodes have loaded)
$this->assertEmpty( $request->get_query_analyzer()->get_runtime_nodes() );
// execute the request
$request->execute();
$nodes = $request->get_query_analyzer()->get_runtime_nodes();
$node_id = \GraphQLRelay\Relay::toGlobalId( 'post', $this->post_id );
$this->assertEqualSets( [ $node_id ], $nodes );
}
public function testQueryTypes() {
$query = '{ posts { nodes { id, title } } }';
$request = graphql([
'query' => $query,
], true);
// before execution, this should be null
$this->assertEmpty( $request->get_query_analyzer()->get_query_types() );
// Execute the request
$request->execute();
$types = $request->get_query_analyzer()->get_query_types();
$this->assertEqualSets( [ 'rootquery', 'rootquerytopostconnection', 'post' ], $types );
}
public function testQueryForListOfNonNodeInterfaceTypesDoesntAddKeys() {
// types that do not implement the "Node" interface shouldn't be tracked as keys
// in the Query Analyzer
add_action( 'graphql_register_types', function() {
register_graphql_interface_type( 'TestInterface', [
'eagerlyLoadType' => true,
'fields' => [
'test' => [
'type' => 'String',
],
],
'resolveType' => function () {
return 'TestType';
},
] );
register_graphql_object_type( 'TestType', [
'eagerlyLoadType' => true,
'interfaces' => [ 'TestInterface' ],
'fields' => [
'test' => [
'type' => 'String',
],
],
] );
register_graphql_field( 'Post', 'testField', [
'type' => [ 'list_of' => 'TestInterface' ],
'resolve' => function () {
return [
[
'test' => 'value',
],
[
'test' => 'value',
],
];
},
] );
} );
$query = '
{
posts {
nodes {
testField {
test
}
}
}
}
';
$request = graphql([
'query' => $query
], true );
$request->execute();
$list_types = $request->get_query_analyzer()->get_list_types();
codecept_debug( $list_types );
$keys_array = $list_types;
codecept_debug( $list_types );
$this->assertNotContains( 'list:testinterface', $list_types );
$this->assertNotContains( 'list:testtype', $list_types );
$this->assertContains( 'list:post', $list_types );
}
}