Example #1
0
        private static void MergeRequest(
            BufferedRequest bufferedRequest,
            MergeRequestRewriter rewriter,
            IDictionary <string, object?> variableValues,
            NameString requestPrefix)
        {
            MergeVariables(
                bufferedRequest.Request.VariableValues,
                variableValues,
                requestPrefix);

            bool isAutoGenerated =
                bufferedRequest.Request.ContextData?.ContainsKey(IsAutoGenerated) ?? false;

            bufferedRequest.Aliases = rewriter.AddQuery(
                bufferedRequest,
                requestPrefix,
                isAutoGenerated);
        }
Example #2
0
        public static IEnumerable <(IQueryRequest, IEnumerable <BufferedRequest>)> MergeRequests(
            IEnumerable <BufferedRequest> requests,
            ISet <string> requestVariableNames)
        {
            foreach (var group in requests.GroupBy(t => t.Operation.Operation))
            {
                var rewriter       = new MergeRequestRewriter(requestVariableNames);
                var variableValues = new Dictionary <string, object?>();

                var operationName = group
                                    .Select(r => r.Request.OperationName)
                                    .Where(n => n != null)
                                    .Distinct()
                                    .FirstOrDefault();

                if (operationName is not null)
                {
                    rewriter.SetOperationName(new NameNode(operationName));
                }

                var             i     = 0;
                BufferedRequest first = null !;
                foreach (BufferedRequest request in group)
                {
                    first = request;
                    MergeRequest(request, rewriter, variableValues, $"__{i++}_");
                }

                IQueryRequest batch =
                    QueryRequestBuilder.New()
                    .SetQuery(rewriter.Merge())
                    .SetOperation(operationName)
                    .SetVariableValues(variableValues)
                    .TrySetServices(first.Request.Services)
                    .Create();

                yield return(batch, group);
            }
        }