コード例 #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(sp =>
            {
                var client = new MongoClient();
                IMongoDatabase database = client.GetDatabase(
                    "db_" + Guid.NewGuid().ToString("N"));

                IMongoCollection <City> collection
                    = database.GetCollection <City>("col");
                collection.InsertMany(new[]
                {
                    new City(1, "Amsterdam", "nl", true),
                    new City(2, "Berlin", "de", true),
                    new City(3, "Paris", "fr", true),
                    new City(4, "Zürich", "ch", false)
                });

                return(collection);
            });

            services.AddGraphQL(s =>
            {
                ISchemaBuilder builder = SchemaBuilder.New()
                                         .AddQueryType <QueryType>()
                                         .AddServices(s);

                return(builder.Create());
            }, QueryOptions);
        }
コード例 #2
0
        public void Convention_DefaultScope_Extensions()
        {
            // arrange
            // act
            var convention = new FilterConvention(
                x => x.UseMock()
                .Configure <StringOperationFilterInput>(
                    y => y.Operation(DefaultOperations.Like).Type <StringType>())
                .Operation(DefaultOperations.Like)
                .Name("like"));

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <IFilterConvention>(convention)
                                     .AddTypeInterceptor <FilterTypeInterceptor>()
                                     .AddQueryType(
                c =>
                c.Name("Query")
                .Field("foo")
                .Type <StringType>()
                .Resolver("bar")
                .Argument("test", x => x.Type <TestFilter>()));

            ISchema schema = builder.Create();

            // assert
            schema.ToString().MatchSnapshot();
        }
コード例 #3
0
        public void Convention_DefaultScope_Extensions()
        {
            // arrange
            // act
            var convention = new SortConvention(
                x => x.UseMock()
                .Configure <TestSort>(
                    y => y.Field("foo").Type <DefaultSortEnumType>())
                .Operation(123).Name("test"));

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <ISortConvention>(convention)
                                     .TryAddTypeInterceptor <SortTypeInterceptor>()
                                     .AddQueryType(c =>
                                                   c.Name("Query")
                                                   .Field("foo")
                                                   .Type <StringType>()
                                                   .Resolver("bar")
                                                   .Argument("test", x => x.Type <TestSort>()));

            ISchema schema = builder.Create();

            // assert
            schema.ToString().MatchSnapshot();
        }
コード例 #4
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());
        }
コード例 #5
0
        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));
        }
コード例 #6
0
        protected IRequestExecutor CreateSchema <TEntity, T>(
            TEntity?[] entities,
            SortConvention?convention = null)
            where TEntity : class
            where T : SortInputType <TEntity>
        {
            convention ??= new SortConvention(x => x.AddDefaults().BindRuntimeType <TEntity, T>());

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

            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <ISortConvention>(convention)
                                     .AddSorting()
                                     .AddQueryType(
                c =>
            {
                c
                .Name("Query")
                .Field("root")
                .Resolver(resolver)
                .UseSorting <T>();

                c
                .Name("Query")
                .Field("rootExecutable")
                .Resolver(ctx => resolver(ctx).AsExecutable())
                .UseSorting <T>();
            });

            ISchema?schema = builder.Create();

            return(schema.MakeExecutable());
        }
コード例 #7
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 =>
            {
                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);
        }
コード例 #8
0
ファイル: Startup.cs プロジェクト: gmiserez/HotChocolate_Demo
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(sp =>
            {
                var client = new MongoClient();
                IMongoDatabase database = client.GetDatabase(
                    "db_" + Guid.NewGuid().ToString("N"));

                IMongoCollection <City> collection
                    = database.GetCollection <City>("col");
                collection.InsertMany(new[]
                {
                    new City(
                        1,
                        "Amsterdam",
                        "nl",
                        true,
                        new DateTime(2019, 6, 1, 1, 1, 1, DateTimeKind.Utc)),
                    new City(
                        2,
                        "Berlin",
                        "de",
                        true,
                        new DateTime(2019, 7, 10, 1, 1, 1, DateTimeKind.Utc)),
                    new City(
                        3,
                        "Paris",
                        "fr",
                        true,
                        new DateTime(2019, 6, 18, 1, 1, 1, DateTimeKind.Utc)),
                    new City(
                        4,
                        "Zürich",
                        "ch",
                        false,
                        new DateTime(2019, 3, 15, 1, 1, 1, DateTimeKind.Utc))
                });

                return(collection);
            });

            services.AddGraphQL(s =>
            {
                ISchemaBuilder builder = SchemaBuilder.New()
                                         .AddType <CityType>()
                                         .AddQueryType <QueryType>()
                                         .AddServices(s);

                return(builder.Create());
            }, QueryOptions);

            services.AddDiagnosticObserver <HotChocolateLogger>();
            services.AddLogging();
        }
コード例 #9
0
        public void FilterInputType_Should_UseCustomFilterInput_When_Nested()
        {
            // arrange
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddFiltering()
                                     .AddQueryType <UserQueryType>();

            // act
            // assert
            builder.Create().Print().MatchSnapshot();
        }
コード例 #10
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGraphQL(s =>
            {
                ISchemaBuilder builder = SchemaBuilder.New()
                                         .EnableRelaySupport()
                                         .AddQueryType <QueryType>()
                                         .AddServices(s);

                return(builder.Create());
            }, QueryOptions);
        }
コード例 #11
0
        protected IRequestExecutor CreateSchema <TEntity>(TEntity[] entities)
            where TEntity : class
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddQueryType(
                c => c.Name("Query")
                .Field("root")
                .UseDbContext <DatabaseContext <TEntity> >()
                .Resolve(ctx =>
            {
                DatabaseContext <TEntity> context =
                    ctx.DbContext <DatabaseContext <TEntity> >();
                BuildContext(context, entities);
                return(context.Data);
            })
                .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;
                    }
                }
            })
                .UseOffsetPaging <ObjectType <TEntity> >(options: new()
            {
                IncludeTotalCount = true
            }));

            ISchema schema = builder.Create();

            return(new ServiceCollection()
                   .Configure <RequestExecutorSetup>(
                       Schema.DefaultName,
                       o => o.Schema = schema)
                   .AddPooledDbContextFactory <DatabaseContext <TEntity> >(
                       b => b.UseSqlite($"Data Source={Guid.NewGuid():N}.db"))
                   .AddGraphQL()
                   .UseDefaultPipeline()
                   .Services
                   .BuildServiceProvider()
                   .GetRequiredService <IRequestExecutorResolver>()
                   .GetRequestExecutorAsync()
                   .Result);
        }
コード例 #12
0
ファイル: TypeTestBase.cs プロジェクト: anadale/hotchocolate
        public static ISchema CreateSchema(Action <ISchemaBuilder> configure)
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddQueryType(c =>
                                                   c.Name("Query")
                                                   .Field("foo")
                                                   .Type <StringType>()
                                                   .Resolve("bar"));

            configure(builder);

            return(builder.Create());
        }
コード例 #13
0
        protected ISchema CreateSchemaWith(ISortInputType type, SortConvention convention)
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddConvention <ISortConvention>(convention)
                                     .AddSorting()
                                     .AddQueryType(
                c =>
                c.Name("Query")
                .Field("foo")
                .Type <StringType>()
                .Resolver("bar"))
                                     .AddType(type);

            return(builder.Create());
        }
コード例 #14
0
    public void FilterInputType_Should_InfereType_When_ItIsAInterface()
    {
        // arrange
        ISchemaBuilder builder = SchemaBuilder.New()
                                 .AddFiltering()
                                 .AddQueryType <TestingType <ITest <Foo> > >()
                                 .AddObjectType <ITest <Foo> >();

        // act
        ISchema schema = builder.Create();

        // assert
        schema.ToString().MatchSnapshot();
        schema.Print().MatchSnapshot();
    }
