Closed Lucasmiguelmac closed 3 months ago
Update: same error raised when trying to implement the following mutation under my Mutation
class:
deleteFruit: Fruit = mutations.delete()
Got the same issue, in a pretty plain vanilla setup, InputType with one string field.
Django 3.2 / Python 3.8
Same issue for me, using Django 4.0.2 and Python 3.10.2. It works, when you use a List type:
update_fruit: List[FruitType] = mutations.update(FruitPartialInput)
But for mutations.delete() does not work.
Both update
and delete
mutations require List
type. See example project.
https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/examples/django/app/schema.py#L39-L40
Following should work. The reason for that is that both of these mutations may update or delete multiple items and therefore multiple items may be returned.
@strawberry.type
class Mutation:
createColor: Color = mutations.create(ColorInput)
createFruit: Fruit = mutations.create(FruitInput)
updateFruits: List[Fruit] = mutations.update(FruitPartialInput)
deleteFruits: List[Fruit] = mutations.delete()
We definitely have to improve error reporting.
@la4de thanks for the feedback. I am using the django-strawberry-plus package for crud mutations now, but it feels strange to have only the possibility to do 'update many' mutations. What is the selection predicate for those mutations?
@gersmann, that's very good question. You need to add filters for that purpose. See filtering chapter from our docs https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/docs/references/mutations.md
After that you can update data for filtered set.
{
updateFruits(data: { name: "orange" }, filters: { id: { inList: [1, 2] } } ) {
id
name
}
}
Be careful, the filter is applied to both the query to update and the query for return results.
So that:
{
updateFruits(data: { name: "orange" }, filters: { name: { exact: "Orange" } } ) {
id
name
}
}
Will promptly rename the "Orange" fruit(s) to "orange" fruit(s) but will return you 0 result.
This is because the filter "name is exactly Orange" will match no model instance once the migration has been applied.
This is probably a bug, but I don’t see how to fix this right now.
The mutations update has been tweaked a lot since this issue was reported, and I'm almost sure this issue has been fixed already.
Going to mark it as resolved, but please comment in here if that is not the case so that we can reopen it :)
Hello there!
I was basically trying this library for the first time, following [this example] incrementally with some minor tweaks like a
TextChoices
field for thename
field in theColor
model.I could succesfully implement the following mutations:
createColor
createFruit
But when I try to create the
updateFruit
, my Djangomanage.py runserver
will raise the following error in the logs when adding theupdateFruit
under theMutation
type:As you can imagine I can also trigger this traceback by running
python manage.py check
I share a more detailed traceback at the end of this comment.My models.py looks like this:
My
types.py
looks like this:My
schema.py
looks like this:Though probably irrelevant, my
urls.py
looks like this:My
settings.py
looks like this:Detailed traceback:
Upvote & Fund