Open philipstarkey opened 2 weeks ago
This PR implements multi-level nested create/update functionality for resources and ensures proper model validation through full_clean()
. The implementation focuses on allowing nested resource creation at any depth while maintaining consistent code paths and proper validation.
sequenceDiagram
actor User
participant Client
participant Server
participant Database
User->>Client: Initiate create/update request
Client->>Server: Send GraphQL mutation
Server->>Database: Validate data with full_clean()
alt Validation passes
Server->>Database: Create/update nested resources
Database-->>Server: Return success
Server-->>Client: Return success response
else Validation fails
Server-->>Client: Return validation error
end
classDiagram
class MutationResolver {
+create(info, model, data, key_attr, full_clean, pre_save_hook, exclude_m2m)
+update(info, model, data, key_attr, full_clean, pre_save_hook, exclude_m2m)
+_create(info, manager, data, key_attr, full_clean, pre_save_hook, exclude_m2m)
+update_m2m(info, instance, field, value, key_attr)
}
class ProjectInputPartial {
+name
+milestones
}
class MilestoneInputPartial {
+name
+issues
}
class MilestoneIssueInputPartial {
+name
+tags
}
MutationResolver --> ProjectInputPartial
ProjectInputPartial --> MilestoneInputPartial
MilestoneInputPartial --> MilestoneIssueInputPartial
Change | Details | Files |
---|---|---|
Added support for multi-level nested resource creation and updates |
|
strawberry_django/mutations/resolvers.py |
Added model validation through full_clean() |
|
strawberry_django/mutations/resolvers.py |
Added comprehensive test coverage for nested mutations |
|
tests/test_input_mutations.py |
Extended schema to support nested mutations |
|
tests/projects/schema.py |
Issue | Objective | Addressed | Explanation |
---|---|---|---|
#398 | Perform validation check on nested fields when they are updated via the parent field | ✅ |
full_clean()
is called during nested updates, addressing validation.
Description
This implements the core functionality of #604, allowing multi-level create/update of nested resources. It also ensures that
model.full_clean()
is called before creating a new resource, which ensures that internal information about constraint names doesn't leak out to the client viaIntegrityError
messages (see #398).To solve this, we allow
update_m2m
to recursively callcreate()
, ensuring that the logic for creation of records uses a consistent code path regardless of what depth in the nested data it is created at.Notes
This is not everything from #604, nor does it fix all of #603. It does fix all of #398. It seemed like #604 was getting too large to review though, so I'm hoping this approach of breaking it up into smaller chunks will work better.
Part of the reason why this approach was a bit easier was that I didn't try to preserve the change from #362. This change didn't come with any tests, has probably been superseded by the
key_attr
functionality that was added after the fix was merged, and it seems risky to assume that the default behaviour should be to reuse an existing model instance just because some fields match (as pointed out by @bellini666 previously in this comment).From the changes in this PR, I see several other subsequent bugfixes/improvements that could be made to address the remaining issues from #603 and comments raised in the previous PR #604:
key_attr
to specify more than one column to perform the lookup on (comment in #604)key_attr
and full_clean/full_clean_options to be specified individually for each type(?) of nested resource specified.project.milestones.0.name
orproject.milestones.0.issues.2.tags.3.name
could be an option?. Validation errors probably shouldn't stop other nested resources from being created, so all validation errors can be caught and returned at the end. (#404)Types of Changes
Issues Fixed or Closed by This PR
Checklist
Summary by Sourcery
Implement support for multi-level nested create and update operations with model full_clean() to enhance data integrity and prevent constraint name leaks. Add tests to verify the new functionality.
New Features:
Bug Fixes:
Enhancements:
Tests: