Skip to content

Commit

Permalink
[PLA-1608] Token metadata fallback, and {id} replacement. (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
v16Studios authored Mar 8, 2024
1 parent 0a1db99 commit d1a6a3e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/GraphQL/Types/Substrate/AttributeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Enjin\Platform\GraphQL\Types\Traits\InSubstrateSchema;
use Enjin\Platform\Interfaces\PlatformGraphQlType;
use Enjin\Platform\Models\Attribute;
use Illuminate\Support\Str;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Type;

Expand Down Expand Up @@ -40,7 +41,7 @@ public function fields(): array
'description' => __('enjin-platform::mutation.batch_set_attribute.args.value'),
'resolve' => function ($attribute) {
if (strtolower($attribute->key) == 'uri' && strpos($attribute->value, '{id}') !== false && $attribute->token_id) {
return str_replace('{id}', $attribute->token->token_chain_id, $attribute->value);
return Str::replace('{id}', "{$attribute->token->collection->collection_chain_id}-{$attribute->token->token_chain_id}", $attribute->value);
}

return $attribute->value;
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Laravel/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Block extends BaseModel
/**
* Get the prunable model query.
*
* @return \Illuminate\Database\Eloquent\Builder
* @return Builder
*/
public function prunable(): Builder
{
Expand Down
24 changes: 23 additions & 1 deletion src/Models/Laravel/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Facades\Enjin\Platform\Services\Database\MetadataService;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use Staudenmeir\EloquentEagerLimit\HasEagerLimit;

class Token extends BaseModel
Expand Down Expand Up @@ -140,7 +141,21 @@ protected function fetchMetadata(): Attribute
protected function metadata(): Attribute
{
return new Attribute(
get: fn () => $this->attributes['metadata'] ?? MetadataService::fetch($this->getRelation('attributes')->first()),
get: function () {
$tokenUriAttribute = $this->fetchUriAttribute($this);
$fetchedMetadata = $this->attributes['metadata'] ?? MetadataService::fetch($tokenUriAttribute);

if (!$fetchedMetadata) {
$collectionUriAttribute = $this->fetchUriAttribute($this->collection);
if ($collectionUriAttribute && Str::contains($collectionUriAttribute->value, '{id}')) {
$collectionUriAttribute->value = Str::replace('{id}', "{$this->collection->collection_chain_id}-{$this->token_chain_id}", $collectionUriAttribute->value);
}

$fetchedMetadata = MetadataService::fetch($collectionUriAttribute);
}

return $fetchedMetadata;
},
);
}

Expand All @@ -162,4 +177,11 @@ protected function pivotIdentifier(): Attribute
get: fn () => "{$collection->collection_chain_id}:{$this->token_chain_id}",
);
}

private function fetchUriAttribute($model)
{
return $model->load('attributes')->getRelation('attributes')
->filter(fn ($attribute) => 'uri' == $attribute->key)
->first();
}
}
2 changes: 1 addition & 1 deletion tests/Feature/GraphQL/Queries/GetTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function test_it_can_replace_id_metadata(): void
$this->assertArraySubset([
'attributes' => [[
'key' => 'uri',
'value' => 'https://example.com/' . $this->token->token_chain_id,
'value' => "https://example.com/{$this->collection->collection_chain_id}-{$this->token->token_chain_id}",
],
],
], $response);
Expand Down

0 comments on commit d1a6a3e

Please sign in to comment.