strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
MIT License
391 stars 115 forks source link

OneToManyInput saves and runs validation on foreign key #487

Closed keithhackbarth closed 2 weeks ago

keithhackbarth commented 4 months ago

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

Fund with Polar

bellini666 commented 4 months ago

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.

keithhackbarth commented 4 months ago

@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

bellini666 commented 4 months ago

@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 commented 2 weeks ago

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