def complete_value_catching_error(exe_context, return_type, field_asts, info, result): # If the field type is non-nullable, then it is resolved without any # protection from errors. if isinstance(return_type, GraphQLNonNull): return complete_value(exe_context, return_type, field_asts, info, result) # Otherwise, error protection is applied, logging the error and # resolving a null value for this field if one is encountered. try: completed = complete_value(exe_context, return_type, field_asts, info, result) if is_thenable(completed): def handle_error(error): exe_context.errors.append(error) return Promise.fulfilled(None) return promisify(completed).then(None, handle_error) return completed except Exception as e: exe_context.errors.append(e) return None
def test_promisify_future_rejected(): future = Future() promise = promisify(future) assert promise.is_pending future.set_exception(Exception('Future rejected')) assert promise.is_rejected assert_exception(promise.reason, Exception, 'Future rejected')
def execute(self, fn, *args, **kwargs): result = fn(*args, **kwargs) if isinstance(result, Future) or iscoroutine(result): future = ensure_future(result, loop=self.loop) self.futures.append(future) return promisify(future) return result
def test_promisify_future(): future = Future() promise = promisify(future) assert promise.is_pending future.set_result(1) assert promise.is_fulfilled assert promise.value == 1
def complete_value(exe_context, return_type, field_asts, info, result): """ Implements the instructions for completeValue as defined in the "Field entries" section of the spec. If the field type is Non-Null, then this recursively completes the value for the inner type. It throws a field error if that completion returns null, as per the "Nullability" section of the spec. If the field type is a List, then this recursively completes the value for the inner type on each item in the list. If the field type is a Scalar or Enum, ensures the completed value is a legal value of the type by calling the `serialize` method of GraphQL type definition. If the field is an abstract type, determine the runtime type of the value and then complete based on that type. Otherwise, the field type expects a sub-selection set, and will complete the value by evaluating all sub-selections. """ # If field type is NonNull, complete for inner type, and throw field error if result is null. if is_thenable(result): return promisify(result).then( lambda resolved: complete_value( exe_context, return_type, field_asts, info, resolved ), lambda error: Promise.rejected(GraphQLLocatedError(field_asts, original_error=error)) ) # print return_type, type(result) if isinstance(result, Exception): raise GraphQLLocatedError(field_asts, original_error=result) if isinstance(return_type, GraphQLNonNull): return complete_nonnull_value(exe_context, return_type, field_asts, info, result) # If result is null-like, return null. if result is None: return None # If field type is List, complete each item in the list with the inner type if isinstance(return_type, GraphQLList): return complete_list_value(exe_context, return_type, field_asts, info, result) # If field type is Scalar or Enum, serialize to a valid value, returning null if coercion is not possible. if isinstance(return_type, (GraphQLScalarType, GraphQLEnumType)): return complete_leaf_value(return_type, result) if isinstance(return_type, (GraphQLInterfaceType, GraphQLUnionType)): return complete_abstract_value(exe_context, return_type, field_asts, info, result) if isinstance(return_type, GraphQLObjectType): return complete_object_value(exe_context, return_type, field_asts, info, result) assert False, u'Cannot complete value of unexpected type "{}".'.format(return_type)
def complete_value(exe_context, return_type, field_asts, info, result): """ Implements the instructions for completeValue as defined in the "Field entries" section of the spec. If the field type is Non-Null, then this recursively completes the value for the inner type. It throws a field error if that completion returns null, as per the "Nullability" section of the spec. If the field type is a List, then this recursively completes the value for the inner type on each item in the list. If the field type is a Scalar or Enum, ensures the completed value is a legal value of the type by calling the `serialize` method of GraphQL type definition. If the field is an abstract type, determine the runtime type of the value and then complete based on that type. Otherwise, the field type expects a sub-selection set, and will complete the value by evaluating all sub-selections. """ # If field type is NonNull, complete for inner type, and throw field error if result is null. if is_thenable(result): return promisify(result).then( lambda resolved: complete_value(exe_context, return_type, field_asts, info, resolved), lambda error: Promise.rejected( GraphQLLocatedError(field_asts, original_error=error))) # print return_type, type(result) if isinstance(result, Exception): raise GraphQLLocatedError(field_asts, original_error=result) if isinstance(return_type, GraphQLNonNull): return complete_nonnull_value(exe_context, return_type, field_asts, info, result) # If result is null-like, return null. if result is None: return None # If field type is List, complete each item in the list with the inner type if isinstance(return_type, GraphQLList): return complete_list_value(exe_context, return_type, field_asts, info, result) # If field type is Scalar or Enum, serialize to a valid value, returning null if coercion is not possible. if isinstance(return_type, (GraphQLScalarType, GraphQLEnumType)): return complete_leaf_value(return_type, result) if isinstance(return_type, (GraphQLInterfaceType, GraphQLUnionType)): return complete_abstract_value(exe_context, return_type, field_asts, info, result) if isinstance(return_type, GraphQLObjectType): return complete_object_value(exe_context, return_type, field_asts, info, result) assert False, u'Cannot complete value of unexpected type "{}".'.format( return_type)
def connection_resolver(cls, resolver, connection_type, root, args, context, info): resolved = resolver(root, args, context, info) on_resolve = partial(cls.resolve_connection, connection_type, args) if is_thenable(resolved): return promisify(resolved).then(on_resolve) return on_resolve(resolved)
def test_promisify_function_rejected(resolve): promisified_func = promisify(sum_function) result = promisified_func(None, None) assert isinstance(result, Promise) with raises(Exception) as exc_info_promise: result.get() with raises(Exception) as exc_info: sum_function(None, None) assert str(exc_info_promise.value) == str(exc_info.value)
def execute_field_callback(results, response_name): field_asts = fields[response_name] result = resolve_field(exe_context, parent_type, source_value, field_asts) if result is Undefined: return results if is_thenable(result): def collect_result(resolved_result): results[response_name] = resolved_result return results return promisify(result).then(collect_result, None) results[response_name] = result return results
def execute_field_callback(results, response_name): field_asts = fields[response_name] result = resolve_field( exe_context, parent_type, source_value, field_asts ) if result is Undefined: return results if is_thenable(result): def collect_result(resolved_result): results[response_name] = resolved_result return results return promisify(result).then(collect_result, None) results[response_name] = result return results
def create_promise(): # unnecessary function call return promisify(instance)
def test_promises_promisify_still_works_but_deprecated_for_non_callables(): x = promisify(1) assert isinstance(x, Promise) assert x.get() == 1
def test_promisify_function_resolved(resolve): promisified_func = promisify(sum_function) result = promisified_func(1, 2) assert isinstance(result, Promise) assert result.get() == 3
def test_promisify_promise(): promise = Promise() assert promisify(promise) == promise
def test_promisify_then_object(): promise = FakeThenPromise() with pytest.raises(Exception) as excinfo: promisify(promise) assert str(excinfo.value) == "FakeThenPromise raises in 'then'"
def test_promisify_object(): with pytest.raises(TypeError) as excinfo: promisify(object()) assert str(excinfo.value) == "Object is not a Promise like object."
def test_promisify_done_object(): promise = FakeDonePromise() with pytest.raises(Exception) as excinfo: promisify(promise) assert str(excinfo.value) == "FakeDonePromise raises in 'done'"