public async Task <bool> HandleAsync <TCommand>(IResolverContext context, CancellationToken token) { try { var httpContext = (HttpContext)context.ContextData[nameof(HttpContext)]; var headers = new Dictionary <string, string>(); foreach (var header in httpContext.Request.Headers) { headers.Add(header.Key, header.Value.FirstOrDefault()); } using (var _ = AnalyticsService.StartRequestOperation(this, typeof(TCommand).Name, headers)) { var command = context.Argument <TCommand>("command"); if (command == null) { throw new ArgumentException("Unable to extract command from resolver context"); } AnalyticsService.TraceVerbose(this, $"{typeof(TCommand).Name} received", command.ToObjectDictionary()); var commandResult = await HandleCommandAsync <TCommand>(context, command, token); if (commandResult.IsFailure) { context.ReportError(new ErrorBuilder().SetMessage(commandResult.Error.ToString()).Build()); return(default);
public IEnumerable <ICharacter> GetCharacter(string[] characterIds, IResolverContext context) { foreach (string characterId in characterIds) { ICharacter character = _repository.GetCharacter(characterId); if (character == null) { context.ReportError( "Could not resolve a charachter for the " + $"character-id {characterId}."); } else { yield return(character); } } }
private static void ReportErrors( NameString schemaName, IResolverContext context, IEnumerable <IError> errors) { foreach (IError error in errors) { IErrorBuilder builder = ErrorBuilder.FromError(error) .SetExtension(_remoteErrorField, error.RemoveException()) .SetExtension(_schemaNameErrorField, schemaName.Value); if (error.Path != null) { Path path = RewriteErrorPath(error, context.Path); builder.SetPath(path) .ClearLocations() .AddLocation(context.FieldSelection); } else if (IsHttpError(error)) { builder.SetPath(context.Path) .ClearLocations() .AddLocation(context.FieldSelection); } context.ReportError(builder.Build()); } }
private static void ReportErrors( IResolverContext context, IEnumerable <IError> errors) { IReadOnlyCollection <object> path = context.Path.ToCollection(); foreach (IError error in errors) { context.ReportError(error.AddExtension("remote", path)); } }
/// <summary> /// Retrieve data related to user of given username and password. /// </summary> /// <param name="username">A unique username of the user.</param> /// <param name="password">An unhashed password.</param> /// <param name="resolverContext">Resolver context used in error reporting.</param> /// <returns>An instance of proxy class <see cref="UserSignInPayload"/> containing some of user fields.</returns> public UserSignInPayload SignIn(string username, string password, IResolverContext resolverContext) { if (username == null || password == null) { return(null); } var user = _context.Users.SingleOrDefault(x => x.Username == username); if (user == null) { resolverContext.ReportError(ErrorBuilder.New() .SetCode(UserSignInErrorCode.UserNotFound.ToString()) .SetMessage("User of given username was not found!") .Build()); return(null); } if (!_hasher.IsMatching(password, user.Password)) { resolverContext.ReportError(ErrorBuilder.New() .SetCode(UserSignInErrorCode.PasswordMismatch.ToString()) .SetMessage("Wrong password!") .Build()); return(null); } return(new UserSignInPayload() { Id = user.Id, Nickname = user.Nickname, Token = user.Token }); }
public IEnumerable <Department> GetDepartments(string[] departments, IResolverContext context) { foreach (string deptId in departments) { Department dept = _repository.GetDepartment(deptId); if (dept == null) { context.ReportError( "Could not resolve a department for the " + $"department-id {deptId}."); } else { yield return(dept); } } }
public IEnumerable <Student> GetStudents([Parent] Department department, IResolverContext context) { var dept = _repository.GetDepartment(department.Id); if (dept == null) { context.ReportError( "Could not resolve a department for the " + $"department-id {department.Id}."); } else { foreach (var s in dept.Students) { yield return(s); } } }
protected override Task <Response <TPayload> > HandleCommandAsync <TPayload, TCommand>(IResolverContext context, TCommand command, CancellationToken token) { if (!TryExtractUser(context, out var authenticatedUser)) { context.ReportError(new ErrorBuilder().SetMessage(AuthErrors.NotAuthenticated.ToString()).Build()); return(Task.FromResult(AnalyticsService.TraceErrorResponse <TPayload>(this, AuthErrors.NotAuthenticated))); } ; var commandHandler = context.Service <IAuthenticatedCommandHandler <TPayload, TCommand, TUser> >(); if (commandHandler == null) { throw new ArgumentException("No command handler found for command"); } return(commandHandler.HandleAsync(command, authenticatedUser, token)); }
private static void ReportErrors( IResolverContext context, IEnumerable <IError> errors) { foreach (IError error in errors) { IErrorBuilder builder = ErrorBuilder.FromError(error) .SetExtension("remote", error); if (error.Path != null) { Path path = RewriteErrorPath(error, context.Path); builder.SetPath(path) .ClearLocations() .AddLocation(context.FieldSelection); } context.ReportError(builder.Build()); } }
/// <summary> /// Create and store an instance of <see cref="User"/>. /// </summary> /// <param name="nickname">Nickname of the user.</param> /// <param name="username">A unique username of the user.</param> /// <param name="password">An unhashed password.</param> /// <param name="resolverContext">Resolver context used in error reporting.</param> /// <returns>An instance of proxy class <see cref="UserSignUpPayload"/> containing some of user fields.</returns> public UserSignUpPayload SignUp(string nickname, string username, string password, IResolverContext resolverContext) { if (nickname == null || username == null || password == null) { return(null); } if (_context.Users.Any(x => x.Username == username)) { resolverContext.ReportError(ErrorBuilder.New() .SetCode(UserSignUpErrorCode.UsernameNotUnique.ToString()) .SetMessage("Username is not unique!") .Build()); return(null); } var user = new User() { Nickname = nickname, Username = username, Password = _hasher.CreateHash(password) }; // Add and save user to generate Id _context.Add(user); _context.SaveChanges(); // Id and Username to ensures unique hash user.Token = _tokenizer.CreateToken(user.Id + ";" + user.Username); _context.Update(user); _context.SaveChanges(); return(new UserSignUpPayload() { Id = user.Id, Nickname = user.Nickname, Token = user.Token }); }
protected override async Task <Response <bool> > HandleCommandAsync <TCommand>(IResolverContext context, TCommand command, CancellationToken token) { if (!TryExtractUser(context, out var authenticatedUser)) { context.ReportError(new ErrorBuilder().SetMessage(AuthErrors.NotAuthenticated.ToString()).Build()); return(AnalyticsService.TraceErrorResponse <bool>(this, AuthErrors.NotAuthenticated)); } ; var commandHandler = context.Service <IVoidAuthenticatedCommandHandler <TCommand, TUser> >(); if (commandHandler == null) { throw new ArgumentException("No command handler found for command"); } var result = await commandHandler.HandleAsync(command, authenticatedUser, token); return(result.IsFailure ? Response.Failure <bool>(result.Error) : Response.Success(true)); }
private static void ReportError(IResolverContext context, int item, Exception ex) { Path itemPath = context.Path.Append(item); context.ReportError(ex, error => error.SetPath(itemPath)); }
public void ReportError(string errorMessage) => _resolverContext.ReportError(errorMessage);
public string Hello([Service] IResolverContext context) { context.ReportError(new Error("Hello world from error", "001")); return("hello"); }