Repository: nuwave/lighthouse (language: php)

Issue:
Compare raw attributes only when ensuring uniqueness of paginated related models
<!-- Link to related issues this PR resolves, e.g. "Resolves #236"-->
 
- [ ] Added or updated tests
- [ ] Documented user facing changes
- [x] Updated CHANGELOG.md

**Changes**

Right now, `toJson` is used which "is recursive, so all attributes and relations will be converted to JSON" (see https://laravel.com/docs/11.x/eloquent-serialization#serializing-to-json). This means that all accessors are executed as well which can affect the performance of an application. The new approach simply compares the raw attributes of the models which should be enough in almost all cases as far as I can see.

**Breaking changes**

There might be a tiny chance that something doesn't work as before because accessors are not compared anymore.



Discussion / hints:
@spawnia Hi, this breaks when Models have native Enum casted attributes. `$model->getOriginal()` casts the attributes. `json_encode($relatedModel->getOriginal())` will then fail because enums are not stringable. Can we just use `$model->getRawOriginal()`?
Hm, the best would actually be to pick `$original` from the model but it's protected.
That would be `$model->getRawOriginal()`
@skaesdorf great, can you raise a PR for that?

Fix patch:
diff --git a/src/Execution/ModelsLoader/PaginatedModelsLoader.php b/src/Execution/ModelsLoader/PaginatedModelsLoader.php
index 3bbeadb08..c53a26cc9 100644
--- a/src/Execution/ModelsLoader/PaginatedModelsLoader.php
+++ b/src/Execution/ModelsLoader/PaginatedModelsLoader.php
@@ -85,7 +85,7 @@ protected function loadRelatedModels(EloquentCollection $parents): EloquentColle
         return $relatedModels->unique(
             // Compare all attributes because there might not be a unique primary key
             // or there could be differing pivot attributes.
-            static fn (Model $relatedModel): string => json_encode($relatedModel->getOriginal()),
+            static fn (Model $relatedModel): string => json_encode($relatedModel->getRawOriginal()),
         );
     }
 


Test patch: