Beispiel #1
0
 public static ISchemaBuilder AddConvention <T>(
     this ISchemaBuilder builder, IConvention convention)
     where T : IConvention
 {
     builder.AddConvention(typeof(T), convention);
     return(builder);
 }
Beispiel #2
0
        public static ISchemaBuilder AddConvention(
            this ISchemaBuilder builder,
            Type convention,
            IConvention concreteConvention,
            string?scope = null)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (convention is null)
            {
                throw new ArgumentNullException(nameof(convention));
            }

            if (concreteConvention is null)
            {
                throw new ArgumentNullException(nameof(concreteConvention));
            }

            if (!typeof(IConvention).IsAssignableFrom(convention))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_Convention_NotSuppported,
                          nameof(convention));
            }

            return(builder.AddConvention(convention, (s) => concreteConvention, scope));
        }
Beispiel #3
0
        public static ISchemaBuilder AddConvention(
            this ISchemaBuilder builder,
            Type convention,
            Type concreteConvention)
        {
            if (concreteConvention == null)
            {
                throw new ArgumentNullException(nameof(concreteConvention));
            }
            if (convention == null)
            {
                throw new ArgumentNullException(nameof(convention));
            }

            if (!typeof(IConvention).IsAssignableFrom(convention))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_Convention_NotSuppported,
                          nameof(convention));
            }

            if (!typeof(IConvention).IsAssignableFrom(concreteConvention))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_Convention_NotSuppported,
                          nameof(convention));
            }

            builder.AddConvention(convention, (s) => (IConvention)s.GetService(concreteConvention));

            return(builder);
        }
Beispiel #4
0
        public static ISchemaBuilder AddSpatialFiltering(this ISchemaBuilder builder)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddConvention <IFilterConvention>(
                       new FilterConventionExtension(x => x.AddSpatialDefaults())));
        }
Beispiel #5
0
        public static ISchemaBuilder AddConvention <T>(
            this ISchemaBuilder builder,
            CreateConvention conventionFactory,
            string?scope = null)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddConvention(typeof(T), conventionFactory, scope));
        }
        public static ISchemaBuilder AddSpatialProjections(this ISchemaBuilder builder)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddConvention <IProjectionConvention>(
                       new ProjectionConventionExtension(
                           x => x.AddProviderExtension(
                               new ProjectionProviderExtension(y => y.AddSpatialHandlers())))));
        }
Beispiel #7
0
        public static ISchemaBuilder AddConvention <TConvetion, TConcreteConvention>(
            this ISchemaBuilder builder)
            where TConvetion : IConvention
            where TConcreteConvention : IConvention
        {
            builder.AddConvention(
                typeof(TConvetion),
                typeof(TConcreteConvention)
                );

            return(builder);
        }
Beispiel #8
0
        public static ISchemaBuilder AddConvention(
            this ISchemaBuilder builder,
            Type convention,
            Type concreteConvention,
            string?scope = null)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (convention is null)
            {
                throw new ArgumentNullException(nameof(convention));
            }

            if (concreteConvention is null)
            {
                throw new ArgumentNullException(nameof(concreteConvention));
            }

            if (!typeof(IConvention).IsAssignableFrom(convention))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_Convention_NotSuppported,
                          nameof(convention));
            }

            if (!typeof(IConvention).IsAssignableFrom(concreteConvention))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_Convention_NotSuppported,
                          nameof(convention));
            }

            return(builder.AddConvention(
                       convention,
                       s =>
            {
                if (s.TryGetOrCreateService <IConvention>(
                        concreteConvention,
                        out IConvention convention))
                {
                    return convention;
                }

                throw Convention_UnableToCreateConvention(concreteConvention);
            },
                       scope));
        }
Beispiel #9
0
        protected ISchema CreateSchemaWith(
            IFilterInputType type,
            FilterConvention convention,
            params FilterConventionExtension[] extensions)
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <IFilterConvention>(convention)
                                     .AddFiltering()
                                     .AddQueryType(
                c =>
                c.Name("Query")
                .Field("foo")
                .Type <StringType>()
                .Resolver("bar"))
                                     .AddType(type);

            foreach (var extension in extensions)
            {
                builder.AddConvention <IFilterConvention>(extension);
            }

            return(builder.Create());
        }
Beispiel #10
0
        protected ISchema CreateSchemaWithTypes(
            ISortInputType type,
            SortConvention convention,
            params Type[] extensions)
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <ISortConvention>(convention)
                                     .AddSorting()
                                     .AddQueryType(
                c =>
                c.Name("Query")
                .Field("foo")
                .Type <StringType>()
                .Resolve("bar"))
                                     .AddType(type);

            foreach (var extension in extensions)
            {
                builder.AddConvention <ISortConvention>(extension);
            }

            return(builder.Create());
        }
Beispiel #11
0
 public static ISchemaBuilder AddConvention <TConvetion, TConcreteConvention>(
     this ISchemaBuilder builder,
     string?scope = null)
     where TConvetion : IConvention
     where TConcreteConvention : IConvention =>
 builder.AddConvention(typeof(TConvetion), typeof(TConcreteConvention), scope);
Beispiel #12
0
 public static ISchemaBuilder AddConvention <T>(
     this ISchemaBuilder builder,
     IConvention convention,
     string?scope = null)
     where T : IConvention =>
 builder.AddConvention(typeof(T), convention, scope);
Beispiel #13
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);
        }