private async Task <object> ResolveConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var query = context.GetCartQuery <SearchCartQuery>(); query.Skip = skip; query.Take = first ?? context.PageSize ?? 10; query.Sort = context.GetArgument <string>("sort"); query.Filter = context.GetArgument <string>("filter"); query.IncludeFields = context.SubFields.Values.GetAllNodesPaths().ToArray(); context.CopyArgumentsToUserContext(); var authorizationResult = await _authorizationService.AuthorizeAsync(context.GetCurrentPrincipal(), query, new CanAccessCartAuthorizationRequirement()); if (!authorizationResult.Succeeded) { throw new ExecutionError($"Access denied"); } var response = await mediator.Send(query); foreach (var cartAggregate in response.Results) { context.SetExpandedObjectGraph(cartAggregate); } return(new PagedConnection <CartAggregate>(response.Results, skip, Convert.ToInt32(context.After ?? 0.ToString()), response.TotalCount)); }
private async Task <object> ResolveConnectionAsync(IResolveConnectionContext <object> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var response = await _mediator.Send(new GetRecommendationsRequest { Skip = skip, Take = first ?? 20, Scenario = context.GetArgument <string>("scenario"), ItemId = context.GetArgument <string>("itemId"), UserId = context.GetArgument <string>("userId") }); var result = new Connection <ProductRecommendation>() { Edges = response.Products.Select((x, index) => new Edge <ProductRecommendation>() { Cursor = (skip + index).ToString(), Node = x, }) .ToList(), PageInfo = new PageInfo() { HasNextPage = response.TotalCount > (skip + first), HasPreviousPage = skip > 0, StartCursor = skip.ToString(), EndCursor = Math.Min(response.TotalCount, (int)(skip + first)).ToString() }, TotalCount = response.TotalCount, }; return(result); }
private static async Task <object> ResolveCategoriesConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var includeFields = context.SubFields.Values.GetAllNodesPaths().Select(x => x.Replace("items.", "")).ToArray(); var query = context.GetCatalogQuery <SearchCategoryQuery>(); var categoryIds = context.GetArgument <List <string> >("categoryIds"); query.IncludeFields = includeFields; if (categoryIds.IsNullOrEmpty()) { query.Skip = skip; query.Take = first ?? context.PageSize ?? 10; query.Query = context.GetArgument <string>("query"); query.Filter = context.GetArgument <string>("filter"); query.Facet = context.GetArgument <string>("facet"); query.Fuzzy = context.GetArgument <bool>("fuzzy"); query.FuzzyLevel = context.GetArgument <int?>("fuzzyLevel"); query.Sort = context.GetArgument <string>("sort"); } else { query.ObjectIds = categoryIds.ToArray(); query.Take = categoryIds.Count; } var response = await mediator.Send(query); return(new PagedConnection <ExpCategory>(response.Results, query.Skip, query.Take, response.TotalCount)); }
private static async Task <object> ResolveConnectionAsync(IMediator mediator, IResolveConnectionContext <DynamicProperty> context) { int.TryParse(context.After, out var skip); var query = context.GetDynamicPropertiesQuery <SearchDynamicPropertyDictionaryItemQuery>(); query.PropertyId = context.Source.Id; query.Skip = skip; query.Take = context.First ?? context.PageSize ?? 10; query.Sort = context.GetArgument <string>("sort"); query.Filter = context.GetArgument <string>("filter"); context.CopyArgumentsToUserContext(); var response = await mediator.Send(query); return(new PagedConnection <DynamicPropertyDictionaryItem>(response.Results, query.Skip, query.Take, response.TotalCount)); }
private static async Task <object> ResolvePropertiesConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var query = new SearchPropertiesQuery { Skip = skip, Take = first ?? context.PageSize ?? 10, CatalogId = (string)context.UserContext["catalog"], Types = context.GetArgument <object[]>("types"), Filter = context.GetArgument <string>("filter") }; var response = await mediator.Send(query); return(new PagedConnection <Property>(response.Result.Results, query.Skip, query.Take, response.Result.TotalCount)); }
private async Task <object> ResolveGenericRtEntitiesQuery(IResolveConnectionContext <object> arg) { Logger.Debug("GraphQL query handling for generic runtime entity started"); var graphQlUserContext = (GraphQLUserContext)arg.UserContext; var ckId = arg.GetArgument <string>(Statics.CkIdArg); using var session = await graphQlUserContext.TenantContext.Repository.StartSessionAsync(); session.StartTransaction(); int?offset = arg.GetOffset(); DataQueryOperation dataQueryOperation = arg.GetDataQueryOperation(); arg.TryGetArgument(Statics.RtIdArg, out OspObjectId? key); arg.TryGetArgument(Statics.RtIdsArg, new List <ObjectId>(), out IEnumerable <ObjectId> keys); var keysList = keys.ToList(); if (keysList.Any()) { var resultSetIds = await graphQlUserContext.TenantContext.Repository.GetRtEntitiesByIdAsync(session, ckId, keysList, dataQueryOperation, offset, arg.First); Logger.Debug("GraphQL query handling returning data by keys"); return(ConnectionUtils.ToConnection(resultSetIds.Result.Select(RtEntityDtoType.CreateRtEntityDto), arg, resultSetIds.TotalCount > 0 ? offset.GetValueOrDefault(0) : 0, (int)resultSetIds.TotalCount)); } if (key.HasValue) { var result = await graphQlUserContext.TenantContext.Repository.GetRtEntityAsync(session, new RtEntityId(ckId, key.Value)); var resultList = new List <RtEntityDto>(); if (result != null) { resultList.Add(RtEntityDtoType.CreateRtEntityDto(result)); } Logger.Debug("GraphQL query handling returning data by key"); return(ConnectionUtils.ToConnection(resultList, arg)); } var resultSet = await graphQlUserContext.TenantContext.Repository.GetRtEntitiesByTypeAsync(session, ckId, dataQueryOperation, offset, arg.First); await session.CommitTransactionAsync(); Logger.Debug("GraphQL query handling returning data"); return(ConnectionUtils.ToConnection(resultSet.Result.Select(RtEntityDtoType.CreateRtEntityDto), arg, resultSet.TotalCount > 0 ? offset.GetValueOrDefault(0) : 0, (int)resultSet.TotalCount)); }
private static async Task <object> ResolveConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var includeFields = context.SubFields.Values.GetAllNodesPaths().Select(x => x.TrimStart("items.")).ToArray(); var request = new SearchProductRequest { Skip = skip, Take = first ?? context.PageSize ?? 10, Query = context.GetArgument <string>("query"), Filter = context.GetArgument <string>("filter"), Facet = context.GetArgument <string>("facet"), Fuzzy = context.GetArgument <bool>("fuzzy"), Sort = context.GetArgument <string>("sort"), IncludeFields = includeFields.ToArray(), }; var response = await mediator.Send(request); return(new ProductsConnection <ExpProduct>() { Edges = response.Results .Select((x, index) => new Edge <ExpProduct>() { Cursor = (skip + index).ToString(), Node = x, }) .ToList(), PageInfo = new PageInfo() { HasNextPage = response.TotalCount > (skip + first), HasPreviousPage = skip > 0, StartCursor = skip.ToString(), EndCursor = Math.Min(response.TotalCount, (int)(skip + first)).ToString() }, TotalCount = response.TotalCount, Facets = response.Facets }); }
private async Task <object> ResolveOrdersConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var request = new SearchOrderQuery { Skip = skip, Take = first ?? context.PageSize ?? 10, Filter = context.GetArgument <string>("filter"), Sort = context.GetArgument <string>("sort"), CultureName = context.GetArgument <string>(nameof(Currency.CultureName).ToCamelCase()), CustomerId = context.GetArgumentOrValue <string>("userId") }; context.CopyArgumentsToUserContext(); var allCurrencies = await _currencyService.GetAllCurrenciesAsync(); //Store all currencies in the user context for future resolve in the schema types context.SetCurrencies(allCurrencies, request.CultureName); var authorizationResult = await _authorizationService.AuthorizeAsync(context.GetCurrentPrincipal(), request, new CanAccessOrderAuthorizationRequirement()); if (!authorizationResult.Succeeded) { throw new ExecutionError($"Access denied"); } var response = await mediator.Send(request); foreach (var customerOrderAggregate in response.Results) { context.SetExpandedObjectGraph(customerOrderAggregate); } return(new PagedConnection <CustomerOrderAggregate>(response.Results, skip, Convert.ToInt32(context.After ?? 0.ToString()), response.TotalCount)); }
private static async Task <object> ResolveConnectionAsync(IMediator madiator, IResolveConnectionContext <ExpProduct> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var criteria = new ProductAssociationSearchCriteria { Skip = skip, Take = first ?? context.PageSize ?? 10, // We control the resulting product structure by passing IncludeFields, and to prevent forced reduction of already loaded fields, you need to pass ItemResponseGroup.Full // in any case, the object will be loaded from the index, and the response group will not affect overall performance ResponseGroup = ItemResponseGroup.Full.ToString(), Keyword = context.GetArgument <string>("query"), Group = context.GetArgument <string>("group"), ObjectIds = new[] { context.Source.CatalogProduct.Id } }; var response = await madiator.Send(new SearchProductAssociationsRequest { Criteria = criteria }); return(new Connection <ProductAssociation>() { Edges = response.Result.Results .Select((x, index) => new Edge <ProductAssociation>() { Cursor = (skip + index).ToString(), Node = x, }) .ToList(), PageInfo = new PageInfo() { HasNextPage = response.Result.TotalCount > (skip + first), HasPreviousPage = skip > 0, StartCursor = skip.ToString(), EndCursor = Math.Min(response.Result.TotalCount, (int)(skip + first)).ToString() }, TotalCount = response.Result.TotalCount, }); }
private async Task <object> ResolveConnectionAsync(IMediator mediator, IResolveConnectionContext <OrganizationAggregate> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var response = await mediator.Send(new SearchOrganizationMembersQuery { OrganizationId = context.Source.Organization.Id, Take = first ?? 20, Skip = skip, SearchPhrase = context.GetArgument <string>("searchPhrase") }); return(new PagedConnection <ContactAggregate>(response.Results.Select(x => new ContactAggregate(x as Contact)), skip, Convert.ToInt32(context.After ?? 0.ToString()), response.TotalCount)); }
private static async Task <object> ResolveProductsConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context) { var first = context.First; var skip = Convert.ToInt32(context.After ?? 0.ToString()); var includeFields = context.SubFields.Values.GetAllNodesPaths(); //TODO: Need to be able get entire query from context and read all arguments to the query properties var query = context.GetCatalogQuery <SearchProductQuery>(); query.IncludeFields = includeFields; var productIds = context.GetArgument <List <string> >("productIds"); if (productIds.IsNullOrEmpty()) { query.Skip = skip; query.Take = first ?? context.PageSize ?? 10; query.Query = context.GetArgument <string>("query"); query.Filter = context.GetArgument <string>("filter"); query.Facet = context.GetArgument <string>("facet"); query.Fuzzy = context.GetArgument <bool>("fuzzy"); query.FuzzyLevel = context.GetArgument <int?>("fuzzyLevel"); query.Sort = context.GetArgument <string>("sort"); } else { query.ObjectIds = productIds.ToArray(); query.Take = productIds.Count; } var response = await mediator.Send(query); var currencyCode = context.GetArgumentOrValue <string>("currencyCode"); if (string.IsNullOrWhiteSpace(currencyCode)) { context.SetCurrency(response.Currency); } var result = new ProductsConnection <ExpProduct>(response.Results, skip, Convert.ToInt32(context.After ?? 0.ToString()), response.TotalCount) { Facets = response.Facets }; return(result); }
public IResult <IQueryable <Brewery> > ResolveNearbyBreweries(IResolveConnectionContext context) { var latitude = GetLatitude(context); var longitude = GetLongitude(context); if (latitude.HasValue && longitude.HasValue) { var id = GetBreweryId(context); var within = context.GetArgument("within", 25); return(_breweryConductor .FindAllByLocation( latitude.Value, longitude.Value, within) .Filter(b => id == null || b.Id != id.Value) .Include( nameof(Brewery.BreweryTags), context.ContainsField("tag_list"))); } return(new Result <IQueryable <Brewery> >(Enumerable.Empty <Brewery>().AsQueryable())); }
private object ResolveAttributes(IResolveConnectionContext <RtEntityDto> ctx) { var graphQlContext = (GraphQLUserContext)ctx.UserContext; var filterAttributeNames = ctx.GetArgument <IEnumerable <string> >(Statics.AttributeNamesFilterArg); var entityCacheItem = graphQlContext.TenantContext.CkCache.GetEntityCacheItem(ctx.Source.CkId); IEnumerable <AttributeCacheItem> resultList; if (filterAttributeNames == null) { resultList = entityCacheItem.Attributes.Values; } else { resultList = entityCacheItem.Attributes.Values.Where(a => filterAttributeNames.Contains(a.AttributeName.ToCamelCase())); } return(ConnectionUtils.ToConnection(resultList.Select(item => CreateRtEntityAttributeDto((RtEntity)ctx.Source.UserContext, item)), ctx)); }