public async Task <IDictionary <Id, Tuple <Todo, Id> > > Handle(GetTodosListRequest request, CancellationToken cancellationToken) { var query = _items; if (request.Status.HasValue) { var val = request.Status.Value; query = query .Where(x => x.Status == val); } if (request.SpecifiedIds != null) { var idList = request.SpecifiedIds .Select(x => (Guid)x) .ToList(); query = query .Where(x => idList.Contains(x.Id)); } if (request.OnlyUnassigned) { query = query .Where(x => !x.AssignedUserId.HasValue); } else if (request.AssignedUserIds != null) { var idList = request.AssignedUserIds .Select(x => (Guid)x) .ToList(); query = query .Where(x => x.AssignedUserId.HasValue && idList.Contains(x.AssignedUserId.Value)); } if (!string.IsNullOrEmpty(request.SearchText)) { query = query .Where(x => x.Name.Contains(request.SearchText)); } query = query .OrderBy(x => x.CreatedAt); var entities = await query .ToListAsync(cancellationToken); return(entities.ToDictionary(x => (Id)x.Id, x => Tuple.Create(x.ToModel(), (Id)x.AssignedUserId))); }
public UserType(IHttpContextAccessor accessor, IDataLoaderContextAccessor dataLoaderContextAccessor) { Field <IdGraphType>() .Name("Id") .Resolve(x => (Guid)x.Source.Key); Field(x => x.Value.Username); Field(x => x.Value.Email); Field(x => x.Value.Picture); Field <ListGraphType <TodoType>, IEnumerable <KeyValuePair <Id, Tuple <Todo, Id> > > >() .Name("Todos") .ResolveAsync(async x => { var userId = x.Source.Key; var mediator = accessor.GetMediator(); var loader = dataLoaderContextAccessor.Context.GetOrAddCollectionBatchLoader <Id, IEnumerable <KeyValuePair <Id, Tuple <Todo, Id> > > >( TasksByUserKey, async(ids, cancellationToken) => { var userIds = ids .Distinct() .ToArray(); var request = new GetTodosListRequest { AssignedUserIds = userIds }; var todos = await mediator.Send(request, cancellationToken); return(userIds .ToDictionary(id => id, id => todos.Where(t => t.Value.Item2 == id)) .ToLookup(id => id.Key, id => id.Value)); }); var allTodos = await loader.LoadAsync(userId); return(allTodos .SelectMany(u => u .Where(z => z.Value.Item2 == userId) )); }); }
public Mutation(IHttpContextAccessor accessor) { Field <TodoType, KeyValuePair <Id, Tuple <Todo, Id> > >() .Name("createTodo") .Argument <NonNullGraphType <AddTodoInputType> >("todo", "Todo input.") .ResolveAsync(async context => { var mediator = accessor.GetMediator(); var input = context.GetArgument <AddTodoInput>("todo"); if (string.IsNullOrEmpty(input.Name)) { context.Errors.Add(new ExecutionError("Name should not be empty") { Code = "EmptyName" }); } if (context.Errors.Any()) { return(new KeyValuePair <Id, Tuple <Todo, Id> >(null, null)); } var request = input.ToCommand(); var result = await mediator.Send(request); return(result.Single()); }); Field <TodoType, KeyValuePair <Id, Tuple <Todo, Id> > >() .Name("updateTodo") .Argument <NonNullGraphType <UpdateTodoInputType> >("todo", "Todo input.") .ResolveAsync(async context => { var mediator = accessor.GetMediator(); var input = context.GetArgument <UpdateTodoInput>("todo"); if (string.IsNullOrEmpty(input.Name)) { context.Errors.Add(new ExecutionError("Name should not be empty") { Code = "EmptyName" }); } if (input.Id == Guid.Empty) { context.Errors.Add(new ExecutionError("Id should not be empty") { Code = "EmptyId" }); } if (context.Errors.Any()) { return(new KeyValuePair <Id, Tuple <Todo, Id> >(null, null)); } var request = input.ToCommand(); var currentItemsRequest = new GetTodosListRequest { SpecifiedIds = request.Updates.Keys }; var oldTodos = await mediator.Send(currentItemsRequest); request.OldTodos = oldTodos; var todoContext = context.Arguments["todo"] as Dictionary <string, object>; if (!todoContext.ContainsKey("description")) { var oldItem = oldTodos[input.Id].Item1; request.Updates[input.Id].Item1.Description = oldItem.Description; } var result = await mediator.Send(request); return(result.Single()); }); Field <TodoType, KeyValuePair <Id, Tuple <Todo, Id> > >() .Name("deleteTodo") .Argument <NonNullGraphType <IdGraphType>, Guid>("Id", "Todo id.") .ResolveAsync(async context => { var mediator = accessor.GetMediator(); var id = context.GetArgument <Guid>("id"); var command = new DeleteTodosCommand { TodoIds = new [] { (Id)id } }; var result = await mediator.Send(command); return(result.Single()); }); Field <UserType, KeyValuePair <Id, User> >() .Name("signIn") .Argument <NonNullGraphType <StringGraphType> >("token", "Google token.") .ResolveAsync(async context => { var mediator = accessor.GetMediator(); var input = context.GetArgument <string>("token"); var command = new SignInCommand { Token = input }; var result = await mediator.Send(command); return(result.Single()); }); }
public Query(IHttpContextAccessor accessor) { Name = "query"; Field <ListGraphType <TodoType>, IDictionary <Id, Tuple <Todo, Id> > >() .Name("todos") .Argument <TodoStatusType, TodoStatus?>("status", "Todo status.") .Argument <StringGraphType, string>("searchText", "Search text.") .Argument <StringGraphType, string>("assignedUser", "Assigned user.") .ResolveAsync(async context => { var mediator = (IMediator)accessor.HttpContext.RequestServices.GetService(typeof(IMediator)); var status = context.GetArgument <TodoStatus?>("status"); var searchText = context.GetArgument <string>("searchText"); var assignedUser = context.GetArgument <string>("assignedUser"); var request = new GetTodosListRequest { Status = status, SearchText = searchText }; if (assignedUser == "unassigned") { request.OnlyUnassigned = true; } if (Guid.TryParse(assignedUser, out var userId)) { request.AssignedUserIds = new [] { (Id)userId }; } return(await mediator.Send(request)); }); Field <TodoType, KeyValuePair <Id, Tuple <Todo, Id> > >() .Name("todo") .Argument <NonNullGraphType <IdGraphType>, Guid>("id", "Todo id.") .ResolveAsync(async context => { var mediator = (IMediator)accessor.HttpContext.RequestServices.GetService(typeof(IMediator)); var id = context.GetArgument <Guid>("id"); var todos = await mediator.Send(new GetTodosListRequest { SpecifiedIds = new [] { (Id)id } }); var todo = todos.Single(); if (todo.Value == null) { context.Errors.Add(new ExecutionError("Not found") { Code = "NotFound" }); } return(todo); }); Field <ListGraphType <UserType>, IDictionary <Id, User> >() .Name("users") .ResolveAsync(async context => { var mediator = (IMediator)accessor.HttpContext.RequestServices.GetService(typeof(IMediator)); var status = context.GetArgument <TodoStatus?>("status"); return(await mediator.Send(new GetUsersRequest())); }); }