Skip to content

Commit fe23e64

Browse files
authored
Merge pull request #1 from mcg-web/webonyx-graphql-sync-promise
With webonyx/graphql sync promise
2 parents 28565ea + 90677f1 commit fe23e64

File tree

7 files changed

+119
-84
lines changed

7 files changed

+119
-84
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
### Run without DataLoader
66
```sh
7-
$ bin/run
7+
bin/run
88
```
99

10-
### Run with DataLoader
10+
### Run with DataLoader using React
1111
```sh
12-
$ bin/run --with-dataloader
12+
bin/run --with-dataloader
13+
```
14+
15+
### Run with DataLoader using deferred (native)
16+
```sh
17+
bin/run --with-dataloader --native
1318
```

app/common.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use GraphQL\Executor\Promise\PromiseAdapter;
34
use GraphQL\GraphQL;
45
use GraphQL\Type\Definition\NonNull;
56
use GraphQL\Type\Definition\ObjectType;
@@ -86,8 +87,10 @@ function createSchema(callable $friendsResolver, callable $characterResolver)
8687
return new Schema(['query' => $queryType]);
8788
}
8889

89-
function executeQueries(Schema $schema, &$calls, &$callsIds, callable $beforeExecute = null, callable $afterExecute = null)
90+
function executeQueries(Schema $schema, &$calls, &$callsIds, PromiseAdapter $promiseAdapter = null, callable $beforeExecute = null, callable $afterExecute = null)
9091
{
92+
GraphQL::setPromiseAdapter($promiseAdapter);
93+
9194
foreach (getQueries() as $query) {
9295
$calls = 0;
9396
$callsIds = [];
@@ -106,14 +109,18 @@ function getQueries()
106109
$queries[] = <<<QUERY
107110
{
108111
character1: character(id: "1000") {
112+
id
109113
name
110114
friends {
115+
id
111116
name
112117
}
113118
}
114119
character2: character(id: "1002") {
120+
id
115121
name
116122
friends {
123+
id
117124
name
118125
}
119126
}
@@ -123,9 +130,11 @@ function getQueries()
123130
$queries[] = <<<QUERY
124131
{
125132
character1: character(id: "1000") {
133+
id
126134
name
127135
}
128136
character2: character(id: "1002") {
137+
id
129138
name
130139
}
131140
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use GraphQL\Tests\StarWarsData;
4+
use Overblog\DataLoader\DataLoader;
5+
use Overblog\DataLoader\Promise\Adapter\Webonyx\GraphQL\SyncPromiseAdapter;
6+
use Overblog\PromiseAdapter\Adapter\WebonyxGraphQLSyncPromiseAdapter;
7+
8+
require __DIR__.'/../vendor/autoload.php';
9+
10+
$calls = 0;
11+
$callsIds = [];
12+
13+
$graphQLSyncPromiseAdapter = new SyncPromiseAdapter();
14+
$promiseAdapter = new WebonyxGraphQLSyncPromiseAdapter($graphQLSyncPromiseAdapter);
15+
$batchLoadFn = function ($ids) use (&$calls, &$callsIds, $promiseAdapter) {
16+
$callsIds[] = $ids;
17+
++$calls;
18+
$allCharacters = StarWarsData::humans() + StarWarsData::droids();
19+
$characters = array_intersect_key($allCharacters, array_flip($ids));
20+
21+
return $promiseAdapter->createAll(array_values($characters));
22+
};
23+
$dataLoader = new DataLoader($batchLoadFn, $promiseAdapter);
24+
25+
$schema = createSchema(
26+
function ($character) use ($dataLoader) {
27+
$promise = $dataLoader->loadMany($character['friends']);
28+
return $promise;
29+
},
30+
function ($root, $args) use ($dataLoader) {
31+
$promise = $dataLoader->load($args['id']);
32+
return $promise;
33+
}
34+
);
35+
36+
echo "With DataLoader (Using native promise):\n\n";
37+
38+
executeQueries(
39+
$schema,
40+
$calls,
41+
$callsIds,
42+
$graphQLSyncPromiseAdapter,
43+
function () use ($dataLoader) {
44+
$dataLoader->clearAll();
45+
}
46+
);

app/with-dataloader.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
<?php
22

33
use GraphQL\GraphQL;
4-
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter;
54
use GraphQL\Tests\StarWarsData;
6-
use McGWeb\PromiseFactory\Factory\ReactPromiseFactory;
75
use Overblog\DataLoader\DataLoader;
6+
use Overblog\PromiseAdapter\Adapter\ReactPromiseAdapter;
87
use React\Promise\Promise;
98

109
require __DIR__.'/../vendor/autoload.php';
1110

1211
$calls = 0;
1312
$callsIds = [];
14-
$promiseFactory = new ReactPromiseFactory();
15-
$batchLoadFn = function ($ids) use (&$calls, &$callsIds, $promiseFactory) {
13+
$promiseAdapter = new ReactPromiseAdapter();
14+
$batchLoadFn = function ($ids) use (&$calls, &$callsIds, $promiseAdapter) {
1615
$callsIds[] = $ids;
1716
++$calls;
1817
$allCharacters = StarWarsData::humans() + StarWarsData::droids();
1918
$characters = array_intersect_key($allCharacters, array_flip($ids));
2019

21-
return $promiseFactory->createAll(array_values($characters));
20+
return $promiseAdapter->createAll(array_values($characters));
2221
};
23-
$dataLoader = new DataLoader($batchLoadFn, $promiseFactory);
22+
$dataLoader = new DataLoader($batchLoadFn, $promiseAdapter);
2423

2524
$schema = createSchema(
2625
function ($character) use ($dataLoader) {
@@ -39,13 +38,12 @@ function ($root, $args) use ($dataLoader) {
3938
}
4039
);
4140

42-
echo "With DataLoader:\n\n";
43-
44-
GraphQL::setPromiseAdapter(new ReactPromiseAdapter());
41+
echo "With DataLoader (using reactPHP promise):\n\n";
4542
executeQueries(
4643
$schema,
4744
$calls,
4845
$callsIds,
46+
new \GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter(),
4947
function () use ($dataLoader) {
5048
$dataLoader->clearAll();
5149
},

bin/run

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
$useDataLoader = !empty($_SERVER['argv']) ? in_array('--with-dataloader', $_SERVER['argv']) : false;
55

66
if ($useDataLoader) {
7-
require __DIR__.'/../app/with-dataloader.php';
7+
$useNative = !empty($_SERVER['argv']) ? in_array('--native', $_SERVER['argv']) : false;
8+
if ($useNative) {
9+
require __DIR__.'/../app/with-dataloader-native-promise.php';
10+
} else {
11+
require __DIR__.'/../app/with-dataloader.php';
12+
}
813
} else {
914
require __DIR__.'/../app/without-dataloader.php';
1015
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "mcg-web/sandbox-dataloader-graphql-php",
33
"type": "project",
4+
"minimum-stability": "dev",
45
"require": {
56
"overblog/dataloader-php": "dev-master",
67
"react/promise": "^2.4",
7-
"webonyx/graphql-php": "dev-master"
8+
"webonyx/graphql-php": "^0.9.0"
89
},
910
"autoload": {
1011
"files": [

composer.lock

Lines changed: 40 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)