pythondjangographqlgraphene-pythongraphene-django

Graphene django. Get error: String cannot represent value '(value, )' after mutation attempt


I'm new to graphene library and make updating mutation as was in documentation. Also I looked at this video about CRUD : https://youtu.be/C-EfYVXShLE?si=uksgjJEuavU1k9GW I thought, that problem was in mutate arguments, I tried to make it with kwargs, but result was the same. Unfortunatly, most guides was using django-rest to write crud, but I would like to make it without others dependecies.

Here's the code:

class UpdateAddress(graphene.Mutation):
    address = graphene.Field(AddressType)
    
    class Arguments:
        address_id = graphene.ID(required=True)
        index = graphene.String()
        street = graphene.String()
        house = graphene.String()
        flat = graphene.String()
        locality = graphene.Int()
        
    @classmethod
    def mutate(cls, root, info, address_id, index=None, street=None, house=None, flat=None, locality=None):
        address = Address.objects.get(pk=address_id)
        try:
            entry = Locality.objects.get(pk=locality)
        except Locality.DoesNotExist:
            entry = None

        address.index=index,
        address.street=street,
        address.house=house,
        address.flat=flat,
        address.locality=entry

        address.save()
        return UpdateAddress(address=address)

When i try to update some field with this query:

mutation {
  updateAddress(
    addressId: "1",
    index: "41204"
    street: "Shevchenko",
    house: "1",
    flat: "15",
    locality: 1
  ) {
    address{
      id,
      street,
      index,
      house,
      flat,
      locality{
        id
      }
    }
  }
}

I recieve errors:

  "errors": [
    {
      "message": "String cannot represent value: ('41204',)",
      "locations": [
        {
          "line": 12,
          "column": 7
        }
      ],
      "path": [
        "updateAddress",
        "address",
        "index"
      ]
    }
  ],

Somehow it sets all string values to a tuple and I get field like this:

"id": "1",
"index": "('41204',)",
"street": "('Shevchenko',)",
"house": "('1',)",
"flat": "('1',)"

Honestly, I don't understand what I do wrong :(


Solution

  • Remove the trailing commas. You wrap the values in a singleton tuple:

    address.index = index  # 🖘 no comma
    address.street = street  # 🖘 no comma
    address.house = house  # 🖘 no comma
    address.flat = flat  # 🖘 no comma
    address.locality = entry  # 🖘 no comma