Пример #1
0
    protected IRequestExecutor CreateSchema <TEntity, T>(
        TEntity[] entities,
        FilterConvention?convention       = null,
        bool withPaging                   = false,
        Action <ISchemaBuilder>?configure = 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 =>
        {
            IObjectFieldDescriptor field = c
                                           .Name("Query")
                                           .Field("root")
                                           .Resolve(resolver);

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

            field.UseFiltering <T>();

            field = c
                    .Name("Query")
                    .Field("rootExecutable")
                    .Resolve(ctx => resolver(ctx).AsExecutable());

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

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

        configure?.Invoke(builder);

        ISchema schema = builder.Create();

        return(schema.MakeExecutable());
    }
        private void ApplyConfigurationToField <TEntity, TType>(
            IObjectFieldDescriptor field,
            bool withPaging)
            where TEntity : class
            where TType : FilterInputType <TEntity>
        {
            field.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";
                    }
                }
            });

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

            field.UseFiltering <TType>();
        }
 /// <summary>
 /// Adds cursor pagination support to the field. Rewrites the type to a connection type and
 /// registers the mongo pagination handler
 /// </summary>
 /// <param name="descriptor">The descriptor of the field</param>
 /// <param name="type">
 /// The schema type of the entity. Not a connection type
 /// </param>
 /// <param name="entityType">The type of the entity</param>
 /// <param name="options">The options for pagination</param>
 /// <returns>The <paramref name="descriptor"/></returns>
 public static IObjectFieldDescriptor UseMongoDbPaging(
     this IObjectFieldDescriptor descriptor,
     Type?type             = null,
     Type?entityType       = null,
     PagingOptions options = default) =>
 descriptor.UsePaging(
     type,
     entityType,
     (services, sourceType) => services.GetService <MongoDbCursorPagingProvider>() ??
     new MongoDbCursorPagingProvider(),
     options);
    private static void ApplyConfigurationToFieldDescriptor <TEntity>(
        IObjectFieldDescriptor descriptor,
        Type?type,
        bool usePaging       = false,
        bool useOffsetPaging = false)
    {
        if (usePaging)
        {
            descriptor.UsePaging(nodeType: type ?? typeof(ObjectType <TEntity>));
        }

        if (useOffsetPaging)
        {
            descriptor.UseOffsetPaging(type ?? typeof(ObjectType <TEntity>));
        }

        descriptor
        .Use(
            next => async context =>
        {
            await next(context);

            if (context.Result is IQueryable <TEntity> queryable)
            {
                try
                {
                    context.ContextData["sql"] = queryable.ToQueryString();
                }
                catch (Exception ex)
                {
                    context.ContextData["sql"] = ex.Message;
                }

                context.Result = await queryable.ToListAsync();
            }

            if (context.Result is IExecutable executable)
            {
                try
                {
                    context.ContextData["sql"] = executable.Print();
                }
                catch (Exception ex)
                {
                    context.ContextData["sql"] = ex.Message;
                }
            }
        })
        .UseProjection()
        .UseFiltering()
        .UseSorting();
    }
Пример #5
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)
                                               .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";
                        }
                    }
                });

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

                field.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);
        }