Skip to content

GraphQLError double wrapping returning result #106

Closed
@Checho3388

Description

@Checho3388

I found out something confusing and wanted to know if maybe this could be a bug or if I'm missing something and this is an expected behavior.

This is my example.py file. It only has one resolver that raises an error.

from graphql import (GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString, graphql_sync, )


def resolve_fail(*args, **kwargs):
    raise ValueError("Some error")


schema = GraphQLSchema(
    query=GraphQLObjectType(
        name='RootQueryType',
        fields={
            'hello': GraphQLField(
                GraphQLString,
                resolve=resolve_fail)
        }))

query = '{ hello }'

result = graphql_sync(schema, query)

I'm aware that result.errors[0] is a GraphQLError exception. But, I was expecting result.errors[0].original_error to be ValueError.

However, I can see that result.errors[0].original_error is a GraphQLError and result.errors[0].original_error.original_error is ValueError.

Is this ok?

>>> print(type(result.errors[0]))
<class 'graphql.error.graphql_error.GraphQLError'>
>>> print(type(result.errors[0].original_error))
<class 'graphql.error.graphql_error.GraphQLError'>
>>> print(type(result.errors[0].original_error.original_error))
<class 'ValueError'>

Activity

Cito

Cito commented on Sep 4, 2020

@Cito
Member

Thanks you for reporting this, @Checho3388.

The reason for the double-wrapping is that GraphQL-core wraps the ValueError into a GraphQLError in the method resolve_field_value_or_error() and then wraps that in a located_error again in the method handle_field_error().

The problem is with the first wrapping. GraphQL-core simply tries to do too much here. I think the idea was to mimic the asErrorInstance() function in GraphQL.js, but that function only wraps a non-Error-object into an Error object - it does not create a GraphQLError. Since the case that we get a non-Error-object can be ignored in modern Python, we should simply leave the error as it is in Python and not wrap it.

I will work on a fix and regression test for that.

self-assigned this
on Sep 4, 2020
added
bugSomething isn't working
on Sep 4, 2020
added this to the v3.1 milestone on Sep 4, 2020
Checho3388

Checho3388 commented on Sep 7, 2020

@Checho3388
Author

Glad to know it was fixed. Thanks for your quick response!

patrick91

patrick91 commented on Feb 3, 2021

@patrick91
Member

@Cito was this ever released? I'm encountering the same issue :)

Cito

Cito commented on Feb 3, 2021

@Cito
Member

@patrick91 Not yet, sorry. Will try to work on a new release this weekend.

patrick91

patrick91 commented on Feb 3, 2021

@patrick91
Member

@Cito awesome, thanks for the prompt response, let me know if you need help :)

Cito

Cito commented on Feb 8, 2021

@Cito
Member

Version 3.1.3 with this fix has now been released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    GraphQLError double wrapping returning result · Issue #106 · graphql-python/graphql-core