strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
421 stars 123 forks source link

Fix nested fields update #604

Open EvSpirit opened 4 months ago

EvSpirit commented 4 months ago

Description

This PR addresses issues described in #603. Kindly ask you to review the code and share your ideas.

While working on these two major bugs I also faced another issue related to previously added Django get_or_create shortcut (see PR #362). The current implementation seems a bit inconsistent for me because get_or_create is used only in m2m relations, more than that, this shortcut relies not only on unique fields. So it definitely fixes the issue mentioned in #360 , but at the same time there may be an opposite situation when duplicated records won't be created. First I thought to identify all unique fields and constraints (e.g. using Django total_unique_constraints) to further understand if get should be used before creating an object. But it seems there are quite many caveats. So what I'm suggesting is to have the following convention: if an object input contains explicitly passed None / null pk value (not UNSET), it would indicate an intention to force create an object. If omitted, get_or_create approach will be applied (actually, it's a bit different under the hood, I refactored full_clean usage to be invoked only after get(), otherwise, full_clean may trigger unique constraint violation).

Personally I think that it should not be hardcoded somewhere in the lib and this approach looks only as a workaround for me, probably it's a good idea to further extend input types or probably integrate forms to precisely control fields cleaning and saving, that's for further discussion and may be a a good feature request (as while working on the issue I noticed that full_clean options usability may be limited in case of nested relations).

It the current idea looks good, I think it would be better to update docs respectively.

Other than proposed fixes for the issues mentioned above, there is some minor code refactoring and cleanup:

Please let me know if it is better to move out such changes to another PR.

Types of Changes

Issues Fixed or Closed by This PR

Checklist

Summary by Sourcery

This pull request fixes issues with nested fields update, refines the full_clean process, and enhances the handling of get_or_create for better consistency. It also includes new tests to cover these changes and removes redundant test code.

sourcery-ai[bot] commented 4 months ago

Reviewer's Guide by Sourcery

This pull request addresses issues related to nested fields updates and refines the handling of Django's get_or_create shortcut. The changes include bug fixes, enhancements, and code refactoring. The main approach involves using get before create to avoid unique constraint violations and introducing a convention to force object creation when a None primary key is explicitly passed. Additionally, the PR includes new tests, schema updates, and minor code cleanups.

File-Level Changes

Files Changes
tests/test_input_mutations.py
tests/projects/schema.py
tests/projects/test_schema.py
tests/projects/models.py
tests/mutations/test_mutations.py
tests/projects/schema_inherited.py
tests/types.py
Added new test cases, updated existing tests, and modified schema and model definitions to support new nested relations and input types.
strawberry_django/mutations/resolvers.py
strawberry_django/mutations/mutations.py
strawberry_django/mutations/types.py
strawberry_django/mutations/fields.py
Refactored mutation resolver functions to handle None and UNSET primary key values, introduced full_clean options, and improved nested object creation handling.

Tips - Trigger a new Sourcery review by commenting `@sourcery-ai review` on the pull request. - Continue your discussion with Sourcery by replying directly to review comments. - You can change your review settings at any time by accessing your [dashboard](https://app.sourcery.ai): - Enable or disable the Sourcery-generated pull request summary or reviewer's guide; - Change the review language; - You can always [contact us](mailto:support@sourcery.ai) if you have any questions or feedback.
axieum commented 2 months ago

I'd love to see this merged. Happy to help where I can. 😄

bellini666 commented 2 months ago

I'd love to see this merged. Happy to help where I can. 😄

I'm mostly waiting for the tests to pass and the adjustment to the conflict to review this again.

Not sure if @EvSpirit is going to tackle those, but if not, I do appreciate the help to finish this PR and merge it :)