nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.36k stars 438 forks source link

Compare raw attributes only when ensuring uniqueness of paginated related models #2540

Closed jaulz closed 5 months ago

jaulz commented 5 months ago

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.

skaesdorf commented 5 months ago

@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()?

jaulz commented 5 months ago

Hm, the best would actually be to pick $original from the model but it's protected.

skaesdorf commented 5 months ago

That would be $model->getRawOriginal()

jaulz commented 5 months ago

@skaesdorf great, can you raise a PR for that?

skaesdorf commented 5 months ago

@jaulz Done. See #2550