コード例 #15
0
        public void ObjectField_UseSorting_Generic_SchemaType()
        {
            // arrange
            // act
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddSorting()
                                     .AddQueryType <Query>(
                c =>
                c.Field(x => x.GetFoos()).UseSorting <BarSortType>());

            ISchema schema = builder.Create();

            // assert
            schema.ToString().MatchSnapshot();
        }
コード例 #16
0
        public void ObjectField_UseSorting_Type_RuntimeType()
        {
            // arrange
            // act
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddSorting()
                                     .AddQueryType <Query>(
                c =>
                c.Field(x => x.GetFoos()).UseSorting(typeof(Bar)));

            ISchema schema = builder.Create();

            // assert
            schema.ToString().MatchSnapshot();
        }
コード例 #17
0
        public void Execute_UnknownDefaultCRS()
        {
            // arrange
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddQueryType <Query>()
                                     .AddSpatialTypes(x => x
                                                      .DefaultSrid(4326)
                                                      .AddCoordinateSystemFromString(26918, WKT26918))
                                     .TryAddTypeInterceptor <RoundTypeIntercetor>();

            // act
            Exception?ex = Record.Exception(() => builder.Create());

            // assert
            Assert.IsType <SchemaException>(ex).Message.MatchSnapshot();
        }
コード例 #18
0
        protected ISchema CreateSchema <T>(T type)
            where T : IFilterInputType
        {
            var            convention = new FilterConvention(x => x.AddDefaults());
            ISchemaBuilder builder    = SchemaBuilder.New()
                                        .AddConvention <IFilterConvention>(convention)
                                        .AddTypeInterceptor <FilterTypeInterceptor>()
                                        .AddQueryType(c => c
                                                      .Name("Query")
                                                      .Field("foo")
                                                      .Type <StringType>()
                                                      .Resolver("bar"))
                                        .AddType(type);

            return(builder.Create());
        }
コード例 #19
0
        public void SortInputType_Should_ThrowException_WhenNoConventionIsRegisteredDefault()
        {
            // arrange
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddQueryType(c =>
                                                   c.Name("Query")
                                                   .Field("foo")
                                                   .Resolve(new List <Foo>())
                                                   .UseSorting());

            // act
            // assert
            SchemaException exception = Assert.Throws <SchemaException>(() => builder.Create());

            exception.Message.MatchSnapshot();
        }
コード例 #20
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGraphQL(s =>
            {
                ISchemaBuilder builder = SchemaBuilder.New()
                                         .AddType <AbcType>()
                                         .AddType <XyzType>()
                                         .AddQueryType <QueryType>()
                                         .AddServices(s);

                return(builder.Create());
            }, QueryOptions);

            services.AddDiagnosticObserver <HotChocolateLogger>();
            services.AddLogging();
        }
コード例 #21
0
ファイル: ExtensionsTest.cs プロジェクト: zmarty/hotchocolate
        public void ObjectField_UseFiltering_Descriptor()
        {
            // arrange
            // act
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddFiltering()
                                     .AddQueryType <Query>(
                c =>
                c.Field(x => x.GetFoos())
                .UseFiltering <Bar>(
                    x => x.Name("foo").Field(x => x.Foo)));

            ISchema schema = builder.Create();

            // assert
            schema.ToString().MatchSnapshot();
        }
コード例 #22
0
        protected T CreateDirective <T>(T directiveType,
                                        Action <ISchemaBuilder> configure)
            where T : DirectiveType
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddQueryType(c =>
                                                   c.Name("Query")
                                                   .Field("foo")
                                                   .Type <StringType>()
                                                   .Resolver("bar"))
                                     .AddDirectiveType(directiveType);

            configure(builder);

            builder.Create();

            return(directiveType);
        }
