-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwith-dataloader-native-promise.php
46 lines (38 loc) · 1.31 KB
/
with-dataloader-native-promise.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
<?php
use GraphQL\Tests\StarWarsData;
use Overblog\DataLoader\DataLoader;
use Overblog\DataLoader\Promise\Adapter\Webonyx\GraphQL\SyncPromiseAdapter;
use Overblog\PromiseAdapter\Adapter\WebonyxGraphQLSyncPromiseAdapter;
require __DIR__.'/../vendor/autoload.php';
$calls = 0;
$callsIds = [];
$graphQLSyncPromiseAdapter = new SyncPromiseAdapter();
$promiseAdapter = new WebonyxGraphQLSyncPromiseAdapter($graphQLSyncPromiseAdapter);
$batchLoadFn = function ($ids) use (&$calls, &$callsIds, $promiseAdapter) {
$callsIds[] = $ids;
++$calls;
$allCharacters = StarWarsData::humans() + StarWarsData::droids();
$characters = array_intersect_key($allCharacters, array_flip($ids));
return $promiseAdapter->createAll(array_values($characters));
};
$dataLoader = new DataLoader($batchLoadFn, $promiseAdapter);
$schema = createSchema(
function ($character) use ($dataLoader) {
$promise = $dataLoader->loadMany($character['friends']);
return $promise;
},
function ($root, $args) use ($dataLoader) {
$promise = $dataLoader->load($args['id']);
return $promise;
}
);
echo "With DataLoader (Using native promise):\n\n";
executeQueries(
$schema,
$calls,
$callsIds,
$graphQLSyncPromiseAdapter,
function () use ($dataLoader) {
$dataLoader->clearAll();
}
);