FieldType BuildSingleField <TSource, TReturn>(
            string name,
            Func <ResolveFieldContext <TSource>, IQueryable <TReturn> > resolve,
            IEnumerable <QueryArgument> arguments,
            Type graphType)
            where TReturn : class
        {
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNull(nameof(resolve), resolve);
            return(new FieldType
            {
                Name = name,
                Type = graphType,
                Arguments = ArgumentAppender.GetQueryArguments(arguments),
                Resolver = new FuncFieldResolver <TSource, Task <TReturn> >(async context =>
                {
                    var returnTypes = resolve(context);
                    var withIncludes = includeAppender.AddIncludes(returnTypes, context);
                    var withArguments = withIncludes.ApplyGraphQlArguments(context);

                    var single = await withArguments.SingleOrDefaultAsync(context.CancellationToken).ConfigureAwait(false);
                    if (GlobalFilters.ShouldInclude(context.UserContext, single))
                    {
                        return single;
                    }

                    return null;
                })
            });
        }
Exemple #2
0
        ConnectionBuilder <TGraph, TSource> BuildListConnectionField <TSource, TGraph, TReturn>(
            string name,
            Func <ResolveFieldContext <TSource>, IEnumerable <TReturn> > resolve,
            IEnumerable <string> includeName,
            int pageSize)
            where TGraph : ObjectGraphType <TReturn>, IGraphType
            where TReturn : class
        {
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNull(nameof(resolve), resolve);
            Guard.AgainstNegative(nameof(pageSize), pageSize);
            var builder = ConnectionBuilder.Create <TGraph, TSource>();

            builder.PageSize(pageSize);
            builder.Name(name);
            IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeName);
            builder.Resolve(context =>
            {
                var enumerable = resolve(context);
                enumerable     = enumerable.ApplyGraphQlArguments(context);
                enumerable     = GlobalFilters.ApplyFilter(enumerable, context.UserContext);
                var page       = enumerable.ToList();

                return(ConnectionConverter.ApplyConnectionContext(
                           page,
                           context.First,
                           context.After,
                           context.Last,
                           context.Before));
            });
            return(builder);
        }
 FieldType BuildNavigationField <TSource, TReturn>(
     string name,
     Func <ResolveFieldContext <TSource>, TReturn> resolve,
     IEnumerable <string> includeNames,
     Type graphType,
     IEnumerable <QueryArgument> arguments)
     where TReturn : class
 {
     Guard.AgainstNullWhiteSpace(nameof(name), name);
     Guard.AgainstNull(nameof(graphType), graphType);
     Guard.AgainstNull(nameof(resolve), resolve);
     return(new FieldType
     {
         Name = name,
         Type = graphType,
         Arguments = ArgumentAppender.GetQueryArguments(arguments),
         Metadata = IncludeAppender.GetIncludeMetadata(name, includeNames),
         Resolver = new FuncFieldResolver <TSource, TReturn>(context =>
         {
             var result = resolve(context);
             if (GlobalFilters.ShouldInclude(context.UserContext, result))
             {
                 return result;
             }
             return null;
         })
     });
 }
Exemple #4
0
        public EfGraphQLService(IModel model, GlobalFilters filters)
        {
            Guard.AgainstNull(nameof(model), model);
            this.filters = filters;
            foreach (var entityType in model.GetEntityTypes())
            {
                var names = entityType.FindPrimaryKey().Properties.Select(x => x.Name).ToList();
                keyNames.Add(entityType.ClrType, names);
            }

            includeAppender = new IncludeAppender(NavigationReader.GetNavigationProperties(model));
        }
Exemple #5
0
 public static void RegisterInContainer <TDbContext>(
     IServiceCollection services,
     TDbContext dbContext,
     DbContextFromUserContext <TDbContext> dbContextFromUserContext,
     GlobalFilters filters = null)
     where TDbContext : DbContext
 #endregion
 {
     Guard.AgainstNull(nameof(services), services);
     Guard.AgainstNull(nameof(dbContext), dbContext);
     services.AddTransient(typeof(ConnectionType <>));
     services.AddTransient(typeof(EdgeType <>));
     services.AddSingleton <PageInfoType>();
     RegisterInContainer((type, instance) => { services.AddSingleton(type, instance); }, dbContext, dbContextFromUserContext, filters);
 }
