コード例 #1
0
    FieldType BuildQueryField <TSource, TReturn>(
        Type?itemGraphType,
        string name,
        Func <ResolveEfFieldContext <TDbContext, TSource>, IQueryable <TReturn> >?resolve,
        IEnumerable <QueryArgument>?arguments,
        string?description)
        where TReturn : class
    {
        Guard.AgainstWhiteSpace(nameof(name), name);

        var hasId     = keyNames.ContainsKey(typeof(TReturn));
        var fieldType = new FieldType
        {
            Name        = name,
            Description = description,
            Type        = MakeListGraphType <TReturn>(itemGraphType),
            Arguments   = ArgumentAppender.GetQueryArguments(arguments, hasId, true),
        };

        if (resolve is not null)
        {
            fieldType.Resolver = new AsyncFieldResolver <TSource, IEnumerable <TReturn> >(
                async context =>
            {
                var fieldContext = BuildContext(context);
                var names        = GetKeyNames <TReturn>();
                var query        = resolve(fieldContext);
                if (disableTracking)
                {
                    query = query.AsNoTracking();
                }

                query = includeAppender.AddIncludes(query, context);
                query = query.ApplyGraphQlArguments(context, names, true);

                QueryLogger.Write(query);

                var list = await query
                           .ToListAsync(context.CancellationToken);
                return(await fieldContext.Filters.ApplyFilter(list, context.UserContext));
            });
        }

        return(fieldType);
    }
コード例 #2
0
    static async Task <Connection <TItem> > Range <TSource, TItem>(
        IQueryable <TItem> list,
        int skip,
        int take,
        int count,
        IResolveFieldContext <TSource> context,
        Filters filters,
        CancellationToken cancellation)
        where TItem : class
    {
        var page = list.Skip(skip).Take(take);

        QueryLogger.Write(page);
        IEnumerable <TItem> result = await page.ToListAsync(cancellation);

        result = await filters.ApplyFilter(result, context.UserContext);

        cancellation.ThrowIfCancellationRequested();
        return(Build(skip, take, count, result));
    }
コード例 #3
0
    FieldType BuildSingleField <TSource, TReturn>(
        string name,
        Func <ResolveEfFieldContext <TDbContext, TSource>, IQueryable <TReturn> > resolve,
        Func <ResolveEfFieldContext <TDbContext, TSource>, TReturn, Task>?mutate,
        IEnumerable <QueryArgument>?arguments,
        Type?graphType,
        bool nullable,
        string?description)
        where TReturn : class
    {
        Guard.AgainstWhiteSpace(nameof(name), name);

        graphType ??= GraphTypeFinder.FindGraphType <TReturn>(nullable);

        var hasId = keyNames.ContainsKey(typeof(TReturn));

        return(new()
        {
            Name = name,
            Type = graphType,
            Description = description,

            Arguments = ArgumentAppender.GetQueryArguments(arguments, hasId, false),

            Resolver = new AsyncFieldResolver <TSource, TReturn?>(
                async context =>
            {
                var efFieldContext = BuildContext(context);

                var names = GetKeyNames <TReturn>();

                var query = resolve(efFieldContext);
                if (disableTracking)
                {
                    query = query.AsNoTracking();
                }

                query = includeAppender.AddIncludes(query, context);
                query = query.ApplyGraphQlArguments(context, names, false);

                QueryLogger.Write(query);

                var single = await query.SingleOrDefaultAsync(context.CancellationToken);

                if (single is not null)
                {
                    if (await efFieldContext.Filters.ShouldInclude(context.UserContext, single))
                    {
                        if (mutate is not null)
                        {
                            await mutate.Invoke(efFieldContext, single);
                        }

                        return single;
                    }
                }

                if (nullable)
                {
                    return null;
                }

                throw new ExecutionError("Not found");
            })
        });
    }