vasily-kartashov / graphql-batch-processor
GraphQL 批处理器
0.0.10
2021-01-10 05:00 UTC
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-09-20 10:52:59 UTC
README
简单示例
// 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); });
更复杂示例
return Batch::as('accountsByOrgranizationId') ->collectMultiple($organization->accountIds()) ->fetchOneToOne(function (array $accountIds) { return $this->accountRepository->findAccountsByAccountIds($accountIds); });
正确示例
获取每个用户的全部地址;过滤隐藏地址;将每个地址格式化为字符串;如果没有地址,则默认使用公司地址
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); });
追踪
批处理接受 PSR-3 日志记录器
return Batch::as('usersByUserIds') ->setLogger($logger) ->collectOne(...) ...