Exemple #6
0
        public static void RegisterInContainer(Action <Type, object> register, IModel model, GlobalFilters filters = null)
        #endregion
        {
            Guard.AgainstNull(nameof(register), register);
            Guard.AgainstNull(nameof(model), model);
            Scalars.RegisterInContainer(register);
            ArgumentGraphs.RegisterInContainer(register);

            if (filters == null)
            {
                filters = new GlobalFilters();
            }

            var service = new EfGraphQLService(model, filters);

            register(typeof(IEfGraphQLService), service);
        }
        public EfGraphQLService(IModel model, GlobalFilters filters, DbContextFromUserContext <TDbContext> dbContextFromUserContext)
        {
            Guard.AgainstNull(nameof(model), model);
            Guard.AgainstNull(nameof(dbContextFromUserContext), dbContextFromUserContext);
            this.filters = filters;
            this.dbContextFromUserContext = dbContextFromUserContext;
            foreach (var entityType in model.GetEntityTypes())
            {
                var primaryKey = entityType.FindPrimaryKey();
                //This can happen for views
                if (primaryKey == null)
                {
                    continue;
                }

                var names = primaryKey.Properties.Select(x => x.Name).ToList();
                keyNames.Add(entityType.ClrType, names);
            }

            includeAppender = new IncludeAppender(NavigationReader.GetNavigationProperties(model));
        }
Exemple #8
0
        public EfGraphQLService(IModel model, GlobalFilters filters, Func <object, TDbContext> dbContextFromUserContext)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            this.filters = filters;
            this.dbContextFromUserContext = dbContextFromUserContext ?? throw new ArgumentNullException(nameof(dbContextFromUserContext));
            foreach (var entityType in model.GetEntityTypes())
            {
                var primaryKey = entityType.FindPrimaryKey();
                //This can happen for views
                if (primaryKey == null)
                {
                    continue;
                }

                var names = primaryKey.Properties.Select(x => x.Name).ToList();
                keyNames.Add(entityType.ClrType, names);
            }

            includeAppender = new IncludeAppender(NavigationReader.GetNavigationProperties(model));
        }
Exemple #9
0
        public static void RegisterInContainer <TDbContext>(
            Action <Type, object> register,
            TDbContext dbContext,
            DbContextFromUserContext <TDbContext> dbContextFromUserContext,
            GlobalFilters filters = null)
            where TDbContext : DbContext
        #endregion
        {
            Guard.AgainstNull(nameof(register), register);
            Guard.AgainstNull(nameof(dbContextFromUserContext), dbContextFromUserContext);
            Guard.AgainstNull(nameof(dbContext), dbContext);
            Scalars.RegisterInContainer(register);
            ArgumentGraphs.RegisterInContainer(register);

            if (filters == null)
            {
                filters = new GlobalFilters();
            }

            var service = new EfGraphQLService <TDbContext>(dbContext.Model, filters, dbContextFromUserContext);

            register(typeof(IEfGraphQLService <TDbContext>), service);
        }
 FieldType BuildNavigationField <TSource, TReturn>(
     string name,
     Func <ResolveFieldContext <TSource>, IEnumerable <TReturn> > resolve,
     IEnumerable <string> includeNames,
     Type listGraphType,
     IEnumerable <QueryArgument> arguments)
     where TReturn : class
 {
     Guard.AgainstNullWhiteSpace(nameof(name), name);
     Guard.AgainstNull(nameof(resolve), resolve);
     return(new FieldType
     {
         Name = name,
         Type = listGraphType,
         Arguments = ArgumentAppender.GetQueryArguments(arguments),
         Metadata = IncludeAppender.GetIncludeMetadata(name, includeNames),
         Resolver = new FuncFieldResolver <TSource, IEnumerable <TReturn> >(context =>
         {
             var result = resolve(context);
             result = result.ApplyGraphQlArguments(context);
             return GlobalFilters.ApplyFilter(result, context.UserContext);
         })
     });
 }
Exemple #11
0
        public static void RegisterInContainer(IServiceCollection services, IEnumerable <Assembly> typeConfigurationAssemblies, GlobalFilters filters = null)
        #endregion
        {
            Guard.AgainstNull(nameof(services), services);

            var builder = new DbContextOptionsBuilder();

            builder.UseSqlServer("fake");
            using (var context = new DbContext(builder.Options))
            {
                var modelBuilder = new ModelBuilder(ConventionSet.CreateConventionSet(context));
                foreach (var assembly in typeConfigurationAssemblies)
                {
                    modelBuilder.ApplyConfigurationsFromAssembly(assembly);
                }

                RegisterInContainer(services, modelBuilder.Model, filters);
            }
        }
Exemple #12
0
 public static void RegisterInContainer(IServiceCollection services, IModel model, GlobalFilters filters = null)
 #endregion
 {
     Guard.AgainstNull(nameof(services), services);
     Guard.AgainstNull(nameof(model), model);
     services.AddTransient(typeof(ConnectionType <>));
     services.AddTransient(typeof(EdgeType <>));
     services.AddSingleton <PageInfoType>();
     RegisterInContainer((type, instance) => { services.AddSingleton(type, instance); }, model, filters);
 }
 public EfGraphQLService(IModel model, GlobalFilters filters)
 {
     Guard.AgainstNull(nameof(model), model);
     this.filters    = filters;
     includeAppender = new IncludeAppender(NavigationReader.GetNavigationProperties(model));
 }