コード例 #23
0
ファイル: TypeTestBase.cs プロジェクト: anadale/hotchocolate
        protected static T CreateType <T>(T type,
                                          Action <ISchemaBuilder> configure)
            where T : INamedType
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddQueryType(c =>
                                                   c.Name("Query")
                                                   .Field("foo")
                                                   .Type <StringType>()
                                                   .Resolve("bar"))
                                     .AddType(type);

            configure(builder);

            builder.Create();

            return(type);
        }
コード例 #24
0
        public void AddNameLookup_Single()
        {
            // arrange
            ISchemaBuilder schemaBuilder = SchemaBuilder.New().AddQueryType <CustomQueryType>();

            // act
            schemaBuilder.AddNameLookup("OriginalType1", "NewType1", "Schema1");
            schemaBuilder.AddNameLookup("OriginalType2", "NewType2", "Schema2");

            // assert
            IReadOnlyDictionary <(NameString, NameString), NameString> lookup =
                schemaBuilder
                .Create()
                .GetType <CustomQueryType>(nameof(CustomQueryType))
                .Context
                .GetNameLookup();

            Assert.Equal("OriginalType1", lookup[("NewType1", "Schema1")]);
コード例 #25
0
        public ISchema CreateSchema(Action <ISchemaBuilder> configure)
        {
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddFiltering(x => x
                                                   .AddDefaults()
                                                   .AddSpatialOperations()
                                                   .BindSpatialTypes()
                                                   .Provider(
                                                       new QueryableFilterProvider(
                                                           p => p.AddSpatialHandlers().AddDefaultFieldHandlers())))
                                     .AddQueryType(c => c
                                                   .Name("Query")
                                                   .Field("foo")
                                                   .Type <StringType>()
                                                   .Resolver("bar"));

            configure(builder);

            return(builder.Create());
        }
コード例 #26
0
        public void Create_Global_Explicit_Sorting()
        {
            // arrange
            // act
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddQueryType(c =>
                                                   c.Name("Query")
                                                   .Field("foo")
                                                   .Type <StringType>()
                                                   .Resolve("bar"))
                                     .AddType(new SortInputType <Foo>(
                                                  d => d.BindFieldsExplicitly()
                                                  .Sortable(f => f.Bar)
                                                  ))
                                     .ModifyOptions(t => t.DefaultBindingBehavior = BindingBehavior.Explicit);

            ISchema schema = builder.Create();

            // assert
            schema.ToString().MatchSnapshot();
        }
コード例 #27
0
        public void FilterInputType_Should_IgnoreFieldWithoutCallingConvention()
        {
            // arrange
            ISchemaBuilder builder = SchemaBuilder.New()
                                     .AddFiltering(
                x => x.AddDefaultOperations()
                .BindRuntimeType <string, StringOperationFilterInputType>()
                .Provider(new QueryableFilterProvider(y => y.AddDefaultFieldHandlers())))
                                     .AddQueryType(
                new ObjectType(
                    x => x.Name("Query")
                    .Field("foo")
                    .Resolve(new List <IgnoreTest>())
                    .UseFiltering <IgnoreTestFilterInputType>()));

            // act
            ISchema schema = builder.Create();

            // assert
            schema.ToString().MatchSnapshot();
        }
コード例 #28
0
    public void SortInputType_Should_IgnoreFieldWithoutCallingConvention()
    {
        // arrange
        ISchemaBuilder builder = SchemaBuilder.New()
                                 .AddSorting(
            x => x.AddDefaultOperations()
            .BindRuntimeType <int, DefaultSortEnumType>()
            //should fail when not ignore properly because string is no explicitly bound
            .Provider(new QueryableSortProvider(y => y.AddDefaultFieldHandlers())))
                                 .AddQueryType(
            new ObjectType(
                x => x.Name("Query")
                .Field("foo")
                .Resolve(new List <IgnoreTest>())
                .UseSorting <IgnoreTestSortInputType>()));

        // act
        ISchema schema = builder.Create();

        // assert
        schema.ToString().MatchSnapshot();
    }
コード例 #29
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());
        }
コード例 #30
0
        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));
        }