Exemplo n.º 1
0
 /// <summary>
 /// Chains post-processing to a pending data loader operation
 /// </summary>
 /// <typeparam name="T">The type of the data loader return value</typeparam>
 /// <typeparam name="TResult">The type of the result</typeparam>
 /// <param name="parent">The pending data loader operation</param>
 /// <param name="chainedDelegate">The delegate to execute once the data loader finishes loading</param>
 /// <returns>A pending data loader operation that can return a value once the data loader and the chained delegate finish</returns>
 public static IDataLoaderResult <TResult> Then <T, TResult>(this IDataLoaderResult <T> parent, Func <T, Task <TResult> > chainedDelegate)
 {
     return(new SimpleDataLoader <TResult>(async(cancellationToken) =>
     {
         var result = await parent.GetResultAsync(cancellationToken).ConfigureAwait(false);
         return await chainedDelegate(result).ConfigureAwait(false);
     }));
 }
Exemplo n.º 2
0
        public IDataLoaderResult <IEnumerable <T> > GetLoader(IResolveFieldContext context, string param)
        {
            Type    graphRepositoryType = typeof(IGraphRepository <>).MakeGenericType(new Type[] { _dataType });
            dynamic service             = _httpContextAccessor.HttpContext.RequestServices.GetService(graphRepositoryType);
            var     first = context.GetArgument <int?>("first");
            //Task<IEnumerable<T>> res = null;
            var metaTable = _dbMetadata.GetTableMetadatas().FirstOrDefault(x => x.Type == context.Source.GetType());
            // Console.WriteLine($"--------------------------- main type {context.Source.GetType()} PK {metaTable.NamePK} _dataType {_dataType.Name}----------------------- ");
            var valueField = context.Source.GetType().GetProperty(metaTable.NamePK).GetValue(context.Source, null);
            IDataLoaderResult <IEnumerable <T> > res = null;

            try
            {
                //IEquatable
                if (context.Source is IBaseModel && valueField is int @int)
                {
                    var loader = _accessor.Context.GetOrAddCollectionBatchLoader <int, T>($"GetItemsByIds_{typeof(T).Name}",
                                                                                          (ids) => service.GetItemsByIds(ids, context, param));
                    res = loader.LoadAsync(@int);
                }
                else if (context.Source is IBaseModel && valueField is string @string)
                {
                    var loader = _accessor.Context.GetOrAddCollectionBatchLoader <string, T>($"GetItemsByIds_{typeof(T).Name}",
                                                                                             (ids) => service.GetItemsByIds(ids, context, param, isString: true));
                    res = loader.LoadAsync(@string);
                }
                else if (context.Source is IBaseModel && valueField is Guid @guid)
                {
                    var loader = _accessor.Context.GetOrAddCollectionBatchLoader <Guid, T>($"GetItemsByIds_{typeof(T).Name}",
                                                                                           (ids) => service.GetItemsByIds(ids, context, param, isString: true));
                    res = loader.LoadAsync(@guid);
                }
                else if (context.Source is IdentityUser)
                {
                    var loader = _accessor.Context.GetOrAddCollectionBatchLoader <string, T>($"GetItemsByIds_{typeof(T).Name}",
                                                                                             (ids) => service.GetItemsByIds(ids, context, param, isString: true));
                    res = loader.LoadAsync((context.Source as IdentityUser).Id);
                }
                else if (context.Source is IdentityRole)
                {
                    var loader = _accessor.Context.GetOrAddCollectionBatchLoader <string, T>($"GetItemsByIds_{typeof(T).Name}",
                                                                                             (ids) => service.GetItemsByIds(ids, context, param, isString: true));
                    res = loader.LoadAsync((context.Source as IdentityRole).Id);
                }
                else
                {
                    var loader = _accessor.Context.GetOrAddCollectionBatchLoader <string, T>($"GetItemsByIds_{typeof(T).Name}",
                                                                                             (ids) => service.GetItemsByIds(ids, context, param, isString: true));
                    res = loader.LoadAsync(valueField.ToString());
                }

                if (first.HasValue && first.Value > 0)
                {
                    return(res);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return(res);
        }