Trait to avoid N+1 problem in GraphQL when using the webonyx/graphql-php or any wrapper. It is based in the Deferred resolvers that provide the library and allow you to work with them in an easy way.
The trait split the resolver in two parts, fetch and pluck.
This part receive all the root objects in one array, so you can process all of them in one shoot and return all the data in the best way to be processed in the pluck.
The method receives all the data processed and the root that is requesting the info, so it should search inside the data processed and return the specific data.
You must implement the resolver using the DeferredResolverInterface
and use the trait in the resolver type like this:
'resolve' => $this->deferredResolve(new MyResolver());
You can see an article about the package at https://medium.com/@JoseCardona/solving-graphql-n-1-in-php-92ed9161dd7b
joskfg/graphql-bulk-resolver
has a PHPUnit test suite and a coding style compliance test suite using PHP CS Fixer.
To run the tests, run the following command from the project folder.
$ docker-compose run test
To run interactively using PsySH:
$ docker-compose run psysh
The MIT license. Please see LICENSE for more information.