Graphene-django: DjangoModelFormMutation getting "an invalid keyword argument for Mutation" error instead of model form errors

Created on 4 Apr 2020  路  5Comments  路  Source: graphql-python/graphene-django

When using DjangoModelFormMutation and getting a modelform error the mutation shows an invalid keyword argument for Mutation as shown below. I am supposed to get errors for the field name showing "This name has already been registered." However I am getting graphql.error.located_error.GraphQLLocatedError: 'name' is an invalid keyword argument for CategoryMutation

# model
class Category(models.Model):
    name = models.CharField(max_length=10, unique=True,
                 error_messages={'unique':"This name has already been registered."})

    def __str__(self):
        return self.name

#model form
class CategoryForm(ModelForm):
    class Meta:
        model = Category
        exclude = []

# create category mutation
class CategoryMutation(DjangoModelFormMutation):
    category = graphene.Field(CategoryType)
    class Meta:
        form_class = CategoryForm

mutation:

mutation {
  categoryCreate(input: {name: "spices"}) {
    errors {
      field,
      messages
    }
  }
}

results when the name is not unique:

{
  "errors": [
    {
      "message": "'name' is an invalid keyword argument for CategoryMutation",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "categoryCreate"
      ]
    }
  ],
  "data": {
    "categoryCreate": null
  }
}

Most helpful comment

All 5 comments

There should be two things that should be changed IMO

  1. An assertion that return_field_name is set in Meta options to match a field name in mutation class
    https://github.com/graphql-python/graphene-django/blob/b84f61afab5922f7a330b2f09bdf540b40b44e86/graphene_django/forms/mutation.py#L165-L169
  2. form.data should be handled differently or should be removed altogether and the length of errors should be used to determine success/failure
    https://github.com/graphql-python/graphene-django/blob/b84f61afab5922f7a330b2f09bdf540b40b44e86/graphene_django/forms/mutation.py#L45-L50

@ajatprabha you are right removing the **form.data fixed the problem. But I wonder why it is passed as kwargs? is it intended to return the invalid data back to the client ? I don't see the point of it.

Yes, it's for returning back the invalid data, however, it's not dynamic enough to work properly right now.

This issue should be fixed in #915 Sorry for the inconvenience

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BrianChapman picture BrianChapman  路  3Comments

x9sheikh picture x9sheikh  路  4Comments

nickhudkins picture nickhudkins  路  3Comments

licx picture licx  路  3Comments

amiyatulu picture amiyatulu  路  3Comments