Пример #1
0
        private async ValueTask <object> ExecuteCoreAsync(ResolverContext resolverContext)
        {
            Validate(resolverContext);
            object container = await resolverContext.Field.Resolver.ResolveAsync(resolverContext);

            if (resolverContext.Selection is IFieldSelection fieldSelection1)
            {
                if (fieldSelection1.IncludeAllFields())
                {
                    if (resolverContext.Field.GraphType.Fields.Any())
                    {
                        _log4ReturnRoot(_logger, DateTimeOffset.Now, resolverContext.GraphContext.OperationName, container.GetType().AssemblyQualifiedName, null);
                    }
                    return(container);
                }

                if (fieldSelection1.TryGetQueryResultType(out var queryResultType))
                {
                    _log4QueryResult(_logger, DateTimeOffset.Now, resolverContext.GraphContext.OperationName, container.GetType().AssemblyQualifiedName, null);
                    return(Mapper.Map(container, container.GetType(), queryResultType));
                }
            }

            var subFields = new List <IFieldSelection>();

            subFields.AddRange(resolverContext.Selection.SelectionSet.OfType <IFieldSelection>());

            if (resolverContext.Field.GraphType.IsEnumerable)
            {
                var list = new List <object>();
                foreach (var element in (IEnumerable)container)
                {
                    List <IFieldSelection> elementSubFields = null;
                    if (TryGetFragment(element, resolverContext.Selection, out var fragment1))
                    {
                        elementSubFields = new List <IFieldSelection>(subFields);
                        elementSubFields.AddRange(fragment1.SelectionSet.OfType <IFieldSelection>());
                    }
                    var allFields = elementSubFields ?? subFields;
                    if (!allFields.Any())
                    {
                        list.Add(element);
                        continue;
                    }
                    var dictionary = new Dictionary <string, object>();
                    foreach (var fieldSelection in allFields)
                    {
                        resolverContext.Field.GraphType.Fields.TryGetGetField(element, fieldSelection.Name, out var subField);
                        var newResolverContext = new ResolverContext(resolverContext.GraphContext, subField, fieldSelection, element);
                        if (newResolverContext.Skip())
                        {
                            continue;
                        }
                        var element1 = await ExecuteCoreAsync(newResolverContext);

                        dictionary[fieldSelection.Alias ?? fieldSelection.Name] = element1;
                    }
                    list.Add(dictionary);
                }
                return(list);
            }

            if (TryGetFragment(resolverContext, resolverContext.Selection, out var fragment2))
            {
                subFields.AddRange(fragment2.SelectionSet.OfType <IFieldSelection>());
            }
            if (!subFields.Any())
            {
                return(container);
            }
            var node = new Dictionary <string, object>();

            foreach (IFieldSelection subSelection in subFields)
            {
                resolverContext.Field.GraphType.Fields.TryGetGetField(container, subSelection.Name, out var subField);
                var newResolverContext = new ResolverContext(resolverContext.GraphContext, subField, subSelection, container);
                if (newResolverContext.Skip())
                {
                    continue;
                }
                var element = await ExecuteCoreAsync(newResolverContext);

                node[subSelection.Alias ?? subSelection.Name] = element;
            }
            return(node);
        }