Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLA-1608] Token metadata fallback, and {id} replacement. #138

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading