protected ExecutorBuilder CreateProviderTester <TRuntimeType>(
            FilterInputType <TRuntimeType> type,
            FilterConvention?convention = null)
        {
            convention ??= new FilterConvention(x => x
                                                .AddDefaults()
                                                .AddSpatialOperations()
                                                .BindSpatialTypes()
                                                .Provider(
                                                    new QueryableFilterProvider(
                                                        p => p.AddSpatialHandlers().AddDefaultFieldHandlers())));

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <IFilterConvention>(convention)
                                     .TryAddTypeInterceptor <FilterTypeInterceptor>()
                                     .AddQueryType(c => c
                                                   .Name("Query")
                                                   .Field("foo")
                                                   .Type <StringType>()
                                                   .Resolve("bar"))
                                     .AddType(type);

            builder.Create();

            return(new ExecutorBuilder(type));
        }
Example #2
0
        protected IRequestExecutor CreateSchema <TEntity, T>(
            TEntity[] entities,
            FilterConvention?convention = null,
            bool withPaging             = false)
            where TEntity : class
            where T : FilterInputType <TEntity>
        {
            convention ??= new FilterConvention(x => x.AddDefaults().BindRuntimeType <TEntity, T>());

            Func <IResolverContext, IEnumerable <TEntity> >?resolver = BuildResolver(entities);

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <IFilterConvention>(convention)
                                     .AddFiltering()
                                     .AddQueryType(
                c =>
            {
                IObjectFieldDescriptor field = c
                                               .Name("Query")
                                               .Field("root")
                                               .Resolver(resolver);

                if (withPaging)
                {
                    field.UsePaging <ObjectType <TEntity> >();
                }

                field.UseFiltering <T>();
            });

            ISchema schema = builder.Create();

            return(schema.MakeExecutable());
        }
        protected IRequestExecutor CreateSchema <TEntity, T>(
            TEntity[] entities,
            FilterConvention?convention = null,
            bool withPaging             = false)
            where TEntity : class
            where T : FilterInputType <TEntity>
        {
            convention ??= new FilterConvention(x => x.AddDefaults().BindRuntimeType <TEntity, T>());

            Func <IResolverContext, IEnumerable <TEntity> >?resolver = BuildResolver(entities);

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <IFilterConvention>(convention)
                                     .AddFiltering()
                                     .AddQueryType(
                c =>
            {
                ApplyConfigurationToField <TEntity, T>(
                    c.Name("Query").Field("root").Resolver(resolver),
                    withPaging);

                ApplyConfigurationToField <TEntity, T>(
                    c.Name("Query")
                    .Field("rootExecutable")
                    .Resolver(
                        ctx => resolver(ctx).AsExecutable()),
                    withPaging);
            });

            ISchema schema = builder.Create();

            return(new ServiceCollection()
                   .Configure <RequestExecutorSetup>(
                       Schema.DefaultName,
                       o => o.Schema = schema)
                   .AddGraphQL()
                   .UseRequest(
                       next => async context =>
            {
                await next(context);
                if (context.Result is IReadOnlyQueryResult result &&
                    context.ContextData.TryGetValue("sql", out var queryString))
                {
                    context.Result =
                        QueryResultBuilder
                        .FromResult(result)
                        .SetContextData("sql", queryString)
                        .Create();
                }
            })
                   .UseDefaultPipeline()
                   .Services
                   .BuildServiceProvider()
                   .GetRequiredService <IRequestExecutorResolver>()
                   .GetRequestExecutorAsync()
                   .Result);
        }
        protected async Task <IRequestExecutor> CreateSchemaAsync <TEntity, T>(
            TEntity[] entities,
            FilterConvention?convention = null)
            where TEntity : class
            where T : FilterInputType <TEntity>
        {
            Func <IResolverContext, IEnumerable <TEntity> > resolver =
                await BuildResolverAsync(entities);

            return(await new ServiceCollection()
                   .AddGraphQL()
                   .AddFiltering()
                   .AddSpatialTypes()
                   .AddSpatialFiltering()
                   .AddQueryType(
                       c => c
                       .Name("Query")
                       .Field("root")
                       .Resolve(resolver)
                       .Use(
                           next => async context =>
            {
                await next(context);

                if (context.Result is IQueryable <TEntity> queryable)
                {
                    try
                    {
                        context.ContextData["sql"] = queryable.ToQueryString();
                    }
                    catch (Exception)
                    {
                        context.ContextData["sql"] =
                            "EF Core 3.1 does not support ToQueryString officially";
                    }
                }
            })
                       .UseFiltering <T>())
                   .UseRequest(
                       next => async context =>
            {
                await next(context);
                if (context.Result is IReadOnlyQueryResult result &&
                    context.ContextData.TryGetValue("sql", out var queryString))
                {
                    context.Result =
                        QueryResultBuilder
                        .FromResult(result)
                        .SetContextData("sql", queryString)
                        .Create();
                }
            })
                   .UseDefaultPipeline()
                   .BuildRequestExecutorAsync());
        }
        protected ExecutorBuilder CreateProviderTester <TRuntimeType>(
            FilterInputType <TRuntimeType> type,
            FilterConvention?convention = null)
        {
            convention ??=
            new FilterConvention(
                x => x.AddDefaults().BindRuntimeType(typeof(TRuntimeType), type.GetType()));

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <IFilterConvention>(convention)
                                     .AddTypeInterceptor <FilterTypeInterceptor>()
                                     .AddQueryType(
                c =>
                c.Name("Query")
                .Field("foo")
                .Type <StringType>()
                .Resolver("bar"))
                                     .AddType(type);

            builder.Create();

            return(new ExecutorBuilder(type));
        }
        protected IRequestExecutor CreateSchema <TEntity, T>(
            TEntity[] entities,
            FilterConvention?convention = null)
            where TEntity : class
            where T : FilterInputType <TEntity>
        {
            convention ??= new FilterConvention(x => x.AddDefaults().BindRuntimeType <TEntity, T>());

            Func <IResolverContext, IEnumerable <TEntity> >?resolver = BuildResolver(entities);

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <IFilterConvention>(convention)
                                     .AddFiltering()
                                     .AddQueryType(
                c => c
                .Name("Query")
                .Field("root")
                .Resolver(resolver)
                .Use(next => async context =>
            {
                await next(context);

                if (context.Result is IQueryable <TEntity> queryable)
                {
                    try
                    {
                        context.ContextData["sql"] = queryable.ToQueryString();
                    }
                    catch (Exception)
                    {
                        context.ContextData["sql"] =
                            "EF Core 3.1 does not support ToQuerString offically";
                    }
                }
            })
                .UseFiltering <T>());

            ISchema?schema = builder.Create();

            return(new ServiceCollection()
                   .Configure <RequestExecutorFactoryOptions>(Schema.DefaultName, o => o.Schema = schema)
                   .AddGraphQL()
                   .UseRequest(next => async context =>
            {
                await next(context);
                if (context.Result is IReadOnlyQueryResult result &&
                    context.ContextData.TryGetValue("sql", out var queryString))
                {
                    context.Result =
                        QueryResultBuilder
                        .FromResult(result)
                        .SetContextData("sql", queryString)
                        .Create();
                }
            })
                   .UseDefaultPipeline()
                   .Services
                   .BuildServiceProvider()
                   .GetRequiredService <IRequestExecutorResolver>()
                   .GetRequestExecutorAsync()
                   .Result);
        }