Closed
Description
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 commentedon Sep 4, 2020
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 alocated_error
again in the methodhandle_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.
Checho3388 commentedon Sep 7, 2020
Glad to know it was fixed. Thanks for your quick response!
patrick91 commentedon Feb 3, 2021
@Cito was this ever released? I'm encountering the same issue :)
Cito commentedon Feb 3, 2021
@patrick91 Not yet, sorry. Will try to work on a new release this weekend.
patrick91 commentedon Feb 3, 2021
@Cito awesome, thanks for the prompt response, let me know if you need help :)
Cito commentedon Feb 8, 2021
Version 3.1.3 with this fix has now been released.