1
+ import sys
2
+ import traceback
1
3
import collections
2
4
from functools import partial
3
5
6
+ from ..base import default_resolve_fn
4
7
from ...error import GraphQLError , GraphQLLocatedError
5
8
from ...type import (GraphQLEnumType , GraphQLInterfaceType , GraphQLList ,
6
9
GraphQLNonNull , GraphQLObjectType , GraphQLScalarType ,
@@ -14,15 +17,26 @@ def is_promise(value):
14
17
return type (value ) == Promise
15
18
16
19
17
- def on_complete_resolver (__func , exe_context , info , __resolver , * args , ** kwargs ):
20
+ def on_complete_resolver (catch_error , __func , exe_context , info , __resolver , * args , ** kwargs ):
18
21
try :
19
22
result = __resolver (* args , ** kwargs )
20
23
except Exception , e :
21
- exe_context .errors .append (e )
22
- return None
24
+ # print e, __resolver, info.field_name, traceback.print_tb(sys.exc_info()[2])
25
+ error = GraphQLLocatedError (info .field_asts , original_error = e )
26
+ if catch_error :
27
+ exe_context .errors .append (error )
28
+ return None
29
+ raise error
23
30
24
31
if is_promise (result ):
25
- return result .then (__func )
32
+ def on_error (e ):
33
+ error = GraphQLLocatedError (info .field_asts , original_error = e )
34
+ if catch_error :
35
+ exe_context .errors .append (error )
36
+ return None
37
+ raise error
38
+
39
+ return result .then (__func , on_error )
26
40
return __func (result )
27
41
28
42
@@ -36,11 +50,7 @@ def complete_list_value(inner_resolver, exe_context, info, result):
36
50
37
51
completed_results = []
38
52
for item in result :
39
- try :
40
- completed_item = inner_resolver (item )
41
- except Exception , e :
42
- completed_item = None
43
- exe_context .errors .append (e )
53
+ completed_item = inner_resolver (item )
44
54
completed_results .append (completed_item )
45
55
46
56
return completed_results
@@ -63,45 +73,60 @@ def complete_object_value(fragment_resolve, result):
63
73
64
74
65
75
def field_resolver (field , fragment = None , exe_context = None , info = None ):
66
- return type_resolver (field .type , field .resolver , fragment , exe_context , info )
76
+ return type_resolver (field .type , field .resolver or default_resolve_fn , fragment , exe_context , info , catch_error = True )
77
+ # def new_resolver(*args, **kwargs):
78
+ # try:
79
+ # result = resolver(*args, **kwargs)
80
+ # except Exception, e:
81
+ # exe_context.errors.append(e)
82
+
83
+ # return new_resolver
67
84
68
85
69
- def type_resolver (return_type , resolver , fragment = None , exe_context = None , info = None ):
86
+ def type_resolver (return_type , resolver , fragment = None , exe_context = None , info = None , catch_error = False ):
70
87
if isinstance (return_type , GraphQLNonNull ):
71
88
return type_resolver_non_null (return_type , resolver , fragment , exe_context , info )
72
89
73
90
if isinstance (return_type , (GraphQLScalarType , GraphQLEnumType )):
74
- return type_resolver_leaf (return_type , resolver , exe_context , info )
91
+ return type_resolver_leaf (return_type , resolver , exe_context , info , catch_error )
75
92
76
93
if isinstance (return_type , (GraphQLList )):
77
- return type_resolver_list (return_type , resolver , fragment , exe_context , info )
94
+ return type_resolver_list (return_type , resolver , fragment , exe_context , info , catch_error )
78
95
79
96
if isinstance (return_type , (GraphQLObjectType )):
80
97
assert fragment and fragment .type == return_type , 'Fragment and return_type dont match'
81
- complete_object_value_resolve = partial (complete_object_value , fragment .resolve )
82
- return partial (on_complete_resolver , complete_object_value_resolve , exe_context , info , resolver )
98
+ return type_resolver_type (return_type , resolver , fragment , exe_context , info , catch_error )
99
+ # complete_object_value_resolve = partial(complete_object_value, fragment.resolve)
100
+ # return partial(on_complete_resolver, complete_object_value_resolve, exe_context, info, resolver)
83
101
# return partial(fragment.resolver, resolver)
84
102
85
103
if isinstance (return_type , (GraphQLInterfaceType , GraphQLUnionType )):
86
104
assert fragment , 'You need to pass a fragment to resolve a Interface or Union'
87
- return partial (on_complete_resolver , fragment .resolve , exe_context , info , resolver )
105
+ return type_resolver_type (return_type , resolver , fragment , exe_context , info , catch_error )
106
+ # return partial(on_complete_resolver, fragment.resolve, exe_context, info, resolver)
88
107
# return partial(fragment.resolver, resolver)
89
108
# return partial(fragment.abstract_resolver, resolver, return_type)
90
109
91
110
raise Exception ("The resolver have to be created for a fragment" )
92
111
93
112
94
- def type_resolver_non_null (return_type , resolver , fragment , exe_context , info ):
113
+ def type_resolver_type (return_type , resolver , fragment , exe_context , info , catch_error ):
114
+ complete_object_value_resolve = partial (complete_object_value , fragment .resolve )
115
+ return partial (on_complete_resolver , catch_error , complete_object_value_resolve , exe_context , info , resolver )
116
+
117
+
118
+ def type_resolver_non_null (return_type , resolver , fragment , exe_context , info ): # no catch_error
95
119
resolver = type_resolver (return_type .of_type , resolver , fragment , exe_context , info )
96
120
nonnull_complete = partial (complete_nonnull_value , exe_context , info )
97
- return partial (on_complete_resolver , nonnull_complete , exe_context , info , resolver )
121
+ return partial (on_complete_resolver , False , nonnull_complete , exe_context , info , resolver )
98
122
99
123
100
- def type_resolver_leaf (return_type , resolver , exe_context , info ):
101
- return partial (on_complete_resolver , return_type .serialize , exe_context , info , resolver )
124
+ def type_resolver_leaf (return_type , resolver , exe_context , info , catch_error ):
125
+ return partial (on_complete_resolver , catch_error , return_type .serialize , exe_context , info , resolver )
102
126
103
127
104
- def type_resolver_list (return_type , resolver , fragment , exe_context , info ):
105
- inner_resolver = type_resolver (return_type .of_type , lambda item : item , fragment , exe_context , info )
128
+ def type_resolver_list (return_type , resolver , fragment , exe_context , info , catch_error ):
129
+ item_type = return_type .of_type
130
+ inner_resolver = type_resolver (item_type , lambda item : item , fragment , exe_context , info )
106
131
list_complete = partial (complete_list_value , inner_resolver , exe_context , info )
107
- return partial (on_complete_resolver , list_complete , exe_context , info , resolver )
132
+ return partial (on_complete_resolver , catch_error , list_complete , exe_context , info , resolver )
0 commit comments