strawberry-django icon indicating copy to clipboard operation
strawberry-django copied to clipboard

OneToManyInput saves and runs validation on foreign key

Open keithhackbarth opened this issue 1 year ago • 3 comments

Example type:

@strawberry_django.input(Fruit)
class FruitInputPartial(PermissionMixin):
    id: auto
    location: auto

Example mutation

            mutation PatchFruit {
                patchFruit(data: {id: 1, location: {set: 1}}) {
                    id
                    location { id }
                }
            }

Expected

Only the fruit model receives clean and save. 1 database save operations

Actual

Both the model and relation receive clean and save. 2 database save operations

Additional Context

The reason this is painful is because of performance concerns. But also if paired with a library such as django-simple-history it creates a bunch of empty saves in foreign relationships.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

keithhackbarth avatar Feb 22 '24 00:02 keithhackbarth

I agree with you!

We do support nested updating, but for sure we don't want to call save on those nested relation when they did not get changed.

bellini666 avatar Feb 22 '24 16:02 bellini666

@bellini666 - I can volunteer some time to write some tests and PR if you think is worthy of a fix. Do you know of another way to do it in the meantime?

I also noticed that this case seems to be broken:

@strawberry_django.input(Fruit)
class FruitInputPartial(PermissionMixin):
    id: auto
    location_id: ID

            mutation PatchFruit {
                patchFruit(data: {id: 1, locationID: 1}) {
                    id
                    location { id }
                }
            }

Expected: Updates location or throws an error Actual: Does nothing

keithhackbarth avatar Feb 23 '24 00:02 keithhackbarth

@bellini666 - I can volunteer some time to write some tests and PR if you think is worthy of a fix. Do you know of another way to do it in the meantime?

It surely is. And contributions are always very welcomed! 😊

Feel free to ping me either here or at our discord channel if you need help with anything!

bellini666 avatar Feb 23 '24 16:02 bellini666

Fixed by https://github.com/strawberry-graphql/strawberry-django/pull/490

bellini666 avatar Jun 10 '24 18:06 bellini666