benawad / benawad/php-graphql-example
Fix DataLoader on field resolver
- Dominant language
- PHP
- Stars
- 18
- Forks
- 10
- PR merge metrics
- No merged PRs in 30d
Description
Hi!
First off; really big thanks! Your Youtube videos about GraphQL and this repo really inspired me. We've implemented GraphQL at my work using PHP and I had a task of solving the `n+1` problem. Webonyx repo doesn't really help but this did the trick.
I did a similar implementation using `Slim Framework` and I put on SQL logging. I noticed that this line does not work as intended
https://github.com/benawad/php-graphql-example/blob/dc39fa59923fddfbddbf656b018932a88539cfe2/resolvers.php#L8
The `await()` seems to solve a promised PER `$root['id']` meaning it visits the database fetching a result each time `load()` receives a new value (that is not cached). I did a demo with books and authors and noticed my SQL log looked like this:
```
{
"1": {
"sql": "SELECT * FROM book",
"params": [],
"types": [],
"executionMS": 0.0005571842193603516
},
"2": {
"sql": "SELECT id, `name` FROM author WHERE id in (?)",
"params": [
"1"
],
"types": [],
"executionMS": 0.0009350776672363281
},
"3": {
"sql": "SELECT id, `name` FROM author WHERE id in (?)",
"params": [
"2"
],
"types": [],
"executionMS": 0.0002930164337158203
},
"4": {
"sql": "SELECT id, `name` FROM author WHERE id in (?)",
"params": [
"3"
],
"types": [],
"executionMS": 0.00022983551025390625
}
}
```
What solved it was removing the await() as apparently `GraphQL-PHP` already handles promises:
```
return $context['loaders']['author']->load($book['author_id']);
```
Now my SQL log looks like this:
```
{
"1": {
"sql": "SELECT * FROM book",
"params": [],
"types": [],
"executionMS": 0.00038886070251464844
},
"2": {
"sql": "SELECT id, `name` FROM author WHERE id in (?)",
"params": [
[
"1",
"2",
"3"
]
],
"types": [
101
],
"executionMS": 0.0014162063598632812
}
}
```
You should test that out and fix the line!
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.