You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the latest strawberry-django version (0.47.1) single-level nested objects CUD operations work fine, however, multiple level nested objects cannot be created/updated. Consider the following types hierarchy:
class Project(models.Model):
foo = models.CharField(max_length=255)
class Milestone(models.Model):
bar = models.CharField(max_length=255)
project = models.ForeignKey(Project, on_delete=models.PROTECT)
class Issue(models.Model):
baz = models.CharField(max_length=255)
milestone = models.ForeignKey(Milestone, on_delete=models.PROTECT)
Case 1. Issue - Milestone - Project
When trying to execute the following mutation:
mutation CreateIssue ($input: IssueInput!) {
createIssue (input: $input) {
... on IssueType {
id
milestone {
id
project {
id
name
}
}
}
}
}
parent model (Milestone but not Project) is used, so even if full clean passes somehow (in unit tests that's the case as all models have name field), finally incorrect instance would be created. The fix seems to be quite straightforward also:
related_model = get_model_fields(model).get(k).related_model
v = create(info, related_model, v.data or {})
Using parent model we can extract related model and pass one to create function.
Case 2. Project - Milestone - Issue (reverse relations)
mutation UpdateProject ($input: ProjectInputPartial!) {
updateProject (input: $input) {
... on ProjectType {
id
milestones {
id
issues {
id
}
}
}
}
}
In the latest strawberry-django version (0.47.1) single-level nested objects CUD operations work fine, however, multiple level nested objects cannot be created/updated. Consider the following types hierarchy:
Case 1. Issue - Milestone - Project
When trying to execute the following mutation:
input:
Error occurs:
That happens because of two issues:
_parse_data
function check forNone
pk value but notUNSET
:strawberry-django/strawberry_django/mutations/resolvers.py
Lines 102 to 107 in 9025ee5
So
UNSET
added to parsed data. After fixing if-clause and checking for bothNone
andUNSET
values another issue arises:model
used to create dummy instance and full-clean oneThe problem is in the same function:
strawberry-django/strawberry_django/mutations/resolvers.py
Lines 102 to 103 in 9025ee5
parent model (
Milestone
but notProject
) is used, so even if full clean passes somehow (in unit tests that's the case as all models havename
field), finally incorrect instance would be created. The fix seems to be quite straightforward also:Using parent model we can extract related model and pass one to
create
function.Case 2. Project - Milestone - Issue (reverse relations)
Input:
Error:
That happens because nested issues are passed directly to Django
create
(orget_or_create
):strawberry-django/strawberry_django/mutations/resolvers.py
Lines 623 to 627 in 9025ee5
This issue seems to be related to #383 and #449 PR. #360 is also related as
update_m2m
function is to be updated.I'm working on the PR to address the issues mentioned above, creating this issue to further refer one from the PR.
Upvote & Fund
The text was updated successfully, but these errors were encountered: