10000 Question: Document how to raise exceptions to get an "errors" list in response · Issue #1469 · graphql-python/graphene · GitHub
[go: up one dir, main page]

Skip to content
Question: Document how to raise exceptions to get an "errors" list in response #1469
Closed
@khink

Description

@khink

First off, this is a question. I apologize for creating a ticket here. As suggested in the "bug report" issue template, i asked on StackOverflow first, but unfortunately there are no responses yet. I realize this does not give me the right to create an issue . I'm hoping my question might benefit the documentation in some way.

I don't know how to raise an exception in such a way that it results in an "errors" list in the response data.

In #1410 (comment), @erikwrede wrote that we can just raise a GraphQLError. I don't think that's documented yet. All i found about GraphQLError usage is with regard to validators: https://docs.graphene-python.org/en/latest/execution/queryvalidation/#implementing-custom-validators There's nothing there that says that the response will have an "errors" list.

The reason is ask is that this used to work for us with older Graphene versions (graphene==3.0, graphene-django==3.0.0b7, graphql-core==3.1.7) but stopped working after we upgrade (graphene==3.1.1, graphene-django==3.0.0 and graphql-core==3.2.3).

This used to work:

class DeleteOrderlineMutation(graphene.Mutation):
    Output = graphene.ID()

    class Arguments:
        id = graphene.UUID()
        order_id = graphene.ID()

    @classmethod
    def mutate(cls, root, info, id, order_id):
        user = info.context.user
        order = Order.objects.for_user(user).get(pk=order_id)
        if not order.is_editable_by(user):
            raise GraphQLError(
                "Order mutation not allowed, Orderline can not be deleted."
            )

It would result in a response like this:

{
  ...
  "errors": [
      {"message": "Order mutation not allowed, Orderline can not be deleted.", ...}
  ]
}

However, after upgrade this gives

{
  'data': {
    'deleteOrderline': "<Promise at 0x7f899900d5b0 rejected with GraphQLError('Order mutation not allowed, Orderline can not be deleted.')>"
  }
}

I've read through the changelog of graphene and graphene-django. As this "broke" (for us) in graphene-django 3.0.0b8, i thought graphql-python/graphene-django#1327 might have something to do with it. But then, that also requires a newer version of graphene.

Thanks for taking the time to read this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0