diff --git a/src/GraphQL/Schemas/Primary/Substrate/Queries/GetCollectionsQuery.php b/src/GraphQL/Schemas/Primary/Substrate/Queries/GetCollectionsQuery.php index d1846d86..0b6ed286 100644 --- a/src/GraphQL/Schemas/Primary/Substrate/Queries/GetCollectionsQuery.php +++ b/src/GraphQL/Schemas/Primary/Substrate/Queries/GetCollectionsQuery.php @@ -8,12 +8,15 @@ use Enjin\Platform\GraphQL\Types\Pagination\ConnectionInput; use Enjin\Platform\Interfaces\PlatformGraphQlQuery; use Enjin\Platform\Models\Collection; +use Enjin\Platform\Models\CollectionAccount; +use Enjin\Platform\Models\Token; use Enjin\Platform\Rules\MaxBigInt; use Enjin\Platform\Rules\MinBigInt; use Enjin\Platform\Support\Hex; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; use Rebing\GraphQL\Support\Facades\GraphQL; use Rebing\GraphQL\Support\Query; @@ -68,10 +71,35 @@ public function resolve( ResolveInfo $resolveInfo, Closure $getSelectFields, ): mixed { - return Collection::loadSelectFields($resolveInfo, $this->name) + $result = Collection::loadSelectFields($resolveInfo, $this->name) ->addSelect(DB::raw('cast(collection_chain_id as unsigned integer) as collection_id')) ->when(!empty($args['collectionIds']), fn (Builder $query) => $query->whereIn('collection_chain_id', $args['collectionIds'])) ->cursorPaginateWithTotalDesc('collection_id', $args['first']); + + $items = $result['items']->items(); + if (count(Arr::get($items, '0.tokens', []))) { + $totalCounts = Token::whereIn('collection_id', Arr::pluck($items, 'id')) + ->select('collection_id', DB::raw('count(*) as total')) + ->groupBy('collection_id') + ->pluck('total', 'collection_id'); + + foreach ($items as $item) { + $item->tokens_count = $totalCounts->get($item->id, 0); + } + } + + if (count(Arr::get($items, '0.accounts', []))) { + $totalCounts = CollectionAccount::whereIn('collection_id', Arr::pluck($items, 'id')) + ->select('collection_id', DB::raw('count(*) as total')) + ->groupBy('collection_id') + ->pluck('total', 'collection_id'); + + foreach ($items as $item) { + $item->accounts_count = $totalCounts->get($item->id, 0); + } + } + + return $result; } /** diff --git a/tests/Feature/GraphQL/Queries/GetCollectionsTest.php b/tests/Feature/GraphQL/Queries/GetCollectionsTest.php index 6520c314..f8e1ec05 100644 --- a/tests/Feature/GraphQL/Queries/GetCollectionsTest.php +++ b/tests/Feature/GraphQL/Queries/GetCollectionsTest.php @@ -13,7 +13,9 @@ use Enjin\Platform\Support\Hex; use Enjin\Platform\Tests\Feature\GraphQL\TestCaseGraphQL; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Arr; use Illuminate\Support\Collection as CollectionSupport; +use Illuminate\Support\Facades\Cache; class GetCollectionsTest extends TestCaseGraphQL { @@ -45,6 +47,31 @@ public function test_it_can_fetch_with_no_args(): void $this->assertTrue(count($response['edges']) > 0); } + public function test_it_can_get_total_count_correctly(): void + { + $collection = Collection::factory()->create(); + Token::factory(random_int(16, 100))->create(['collection_id' => $collection->id]); + CollectionAccount::factory(random_int(16, 100))->create(['collection_id' => $collection->id]); + + Cache::flush(); + $response = $this->graphql($this->method, [ + 'collectionIds' => [$collection->collection_chain_id], + 'tokensLimit' => 1, + 'accountsLimit' => 1, + ]); + $this->assertTrue(count($response['edges']) > 0); + $this->assertCount(1, Arr::get($response, 'edges.0.node.tokens.edges')); + $this->assertCount(1, Arr::get($response, 'edges.0.node.accounts.edges')); + $this->assertEquals( + Token::where('collection_id', $collection->id)->count(), + Arr::get($response, 'edges.0.node.tokens.totalCount') + ); + $this->assertEquals( + CollectionAccount::where('collection_id', $collection->id)->count(), + Arr::get($response, 'edges.0.node.accounts.totalCount') + ); + } + public function test_it_can_fetch_with_empty_args(): void { $response = $this->graphql($this->method, []);