This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
DeferredResolverTest.php
136 lines (120 loc) · 3.63 KB
/
DeferredResolverTest.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
<?php
namespace Digia\GraphQL\Test\Functional\Execution;
use React\Promise\Promise;
use function Digia\GraphQL\graphql;
use function Digia\GraphQL\Type\newList;
use function Digia\GraphQL\Type\newObjectType;
use function Digia\GraphQL\Type\newSchema;
use function Digia\GraphQL\Type\stringType;
class DirectorBuffer
{
protected static $directorsIds = [];
protected static $authors = [];
public static function add(int $id)
{
self::$directorsIds[] = $id;
}
public static function get(int $id)
{
return self::$authors[$id];
}
public static function loadBuffered(): void
{
self::$authors = [
42 => [
'name' => 'George Lucas',
],
43 => [
'name' => 'Irvin Kershner'
]
];
}
}
class DeferredResolverTest extends ResolveTest
{
/**
* @throws \Digia\GraphQL\Error\InvariantException
*/
public function testUsingFieldDeferredResolver()
{
$movies = [
[
'title' => 'Episode IV – A New Hope',
'directorId' => 42
],
[
'title' => 'Episode V – The Empire Strikes Back',
'directorId' => 43
]
];
$directorType = newObjectType([
'name' => 'Director',
'description' => 'Director of the movie',
'fields' => [
'name' => [
'type' => stringType(),
]
]
]);
$movieType = newObjectType([
'name' => 'Movie',
'description' => 'A movie',
'fields' => [
'title' => ['type' => stringType()],
'director' => [
'type' => $directorType,
'resolve' => function ($movie, $args) {
DirectorBuffer::add($movie['directorId']);
return new Promise(function (callable $resolve, callable $reject) use ($movie) {
DirectorBuffer::loadBuffered();
$resolve(DirectorBuffer::get($movie['directorId']));
});
}
]
]
]);
$schema = newSchema([
'query' => newObjectType([
'name' => 'Query',
'description' => '',
'fields' => [
'movies' => [
'type' => newList($movieType),
'resolve' => function ($source, $args) use ($movies) {
return $movies;
}
]
]
])
]);
$query = '
{
movies {
title
director {
name
}
}
}
';
$result = graphql($schema, $query, $movies);
$this->assertEquals([
'data' => [
'movies' => [
[
'title' => 'Episode IV – A New Hope',
'director' => [
'name' => 'George Lucas'
]
],
[
'title' => 'Episode V – The Empire Strikes Back',
'director' => [
'name' => 'Irvin Kershner'
]
]
]
]
], $result);
}
}