|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of GraphQL project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
|
6
|
|
|
* created: 2/5/17 12:23 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Youshido\Tests\Performance; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Execution\Processor; |
|
13
|
|
|
use Youshido\GraphQL\Schema\Schema; |
|
14
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
|
15
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
|
16
|
|
|
use Youshido\GraphQL\Type\Scalar\IdType; |
|
17
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
|
18
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
|
19
|
|
|
|
|
20
|
|
|
class NPlusOneTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
private function getDataForPosts() |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* We could make a DB request here, as a simplified version: |
|
27
|
|
|
* SELECT * FROM posts p LEFT JOIN authors a ON (a.id = p.author_id) LIMIT 10, 10 |
|
28
|
|
|
*/ |
|
29
|
|
|
$authors = [ |
|
30
|
|
|
['id' => 1, 'name' => 'John'], |
|
31
|
|
|
['id' => 2, 'name' => 'Alex'], |
|
32
|
|
|
['id' => 3, 'name' => 'Mike'], |
|
33
|
|
|
]; |
|
34
|
|
|
$posts = []; |
|
35
|
|
|
for ($i = 0; $i < 10; $i++) { |
|
36
|
|
|
$posts[] = [ |
|
37
|
|
|
'id' => $i + 1, |
|
38
|
|
|
'title' => sprintf('Post title $%s', $i), |
|
39
|
|
|
'author' => $authors[$i % 3] |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $posts; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testHigherResolver() |
|
47
|
|
|
{ |
|
48
|
|
|
$authorType = new ObjectType([ |
|
49
|
|
|
'name' => 'Author', |
|
50
|
|
|
'fields' => [ |
|
51
|
|
|
'id' => new IdType(), |
|
52
|
|
|
'name' => new StringType(), |
|
53
|
|
|
] |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
$postType = new ObjectType([ |
|
57
|
|
|
'name' => 'Post', |
|
58
|
|
|
'fields' => [ |
|
59
|
|
|
'id' => new IntType(), |
|
60
|
|
|
'title' => new StringType(), |
|
61
|
|
|
'author' => $authorType, |
|
62
|
|
|
] |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
$processor = new Processor(new Schema([ |
|
66
|
|
|
'query' => new ObjectType([ |
|
67
|
|
|
'fields' => [ |
|
68
|
|
|
'posts' => [ |
|
69
|
|
|
'type' => new ListType($postType), |
|
70
|
|
|
'resolve' => function ($source, $args, $info) { |
|
|
|
|
|
|
71
|
|
|
return $this->getDataForPosts(); |
|
72
|
|
|
} |
|
73
|
|
|
] |
|
74
|
|
|
] |
|
75
|
|
|
]) |
|
76
|
|
|
])); |
|
77
|
|
|
|
|
78
|
|
|
$data = $processor->processPayload('{ posts { id, title, author { id, name } } }')->getResponseData(); |
|
79
|
|
|
$this->assertNotEmpty($data['data']['posts'][0]['author']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.