1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ccsv
  • 8,188
  • 12
  • 53
  • 97

1 Answers1

1

I think strawberry_django has a bug here, though you should prefer using the strawberry_django_plus package, because it is better maintained. Hopefully, it will be merged to the official strawberry soon.

In your case, it would be:

from strawberry_django_plus import gql
from strawberry import auto

@gql.django.type(models.Person)
class Person:
    id: auto
    name: auto
    address:'Address'

@gql.django.input(models.Person)
class Person:
    id: auto
    name: auto
    address:'AddressInput'

@gql.type
class Mutation:
    createAddress: Address = gql.django.create_mutation(AddressInput)
    createPerson: Person = gql.django.create_mutation(PersonInput)

schema = strawberry.Schema(mutation=Mutation)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ניר
  • 1,204
  • 1
  • 8
  • 28
  • 1
    Ok I added it as an bug on github hopefully they will fix it or merge the two libraries https://github.com/strawberry-graphql/strawberry-graphql-django/issues/133 – ccsv Jun 29 '22 at 17:43