Skip to content

Commit

Permalink
[PLA-1943] Fix relationship total count (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Oct 16, 2024
1 parent ab04024 commit 398f7d6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/Feature/GraphQL/Queries/GetCollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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, []);
Expand Down

0 comments on commit 398f7d6

Please sign in to comment.