Exemple #1
0
        public IRequestExecutor CreateSchema <TEntity>(
            TEntity[] entities,
            ProjectionProvider?provider           = null,
            Action <ModelBuilder>?onModelCreating = null,
            bool usePaging = false,
            ObjectType <TEntity>?objectType = null)
            where TEntity : class
        {
            provider ??= new QueryableProjectionProvider(x => x.AddDefaults());
            var convention = new ProjectionConvention(x => x.Provider(provider));

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

            ISchemaBuilder builder = SchemaBuilder.New();

            if (objectType is {})
Exemple #2
0
        public IRequestExecutor CreateSchema <TEntity>(
            TEntity[] entities,
            ProjectionProvider?provider           = null,
            Action <ModelBuilder>?onModelCreating = null,
            bool usePaging                    = false,
            bool useOffsetPaging              = false,
            INamedType?objectType             = null,
            Action <ISchemaBuilder>?configure = null)
            where TEntity : class
        {
            provider ??= new QueryableProjectionProvider(x => x.AddDefaults());
            var convention = new ProjectionConvention(x => x.Provider(provider));

            Func <IResolverContext, IQueryable <TEntity> > resolver =
                BuildResolver(onModelCreating, entities);

            ISchemaBuilder builder = SchemaBuilder.New();

            if (objectType is not null)
            {
                builder.AddType(objectType);
            }

            configure?.Invoke(builder);

            builder
            .AddConvention <IProjectionConvention>(convention)
            .AddProjections()
            .AddFiltering()
            .AddSorting()
            .AddQueryType(
                new ObjectType <StubObject <TEntity> >(
                    c =>
            {
                c.Name("Query");

                ApplyConfigurationToFieldDescriptor <TEntity>(
                    c.Field(x => x.Root).Resolve(resolver),
                    usePaging,
                    useOffsetPaging);

                ApplyConfigurationToFieldDescriptor <TEntity>(
                    c.Field("rootExecutable")
                    .Resolve(ctx => resolver(ctx).AsExecutable()),
                    usePaging,
                    useOffsetPaging);
            }));

            builder.ModifyOptions(o => o.ValidatePipelineOrder = false);

            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 object?queryString))
                {
                    context.Result =
                        QueryResultBuilder
                        .FromResult(result)
                        .SetContextData("sql", queryString)
                        .Create();
                }
            })
                   .UseDefaultPipeline()
                   .Services
                   .BuildServiceProvider()
                   .GetRequiredService <IRequestExecutorResolver>()
                   .GetRequestExecutorAsync()
                   .Result);
        }