2

I'm tryting to create the Update mutation of the model that has a foreign key relation. I have done everything as per the documentation but it still doesn't work when I provide the foreign Model Input.

I created the Input with the proper attributes that I should pass in the query but it doesn't work and throws the below shown error. Field 'id' expected a number but got {'id': 2}.

I am unable to understand the reason behind this error. I'm passing the correct input (I believe) could someone, please, help me understand why is this happening?

Your suggestions and inputs are much appreciated.

The inputs:

class FuelTypeInput(graphene.InputObjectType):
  # id of the FuelTypeModel
  id = graphene.Int()
  label = graphene.String()

class FuelSubtypeInput(graphene.InputObjectType):
  # Graphene ID
  id = graphene.ID()
  label = graphene.String()
  fuel_type = graphene.Field(FuelTypeInput)

The Update mutation:

class UpdateFuelSubType(relay.ClientIDMutation):
  class Input:
    id = Int() # id of the Fuel SubTypeModel
    input = FuelSubtypeInput(required=True)

  ok = True
  fuel_subtype = Field(FuelSubTypeNode)

  def mutate_and_get_payload(root, info, id, input):

    ok = False

    if FuelSubType.objects.filter(pk=id).update(**input):
      fuel_subtype = FuelSubType.objects.get(pk=id)
      ok = True

      return UpdateFuelSubType(fuel_subtype=fuel_subtype)

    return UpdateFuelSubType(fuel_subtype=None)

The mutation query at the Client:

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label", 
                    fuelType: { id: 2 }
                }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}

Final result:

{
  "errors": [
    {
      "message": "Field 'id' expected a number but got {'id': 2}.",
      "locations": [
        {
          "line": 48,
          "column": 5
        }
      ],
      "path": [
        "updateFuelSubtype"
      ]
    }
  ],
  "data": {
    "updateFuelSubtype": null
  }
}

I would also like to mention that when I remove the fuelType input from the query, everything works fine, e.g:

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label" }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}
User9102d82
  • 1,172
  • 9
  • 19
Velidan
  • 5,526
  • 10
  • 48
  • 86

1 Answers1

0

Simply sending the primary key of your foreign key in the mutation query will work. Instead of

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label", 
                    fuelType: { id: 2 }
                }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}

send

mutation MyMutations {
    updateFuelSubtype(
        input: { 
            id: 2, 
            input: { label: "Updated 11 Mutation Label", 
                    fuelType: 2 # ## <--- change
                }
                }
    ) {
        fuelSubtype {
            label
        }
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ruhi123
  • 99
  • 6