I am trying to have a GraphQL mutation create the inputs for two Django models at once using strawberry. I checked the documentation here and there weren't any examples of how to do this.
I have the following Django model:
class Address(models.Model):
name = models.CharField()
class Person(models.Model):
name = models.CharField()
address = models.ForeignKey('Address', on_delete=models.CASCADE, blank=False, null=False)
With the type.py
file:
@strawberry.django.type(models.Address)
class Address:
id: auto
name:auto
@strawberry.django.input(models.Address)
class AddressInput:
id: auto
name:auto
@strawberry.django.type(models.Person)
class Person:
id: auto
name: auto
address:'Address'
@strawberry.django.input(models.Person)
class Person:
id: auto
name: auto
address:'AddressInput'
For the schema.py
I have:
@strawberry.type
class Mutation:
createAddress: Address = mutations.create(AddressInput)
createPerson: Person =mutations.create(PersonInput)
schema = strawberry.Schema(mutation=Mutation)
I tried the Mutation, but I got an error:
mutation newPerson ($name: String!, $addressname:String!){
createPerson(data: {name: $name, address: {name: $addressname}}) {
id
name
address {
id
name
}
}
}
#Query Variables
{
"name": "Min",
"addressname": "jkkihh",
}
Error message:
"message": "Field 'id' expected a number but got PersonInput(id=<strawberry.unset._Unset object at 0x00000194FB945C90>, addressname='jkkihh', description=<strawberry.unset._Unset object at 0x00000194FB945C90>, notes=<strawberry.unset._Unset object at 0x00000194FB945C90>)."
This is similar the this question I previously asked using Graphene. Where it was resolved by making an new object type to store and wrote a mutate function inside a class for mutation. I also tried doing two mutations, but I had issues getting the id for the foreign key address when it was created.