Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mutation.py #811

Merged
merged 3 commits into from
Feb 23, 2020
Merged

Update mutation.py #811

merged 3 commits into from
Feb 23, 2020

Conversation

protasovse
Copy link
Contributor

@protasovse protasovse commented Nov 18, 2019

I do not add data to the error class, but add data to payload.
Without sending form data, this request does not work correctly if there are errors in the form.

mutation SignIn {
  signin(input: {name: "Sergey Protasov", email: "protasovse@gmail.com"}) {
    name
    email
    errors {
      field
      messages
    }
  }
}

Here is the answer:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field SignIn2Payload.name.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "signin",
        "name"
      ]
    }
  ],
  "data": {
    "signin": null
  }
}

...and we cannot see form errors from the response of this request...
From the Documentation Explorer we see that the request should look like this:

email: String!
name: String!
errors: [ErrorType]
clientMutationId: String

And this is not at all achievable without overriding the method:

@classmethod
def perform_mutate(cls, form, info):
    form.save()
    return cls(errors=[])

@zbyte64
Copy link
Collaborator

zbyte64 commented Nov 18, 2019

Please add a description, I'll try to help you get the ball rolling:

This appears to add the following behavior to the Django forms mutation: pass the form data to the error class.

What we need to move forward is a unit test demonstrating the use case. We want to make sure the error class can handle the extra data and we would want verify the resulting graphql payload (I am not sure how that extra form data appears to the graphql client for instance).

We also need to state the rationale, which I think is to simplify client-side validation by including the data that failed validation in the response. But it might be good to expand on how exactly this will make your life easier.

@protasovse
Copy link
Contributor Author

Description added

@zbyte64
Copy link
Collaborator

zbyte64 commented Nov 21, 2019

I think I understand now. This seems valid to me, going forward I think we should copy the following test to reflect this desired behavior:

def test_model_form_mutation_mutate_invalid_form(self):

It would supply age but not name and verify age is sent back as part of the response.

@jkimbo jkimbo requested a review from zbyte64 December 27, 2019 16:04
@jkimbo
Copy link
Member

jkimbo commented Dec 27, 2019

Added tests @zbyte64

@jkimbo jkimbo self-assigned this Dec 27, 2019
@jkimbo jkimbo merged commit 2350963 into graphql-python:master Feb 23, 2020
@jarcoal
Copy link

jarcoal commented Mar 27, 2020

Am I crazy or does this PR completely break DjangoModelFormMutation? Just following the example here: https://docs.graphene-python.org/projects/django/en/latest/mutations/#djangomodelformmutation

I think the issue is DjangoModelFormMutation wraps the fields in an envelope (e.g. return_field_name), so trying to spread form.data into the mutation type fails.

return cls(errors=errors, **form.data)

It's late here so forgive me if I'm overlooking something.

@svengt
Copy link

svengt commented Mar 31, 2020

Same here, tried to use DjangoModelFormMutation. When any field does not successfully validate the mutation runs into an exception. Had to role back to version 2.8.1

@jkimbo
Copy link
Member

jkimbo commented Mar 31, 2020

Damn that might well be true. Apologies but we don't have good test coverage in that part of the library so we might have accidentally broken something. I'll try and find some time to fix it.

@jkimbo
Copy link
Member

jkimbo commented Apr 1, 2020

Created a PR to fix it here: #915

@jkimbo
Copy link
Member

jkimbo commented Apr 12, 2020

@jarcoal @svengt I've just released v2.9.1 that should fix the issue: https://github.com/graphql-python/graphene-django/releases/tag/v2.9.1

@jarcoal
Copy link

jarcoal commented Apr 13, 2020

Thanks @jkimbo! It's working for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants