33import logging
44import sys
55
6- from promise import Promise , is_thenable , promise_for_dict , promisify
6+ from promise import Promise , promise_for_dict , promisify
77
88from ..error import GraphQLError , GraphQLLocatedError
99from ..pyutils .default_ordered_dict import DefaultOrderedDict
10+ from ..pyutils .ordereddict import OrderedDict
1011from ..type import (GraphQLEnumType , GraphQLInterfaceType , GraphQLList ,
1112 GraphQLNonNull , GraphQLObjectType , GraphQLScalarType ,
1213 GraphQLSchema , GraphQLUnionType )
1920logger = logging .getLogger (__name__ )
2021
2122
23+ def is_promise (obj ):
24+ return type (obj ) == Promise
25+
26+
2227def execute (schema , document_ast , root_value = None , context_value = None ,
2328 variable_values = None , operation_name = None , executor = None ,
2429 return_promise = False , middlewares = None ):
@@ -92,7 +97,7 @@ def execute_field_callback(results, response_name):
9297 if result is Undefined :
9398 return results
9499
95- if is_thenable (result ):
100+ if is_promise (result ):
96101 def collect_result (resolved_result ):
97102 results [response_name ] = resolved_result
98103 return results
@@ -111,15 +116,15 @@ def execute_field(prev_promise, response_name):
111116def execute_fields (exe_context , parent_type , source_value , fields ):
112117 contains_promise = False
113118
114- final_results = collections . OrderedDict ()
119+ final_results = OrderedDict ()
115120
116121 for response_name , field_asts in fields .items ():
117122 result = resolve_field (exe_context , parent_type , source_value , field_asts )
118123 if result is Undefined :
119124 continue
120125
121126 final_results [response_name ] = result
122- if is_thenable (result ):
127+ if is_promise (result ):
123128 contains_promise = True
124129
125130 if not contains_promise :
@@ -198,7 +203,7 @@ def complete_value_catching_error(exe_context, return_type, field_asts, info, re
198203 # resolving a null value for this field if one is encountered.
199204 try :
200205 completed = complete_value (exe_context , return_type , field_asts , info , result )
201- if is_thenable (completed ):
206+ if is_promise (completed ):
202207 def handle_error (error ):
203208 exe_context .errors .append (error )
204209 return Promise .fulfilled (None )
@@ -232,7 +237,7 @@ def complete_value(exe_context, return_type, field_asts, info, result):
232237 """
233238 # If field type is NonNull, complete for inner type, and throw field error if result is null.
234239
235- if is_thenable (result ):
240+ if is_promise (result ):
236241 return promisify (result ).then (
237242 lambda resolved : complete_value (
238243 exe_context ,
@@ -248,16 +253,7 @@ def complete_value(exe_context, return_type, field_asts, info, result):
248253 raise GraphQLLocatedError (field_asts , original_error = result )
249254
250255 if isinstance (return_type , GraphQLNonNull ):
251- completed = complete_value (
252- exe_context , return_type .of_type , field_asts , info , result
253- )
254- if completed is None :
255- raise GraphQLError (
256- 'Cannot return null for non-nullable field {}.{}.' .format (info .parent_type , info .field_name ),
257- field_asts
258- )
259-
260- return completed
256+ return complete_nonnull_value (exe_context , return_type , field_asts , info , result )
261257
262258 # If result is null-like, return null.
263259 if result is None :
@@ -293,7 +289,7 @@ def complete_list_value(exe_context, return_type, field_asts, info, result):
293289 contains_promise = False
294290 for item in result :
295291 completed_item = complete_value_catching_error (exe_context , item_type , field_asts , info , item )
296- if not contains_promise and is_thenable (completed_item ):
292+ if not contains_promise and is_promise (completed_item ):
297293 contains_promise = True
298294
299295 completed_results .append (completed_item )
@@ -305,15 +301,10 @@ def complete_leaf_value(return_type, result):
305301 """
306302 Complete a Scalar or Enum by serializing to a valid value, returning null if serialization is not possible.
307303 """
308- serialize = getattr (return_type , 'serialize' , None )
309- assert serialize , 'Missing serialize method on type'
304+ # serialize = getattr(return_type, 'serialize', None)
305+ # assert serialize, 'Missing serialize method on type'
310306
311- serialized_result = serialize (result )
312-
313- if serialized_result is None :
314- return None
315-
316- return serialized_result
307+ return return_type .serialize (result )
317308
318309
319310def complete_abstract_value (exe_context , return_type , field_asts , info , result ):
@@ -368,14 +359,21 @@ def complete_object_value(exe_context, return_type, field_asts, info, result):
368359 )
369360
370361 # Collect sub-fields to execute to complete this value.
371- subfield_asts = DefaultOrderedDict (list )
372- visited_fragment_names = set ()
373- for field_ast in field_asts :
374- selection_set = field_ast .selection_set
375- if selection_set :
376- subfield_asts = collect_fields (
377- exe_context , return_type , selection_set ,
378- subfield_asts , visited_fragment_names
379- )
380-
362+ subfield_asts = exe_context .get_sub_fields (return_type , field_asts )
381363 return execute_fields (exe_context , return_type , result , subfield_asts )
364+
365+
366+ def complete_nonnull_value (exe_context , return_type , field_asts , info , result ):
367+ """
368+ Complete a NonNull value by completing the inner type
369+ """
370+ completed = complete_value (
371+ exe_context , return_type .of_type , field_asts , info , result
372+ )
373+ if completed is None :
374+ raise GraphQLError (
375+ 'Cannot return null for non-nullable field {}.{}.' .format (info .parent_type , info .field_name ),
376+ field_asts
377+ )
378+
379+ return completed
0 commit comments