Exemplo n.º 1
0
        public async Task DelegateWithGuidFieldArgument()
        {
            // arrange
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .AddExtensionsFromString(
                                                    FileResource.Open("StitchingExtensions.graphql"))
                                                .AddSchemaConfiguration(c =>
            {
                c.RegisterExtendedScalarTypes();
                c.RegisterType <PaginationAmountType>();
            }));

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            IQueryExecutor executor = services
                                      .GetRequiredService <IQueryExecutor>();
            IExecutionResult result = null;

            // act
            using (IServiceScope scope = services.CreateScope())
            {
                IReadOnlyQueryRequest request =
                    QueryRequestBuilder.New()
                    .SetQuery(@"
                        {
                            customer(id: ""Q3VzdG9tZXIKZDE="") {
                                guid
                            }
                        }")
                    .SetServices(scope.ServiceProvider)
                    .Create();

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }
        public async Task ExecuteStitchingQueryWithVariables()
        {
            // arrange
            var request = new QueryRequest(FileResource.Open(
                                               "StitchingQueryWithVariables.graphql"))
            {
                VariableValues = new Dictionary <string, object>
                {
                    { "customerId", "Q3VzdG9tZXIteDE=" }
                }
            };

            // act
            IExecutionResult result = await ExecuteStitchedQuery(request);

            // assert
            result.MatchSnapshot();
        }
Exemplo n.º 3
0
        private Task <IExecutionResult> ExecuteStitchedQuery(
            IReadOnlyQueryRequest request)
        {
            // arrange
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);

            serviceCollection.AddRemoteQueryExecutor(b => b
                                                     .SetSchemaName("contract")
                                                     .SetSchema(FileResource.Open("Contract.graphql"))
                                                     .AddScalarType <DateTimeType>());

            serviceCollection.AddRemoteQueryExecutor(b => b
                                                     .SetSchemaName("customer")
                                                     .SetSchema(FileResource.Open("Customer.graphql")));

            serviceCollection.AddStitchedSchema(
                FileResource.Open("Stitching.graphql"),
                c => c.RegisterType <DateTimeType>());

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            request = QueryRequestBuilder.From(request)
                      .SetServices(services)
                      .Create();

            IQueryExecutor executor = services
                                      .GetRequiredService <IQueryExecutor>();

            // act
            return(executor.ExecuteAsync(request));
        }
        public async Task ExecuteStitchedQueryBuilderWithLocalSchema()
        {
            // arrange
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .AddSchema("hello",
                                                           Schema.Create(
                                                               "type Query { hello: String! }",
                                                               c => c.BindResolver(ctx => "Hello World")
                                                               .To("Query", "hello")))
                                                .RenameField("customer",
                                                             new FieldReference("Customer", "name"), "foo")
                                                .RenameType("SomeOtherContract", "Other")
                                                .RenameType("LifeInsuranceContract", "Life")
                                                .AddExtensionsFromString(
                                                    FileResource.Open("StitchingExtensions.graphql"))
                                                .AddSchemaConfiguration(c =>
                                                                        c.RegisterType <PaginationAmountType>()));

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            IQueryExecutor executor = services
                                      .GetRequiredService <IQueryExecutor>();
            IExecutionResult result = null;

            // act
            using (IServiceScope scope = services.CreateScope())
            {
                var request = new QueryRequest(@"
                query a($id: ID!) {
                    a: customer2(customerId: $id) {
                        bar: foo
                        contracts {
                            id
                            ... life
                            ... on Other {
                                expiryDate
                            }
                        }
                    }
                    hello
                }

                fragment life on Life
                {
                    premium
                }

                ");
                request.VariableValues = new Dictionary <string, object>
                {
                    { "id", "Q3VzdG9tZXIteDE=" }
                };
                request.Services = scope.ServiceProvider;

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }
        public async Task ConnectionLost()
        {
            // arrange
            var connections = new Dictionary <string, HttpClient>();
            IHttpClientFactory clientFactory = CreateRemoteSchemas(connections);

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .RenameType("CreateCustomerInput", "CreateCustomerInput2")
                                                .AddExtensionsFromString(
                                                    FileResource.Open("StitchingExtensions.graphql"))
                                                .AddSchemaConfiguration(c =>
                                                                        c.RegisterType <PaginationAmountType>())
                                                .AddExecutionConfiguration(b =>
            {
                b.AddErrorFilter(error =>
                {
                    if (error.Exception is Exception ex)
                    {
                        return(ErrorBuilder.FromError(error)
                               .ClearExtensions()
                               .SetMessage(ex.GetType().FullName)
                               .SetException(null)
                               .Build());
                    }
                    ;
                    return(error);
                });
            }));

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            IQueryExecutor executor = services
                                      .GetRequiredService <IQueryExecutor>();
            IExecutionResult result = null;

            using (IServiceScope scope = services.CreateScope())
            {
                var request = new QueryRequest(@"
                    mutation {
                        createCustomer(input: { name: ""a"" })
                        {
                            customer {
                                name
                                contracts {
                                    id
                                }
                            }
                        }
                    }");
                request.Services = scope.ServiceProvider;

                result = await executor.ExecuteAsync(request);
            }

            var client = new HttpClient
            {
                BaseAddress = new Uri("http://127.0.0.1")
            };;

            connections["contract"] = client;
            connections["customer"] = client;

            // act
            using (IServiceScope scope = services.CreateScope())
            {
                var request = new QueryRequest(@"
                    mutation {
                        createCustomer(input: { name: ""a"" })
                        {
                            customer {
                                name
                                contracts {
                                    id
                                }
                            }
                        }
                    }");
                request.Services = scope.ServiceProvider;

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }