vasily-kartashov / graphql-batch-processor
GraphQL batch processor
Installs: 12 894
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7 || ^8
- webonyx/graphql-php: <=0.13.8
Requires (Dev)
- php-parallel-lint/php-parallel-lint: @stable
- phpunit/phpunit: ^6 || ^7 || ^8
- squizlabs/php_codesniffer: @stable
- symfony/polyfill-mbstring: <=1.20.0
- vimeo/psalm: @stable
This package is auto-updated.
Last update: 2024-11-20 11:15:26 UTC
README
Simple Example:
// Name of the cache is `addressesByUserId` return Batch::as('addressesByUserId') // Collect user IDs ->collectOne($user->id()) // When all user IDs are collected, fetch addresses for all collected user IDs // The callback is only executed once for each set of user IDs // And cached internally under the name `addressesByUserId` ->fetchOneToMany(function (array $userIds) { return $this->addressRepository->findAddressesByUserIds($userIds); });
More complex example
return Batch::as('accountsByOrgranizationId') ->collectMultiple($organization->accountIds()) ->fetchOneToOne(function (array $accountIds) { return $this->accountRepository->findAccountsByAccountIds($accountIds); });
Proper example
Get all addresses for each user; post filter out hidden addresses; format each address as a string; if there's no address, default to company's address
return Batch::as('addressesByUserId') ->collectOne($user->id()) ->filter(function (Address $address) { return !$address->hidden(); }) ->format(function (Address $address) { return (string) $address; }) ->defaultTo([$company->defaultAddress()]) ->fetchOneToMany(function (array $userIds) { return $this->addressRepository->findAddressesByUserIds($userIds); });
Tracing
Batches accept PSR-3 Loggers
return Batch::as('usersByUserIds') ->setLogger($logger) ->collectOne(...) ...