From 693c717fc750eb6096d52770c500fcf5a6a24c9a Mon Sep 17 00:00:00 2001 From: Abner Tudtud <114082473+enjinabner@users.noreply.github.com> Date: Thu, 17 Aug 2023 13:40:50 +0800 Subject: [PATCH] Check for null (#29) --- src/GraphQL/Types/BeamClaimType.php | 2 +- src/Models/Laravel/BeamClaim.php | 2 +- src/Models/Laravel/Traits/EagerLoadSelectFields.php | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/GraphQL/Types/BeamClaimType.php b/src/GraphQL/Types/BeamClaimType.php index 8a1b8c9..b8de5f0 100644 --- a/src/GraphQL/Types/BeamClaimType.php +++ b/src/GraphQL/Types/BeamClaimType.php @@ -73,7 +73,7 @@ public function fields(): array 'code' => [ 'type' => GraphQL::type('String!'), 'description' => __('enjin-platform-beam::type.beam_claim.field.code'), - 'resolve' => fn ($claim) => $claim->singleUseCode, + 'resolve' => fn ($claim) => $claim->code ? $claim->singleUseCode : '', ], 'identifierCode' => [ 'type' => GraphQL::type('String!'), diff --git a/src/Models/Laravel/BeamClaim.php b/src/Models/Laravel/BeamClaim.php index 6a309e0..150e0f2 100644 --- a/src/Models/Laravel/BeamClaim.php +++ b/src/Models/Laravel/BeamClaim.php @@ -151,7 +151,7 @@ public function scopeWithSingleUseCode(Builder $query, string $code): Builder public function singleUseCode(): Attribute { return Attribute::make( - get: fn () => encrypt(implode(':', [$this->code, $this->beam->code, $this->nonce])) + get: fn () => encrypt(implode(':', [$this->code, $this->beam?->code, $this->nonce])) ); } diff --git a/src/Models/Laravel/Traits/EagerLoadSelectFields.php b/src/Models/Laravel/Traits/EagerLoadSelectFields.php index 8b5a923..8c110ca 100644 --- a/src/Models/Laravel/Traits/EagerLoadSelectFields.php +++ b/src/Models/Laravel/Traits/EagerLoadSelectFields.php @@ -148,7 +148,10 @@ public static function loadClaims( ]; } - foreach (BeamClaimType::getRelationFields($fieldKeys) as $relation) { + foreach ([ + ...BeamClaimType::getRelationFields($fieldKeys), + ...(isset($fields['code']) ? ['beam'] : []), + ] as $relation) { $with = array_merge( $with, static::getRelationQuery(