def complete_list_value(exe_context, return_type, field_asts, info, result): """ Complete a list value by completing each item in the list with the inner type """ assert isinstance(result, collections.Iterable), \ ('User Error: expected iterable, but did not find one ' + 'for field {}.{}.').format(info.parent_type, info.field_name) item_type = return_type.of_type completed_results = [] contains_promise = False index = 0 path = info.path[:] for item in result: info.path = path + [index] completed_item = complete_value_catching_error(exe_context, item_type, field_asts, info, item) if not contains_promise and is_thenable(completed_item): contains_promise = True completed_results.append(completed_item) index += 1 return Promise.all( completed_results) if contains_promise else completed_results
def resolve(self, root): if root and not self.have_type(root): raise GraphQLError( u'Expected value of type "{}" but got: {}.'.format( self.type, type(root).__name__), self.info.field_asts) contains_promise = False final_results = self.fragment_container() # return OrderedDict( # ((field_name, field_resolver(root, field_args, context, info)) # for field_name, field_resolver, field_args, context, info in self.partial_resolvers) # ) for response_name, field_resolver in self.partial_resolvers: result = field_resolver.execute(root) if result is Undefined: continue if not contains_promise and is_thenable(result): contains_promise = True final_results[response_name] = result if not contains_promise: return final_results return promise_for_dict(final_results)
def execute(self, *args, **kwargs): executed = self.schema.execute(*args, **dict(self.execute_options, **kwargs)) if is_thenable(executed): return Promise.resolve(executed).then(self.format_result) return self.format_result(executed)
def wrapper(cls, root, info, password, **kwargs): def on_resolve(values): user, payload = values payload.token = get_token(user, info.context) return payload username = kwargs.get(get_user_model().USERNAME_FIELD) user = authenticate( request=info.context, username=username, password=password) if user is None: raise exceptions.GraphQLJWTError( _('Please, enter valid credentials')) if hasattr(info.context, 'user'): info.context.user = user result = f(cls, root, info, **kwargs) values = (user, result) # Improved mutation with thenable check if is_thenable(result): return Promise.resolve(values).then(on_resolve) return on_resolve(values)
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): traceback = completed._traceback exe_context.report_error(error, traceback) return None return completed.catch(handle_error) return completed except Exception as e: traceback = sys.exc_info()[2] exe_context.report_error(e, traceback) return None
def wrapper(cls, root, info, password, **kwargs): def on_resolve(values): user, payload = values payload.token = get_token(user, info.context) return payload username = kwargs.get(get_user_model().USERNAME_FIELD) if get_authorization_header(info.context) is not None: del info.context.Meta[jwt_settings.JWT_AUTH_HEADER] user = authenticate(request=info.context, username=username, password=password) if user is None: raise exceptions.GraphQLJWTError( _('Please, enter valid credentials')) if hasattr(info.context, 'user'): info.context.user = user result = f(cls, root, info, **kwargs) values = (user, result) if is_thenable(result): return Promise.resolve(values).then(on_resolve) return on_resolve(values)
def test_already_authenticated(self): @decorators.token_auth def wrapped(cls, root, info, **kwargs): return Promise() headers = { jwt_settings.JWT_AUTH_HEADER_NAME: '{0} {1}'.format( jwt_settings.JWT_AUTH_HEADER_PREFIX, self.token), } info_mock = self.info(AnonymousUser(), **headers) result = wrapped( self, None, info_mock, password='******', username=self.user.get_username()) self.assertIsNotNone(is_thenable(result)) self.assertNotIn( jwt_settings.JWT_AUTH_HEADER_NAME, info_mock.context.META)
def wrapper(cls, root, info, password, **kwargs): def on_resolve(values): user, payload = values payload.token = get_token(user, info.context) if jwt_settings.JWT_LONG_RUNNING_REFRESH_TOKEN: payload.refresh_token = create_refresh_token(user).get_token() return payload username = kwargs.get(get_user_model().USERNAME_FIELD) if get_authorization_header(info.context) is not None: del info.context.META[jwt_settings.JWT_AUTH_HEADER_NAME] user = authenticate(request=info.context, username=username, password=password) if user is None: raise exceptions.JSONWebTokenError( _('Please, enter valid credentials')) if hasattr(info.context, 'user'): info.context.user = user user_logged_in.send(sender=user.__class__, request=info.context, user=user) result = f(cls, root, info, **kwargs) values = (user, result) if is_thenable(result): return Promise.resolve(values).then(on_resolve) return on_resolve(values)
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 Promise.resolve(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 execute_field_callback(results, response_name): # type: (Dict, str) -> Union[Dict, Promise[Dict]] field_asts = fields[response_name] result = resolve_field( exe_context, parent_type, source_value, field_asts, None, path + [response_name], ) if result is Undefined: return results if is_thenable(result): def collect_result(resolved_result): # type: (Dict) -> Dict results[response_name] = resolved_result return results return result.then(collect_result, None) results[response_name] = result return results
def wrapper(cls, root, info, password, **kwargs): def on_resolve(values): user, payload = values payload.token = get_token(user, info.context) return payload username = kwargs.get(get_user_model().USERNAME_FIELD) user = authenticate(request=info.context, username=username, password=password) if user is None: raise exceptions.GraphQLJWTError( # _('Please, enter valid credentials')) _('请输入正确的账号密码!')) if hasattr(info.context, 'user'): info.context.user = user result = f(cls, root, info, **kwargs) values = (user, result) # Improved mutation with thenable check if is_thenable(result): return Promise.resolve(values).then(on_resolve) return on_resolve(values)
def execute_fields( exe_context, # type: ExecutionContext parent_type, # type: GraphQLObjectType source_value, # type: Any fields, # type: DefaultOrderedDict path, # type: List[Union[int, str]] info, # type: Optional[ResolveInfo] ): # type: (...) -> Union[Dict, Promise[Dict]] contains_promise = False final_results = OrderedDict() for response_name, field_asts in fields.items(): result = resolve_field( exe_context, parent_type, source_value, field_asts, info, path + [response_name], ) if result is Undefined: continue final_results[response_name] = result if is_thenable(result): contains_promise = True if not contains_promise: return final_results return promise_for_dict(final_results)
def wrapper(root, info, password, **kwargs): def on_resolve(values): user, payload = values payload['token'] = get_token(user, info.context) if jwt_settings.JWT_LONG_RUNNING_REFRESH_TOKEN: payload['refresh_token'] = create_refresh_token(user).token return payload username = kwargs.get(get_user_model().USERNAME_FIELD) if get_authorization_header(info.context.get('request')) is not None: del info.context.get('request').META[jwt_settings.JWT_AUTH_HEADER] user = authenticate(info.context.get('request'), username=username, password=password) if user is None: raise exceptions.JSONWebTokenError( _('Please, enter valid credentials')) if hasattr(info.context.get('request'), 'user'): info.context.get('request').user = user result = f(root, info, **kwargs) values = (user, result) # Improved mutation with thenable check if is_thenable(result): return Promise.resolve(values).then(on_resolve) return on_resolve(values)
def mutate(cls, root, info, input): """ Most code derived one-to-one from base class :return: """ def on_resolve(payload): try: payload.client_mutation_id = input.get("client_mutation_id") except Exception: raise Exception( "Cannot set client_mutation_id in the payload object {}".format( repr(payload) ) ) return payload result = cls.mutate_and_get_payload(root, info, **input) if result.errors: err_msg = '' for err in result.errors: err_msg += f"Field '{err.field}': {err.messages[0]} " raise GraphQLError(err_msg.strip()) if is_thenable(result): return Promise.resolve(result).then(on_resolve) return on_resolve(result)
def complete_value_catching_error( exe_context, # type: ExecutionContext return_type, # type: Any field_asts, # type: List[Field] info, # type: ResolveInfo path, # type: List[Union[int, str]] result, # type: Any ): # type: (...) -> Any # 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, path, 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, path, result) if is_thenable(completed): def handle_error(error): # type: (Union[GraphQLError, GraphQLLocatedError]) -> Optional[Any] traceback = completed._traceback # type: ignore exe_context.report_error(error, traceback) return None return completed.catch(handle_error) return completed except Exception as e: traceback = sys.exc_info()[2] exe_context.report_error(e, traceback) return None
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 complete_list_value( exe_context, # type: ExecutionContext return_type, # type: GraphQLList field_asts, # type: List[Field] info, # type: ResolveInfo path, # type: List[Union[int, str]] result, # type: Any ): # type: (...) -> List[Any] """ Complete a list value by completing each item in the list with the inner type """ assert isinstance( result, Iterable), ("User Error: expected iterable, but did not find one " + "for field {}.{}.").format(info.parent_type, info.field_name) item_type = return_type.of_type completed_results = [] contains_promise = False index = 0 for item in result: completed_item = complete_value_catching_error(exe_context, item_type, field_asts, info, path + [index], item) if not contains_promise and is_thenable(completed_item): contains_promise = True completed_results.append(completed_item) index += 1 return Promise.all( completed_results) if contains_promise else completed_results
def wrapper(cls, root, info, password, **kwargs): def on_resolve(values): user, payload = values payload.token = get_token(user, info.context) if jwt_settings.JWT_LONG_RUNNING_REFRESH_TOKEN: payload.refresh_token = refresh_token_lazy(user) return payload username = kwargs.get(get_user_model().USERNAME_FIELD) user = authenticate(request=info.context, username=username, password=password, skip_jwt_backend=True) if user is None: raise exceptions.JSONWebTokenError( _('Please, enter valid credentials')) if hasattr(info.context, 'user'): info.context.user = user result = f(cls, root, info, **kwargs) values = (user, result) if is_thenable(result): return Promise.resolve(values).then(on_resolve) return on_resolve(values)
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, model, root, info, **args): resolved = resolver(root, info, **args) on_resolve = partial(cls.resolve_connection, connection_type, model, info, args) if is_thenable(resolved): return Promise.resolve(resolved).then(on_resolve) return on_resolve(resolved)
def connection_resolver(cls, resolver, connection_type, root, info, **args): resolved = resolver(root, info, **args) on_resolve = partial(cls.resolve_connection, connection_type, args) if is_thenable(resolved): return Promise.resolve(resolved).then(on_resolve) return on_resolve(resolved)
def complete_object_value(fragment_resolve, exe_context, on_error, result): if result is None: return None result = fragment_resolve(result) if is_thenable(result): return result.catch(on_error) return result
def test_social_auth_thenable(self, *args): @decorators.social_auth def wrapped(cls, root, info, provider, *args): return Promise() result = wrapped(TestCase, None, MagicMock(), 'google-oauth2', 'token') self.assertTrue(is_thenable(result))
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 mutate(cls, root, info, input): def on_resolve(payload): return payload result = cls.mutate_and_get_payload(root, info, **input) if is_thenable(result): return Promise.resolve(result).then(on_resolve) return on_resolve(result)
def resolve(self, next, root, info, **args): result = next(root, info, **args) if is_thenable(result): result.catch(self.on_error) # Top level only if R.length(info.path) == 1: result.then(lambda response: log_request_body(info, response)) return result
def wrapper(cls, root, info, social, **kwargs): def on_resolve(payload): payload.social = social return payload result = f(cls, root, info, social, **kwargs) if is_thenable(result): return Promise.resolve(result).then(on_resolve) return on_resolve(result)
def wrapper(cls, root, info, **kwargs): context = info.context context._jwt_token_auth = True def on_resolve(values): user, payload = values payload.token = get_token(user, context) if jwt_settings.JWT_LONG_RUNNING_REFRESH_TOKEN: payload.refresh_token = refresh_token_lazy(user) return payload token = kwargs.get('access_token') backend = kwargs.get('backend') if conf.settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY is None or conf.settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET is None: authorization_key = AuthorizationKey.objects.get(name="google-oauth2") conf.settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = authorization_key.key conf.settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = authorization_key.password if conf.settings.SOCIAL_AUTH_FACEBOOK_KEY is None or conf.settings.SOCIAL_AUTH_FACEBOOK_SECRET is None: authorization_key = AuthorizationKey.objects.get(name="facebook") conf.settings.SOCIAL_AUTH_FACEBOOK_KEY = authorization_key.key conf.settings.SOCIAL_AUTH_FACEBOOK_SECRET = authorization_key.password context.social_strategy = load_strategy(context) # backward compatibility in attribute name, only if not already # defined if not hasattr(context, 'strategy'): context.strategy = context.social_strategy uri = reverse('social:complete', args=(backend,)) context.backend = load_backend(context.social_strategy, backend, uri) user_data = context.backend.user_data(token) if not user_data or not (user_data["email"] and user_data["email"].strip()): LOG.error('Empty email or id from social login received') LOG.error(user_data) raise JSONWebTokenError(_('Please, enter valid credentials')) user = context.backend.do_auth(token) if hasattr(context, 'user'): context.user = user result = f(cls, root, info, **kwargs) values = (user, result) token_issued.send(sender=cls, request=context, user=user) if is_thenable(result): return Promise.resolve(values).then(on_resolve) return on_resolve(values)
def connection_resolver(cls, resolver, connection_type, root, info, **args): resolved = resolver(root, info, **args) if isinstance(connection_type, NonNull): connection_type = connection_type.of_type on_resolve = partial(cls.resolve_connection, connection_type, args) if is_thenable(resolved): return Promise.resolve(resolved).then(on_resolve) return on_resolve(resolved)
def wrapper(cls, root, info, social, **kwargs): def on_resolve(payload): payload.social = social return payload result = f(cls, root, info, social, **kwargs) # Improved mutation with thenable check if is_thenable(result): return Promise.resolve(result).then(on_resolve) return on_resolve(result)
def test_token_auth_thenable(self): @decorators.token_auth def wrapped(cls, root, info, **kwargs): return Promise() result = wrapped(self, None, mock.Mock(), password='******', username=self.user.get_username()) self.assertTrue(is_thenable(result))
def test_schema_returns_promise(): object() query = ''' { name pet { type } } ''' result = schema.execute(query, root_value=object(), return_promise=True) assert is_thenable(result)
def test_schema_returns_promise(): object() query = """ { name pet { type } } """ result = schema.execute(query, root_value=object(), return_promise=True) assert is_thenable(result)
def batch_load_fn(self, keys): func = self.batch_load if isgeneratorfunction(func): func = genfunc_to_prom(func) result = func(keys) if not is_thenable(result): # batch_load_fn must always return a promise return Promise.resolve(result) return result
def test_token_auth_thenable(self): @decorators.token_auth def wrapped(cls, root, info, **kwargs): return Promise() result = wrapped( self, None, mock.Mock(), password='******', username=self.user.get_username()) self.assertTrue(is_thenable(result))
def mutate(cls, root, info, input): def on_resolve(payload): try: payload.client_mutation_id = input.get('client_mutation_id') except: raise Exception( ('Cannot set client_mutation_id in the payload object {}' ).format(repr(payload))) return payload result = cls.mutate_and_get_payload(root, info, **input) if is_thenable(result): return Promise.resolve(result).then(on_resolve) return on_resolve(result)
def execute_fields(exe_context, parent_type, source_value, fields): contains_promise = False final_results = collections.OrderedDict() for response_name, field_asts in fields.items(): result = resolve_field(exe_context, parent_type, source_value, field_asts) if result is Undefined: continue final_results[response_name] = result if is_thenable(result): contains_promise = True if not contains_promise: return final_results return promise_for_dict(final_results)
def complete_list_value(exe_context, return_type, field_asts, info, result): """ Complete a list value by completing each item in the list with the inner type """ assert isinstance(result, collections.Iterable), \ ('User Error: expected iterable, but did not find one ' + 'for field {}.{}.').format(info.parent_type, info.field_name) item_type = return_type.of_type completed_results = [] contains_promise = False for item in result: completed_item = complete_value_catching_error(exe_context, item_type, field_asts, info, item) if not contains_promise and is_thenable(completed_item): contains_promise = True completed_results.append(completed_item) return Promise.all(completed_results) if contains_promise else completed_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 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): traceback = completed._traceback exe_context.report_error(error, traceback) return None return completed.catch(handle_error) return completed except Exception as e: traceback = sys.exc_info()[2] exe_context.report_error(e, traceback) return None
def test_is_thenable_then_object(): promise = FakeThenPromise() assert is_thenable(promise)
async def test_coroutine_is_thenable(): async def my_coroutine(): await sleep(.01) return True assert is_thenable(my_coroutine())
def test_is_thenable_done_object(): promise = FakeDonePromise() assert is_thenable(promise)
def test_is_thenable_future(): promise = Future() assert is_thenable(promise)
def create_promise(): return is_thenable(True)
def test_is_thenable_promise(): promise = Promise() assert is_thenable(promise)
def create_promise(): return is_thenable(my_type_instance)
def test_is_thenable_simple_object(): assert not is_thenable(object())