public async Task <IEnumerable <TSource> > ExecuteAsync(string queryName, IEnumerable <QueryStep> querySteps, IGraphRequestContext graphRequestContext)
        {
            var ctx = new QueryExecutionContext();

            foreach (var queryStep in querySteps)
            {
                queryStep.Started = DateTime.UtcNow;

                if (queryStep.Mapper != null)
                {
                    // We may have to make several queries.
                    var nextQueryResults = new List <object>();

                    var queryValues = queryStep.Mapper(ctx);

                    if (queryValues.Count > 0)
                    {
                        var first = queryStep.QueryParameters.First();

                        first.ContextValue = new ContextValue {
                            Values = queryValues, Comparison = Comparisons.Equal
                        };

                        try
                        {
                            var results = await QueryAsync(queryName, queryStep, graphRequestContext);

                            nextQueryResults.AddRange(results);
                        }
                        catch (Exception e)
                        {
                            _logger.LogError(e, "An error has occured while executing query on repository.");
                            throw;
                        }
                    }
                    else
                    {
                        _logger.LogWarning($"No values detected for queryStep @ {queryName}");
                    }

                    ctx.SetQueryResult(nextQueryResults);
                }
                else
                {
                    ctx.SetQueryResult(await QueryAsync(queryName, queryStep, graphRequestContext));
                }

                queryStep.ContextAction?.Invoke(ctx);

                queryStep.Ended = DateTime.UtcNow;
            }

            return(ctx.GetResults <TSource>());
        }
        private async Task <List <object> > QueryAsync(string queryName, QueryStep queryStep, IGraphRequestContext graphRequestContext,
                                                       QueryExecutionContext queryExecutionContext,
                                                       List <ConnectionEdgeDestinationFilter> connectionEdgeDestinationFilters)
        {
            var results = (await _graphQlRepositoryProvider.QueryAsync(queryName, queryStep, graphRequestContext)).ToList();

            if (!queryStep.SkipConnectionEdgeCheck && results.Count > 0)
            {
                await _connectionEdgeHandler.QueryAsync(results, queryStep, graphRequestContext, queryExecutionContext,
                                                        connectionEdgeDestinationFilters);
            }
            return(results);
        }
        public async Task <IEnumerable <TSource> > ExecuteAsync(string queryName, IEnumerable <QueryStep> querySteps, IGraphRequestContext graphRequestContext,
                                                                List <ConnectionEdgeDestinationFilter> connectionEdgeDestinationFilters)
        {
            var ctx = new QueryExecutionContext(graphRequestContext);

            foreach (var queryStep in querySteps)
            {
                queryStep.Started = DateTime.UtcNow;

                // If there are any runtime mappers, run them first.
                queryStep.QueryParameters.ForEach(qp =>
                {
                    if (qp.Mapper != null)
                    {
                        var values = qp.Mapper(new MapperQueryExecutionContext(ctx, queryStep));
                        if (values.Count > 0)
                        {
                            qp.ContextValue = new ContextValue
                            {
                                Values     = values,
                                Comparison = Comparisons.Equal
                            };
                        }
                    }
                });

                var nextQueryResults = new List <object>();

                if (queryStep.StepMapper != null)
                {
                    var queryValues = queryStep.StepMapper(new MapperQueryExecutionContext(ctx, queryStep));

                    if (queryValues.Count > 0)
                    {
                        var first = queryStep.QueryParameters.FirstOrDefault(x => x.Rule != null && x.Rule.PopulateWithQueryValues);
                        if (first == null)
                        {
                            first = queryStep.QueryParameters.First();
                        }

                        first.ContextValue = new ContextValue
                        {
                            Values       = queryValues,
                            Comparison   = Comparisons.Equal,
                            SelectValues = first.ContextValue != null && first.ContextValue.SelectValues != null ? first.ContextValue.SelectValues : null
                        };

                        try
                        {
                            nextQueryResults.AddRange(await QueryAsync(queryName, queryStep, graphRequestContext, ctx,
                                                                       connectionEdgeDestinationFilters));
                        }
                        catch (Exception e)
                        {
                            _logger.LogError(e, "An error has occured while executing query on repository.");
                            throw;
                        }
                    }
                    else
                    {
                        _logger.LogWarning($"No values detected for queryStep @ {queryName}");
                    }
                }
                else
                {
                    if (!IsInvalidForNextQuery(queryStep))
                    {
                        nextQueryResults.AddRange(await QueryAsync(queryName, queryStep, graphRequestContext, ctx,
                                                                   connectionEdgeDestinationFilters));
                    }
                    else
                    {
                        _logger.LogWarning($"Invalid values detected for queryStep @ {queryName}");
                    }
                }

                ctx.SetQueryResult(nextQueryResults);

                queryStep.ContextAction?.Invoke(ctx);

                queryStep.Ended = DateTime.UtcNow;
            }

            return(ctx.GetResults <TSource>());
        }