private Task <IExecutionResult> ExecuteStitchedQuery(
            QueryRequest 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 =
                request.Services =
                    serviceCollection.BuildServiceProvider();

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

            // act
            return(executor.ExecuteAsync(request));
        }
        public void GetFieldDependencies()
        {
            // arrange
            ISchema schema = Schema.Create(
                FileResource.Open("Stitching.graphql"),
                c =>
            {
                c.RegisterType <DateTimeType>();
                c.RegisterDirective <DelegateDirectiveType>();
                c.Use(next => context => Task.CompletedTask);
            });

            DocumentNode query = Utf8GraphQLParser.Parse(
                FileResource.Open("StitchingQuery.graphql"));

            FieldNode selection = query.Definitions
                                  .OfType <OperationDefinitionNode>().Single()
                                  .SelectionSet.Selections
                                  .OfType <FieldNode>().Single();

            // act
            var fieldDependencyResolver         = new FieldDependencyResolver(schema);
            ISet <FieldDependency> dependencies = fieldDependencyResolver
                                                  .GetFieldDependencies(
                query,
                selection,
                schema.GetType <ObjectType>("Customer"));

            // assert
            Snapshot.Match(dependencies);
        }
        ExecutedMutationWithRenamedInputField(
            IQueryRequestBuilder requestBuilder)
        {
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

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

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

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

            using (IServiceScope scope = services.CreateScope())
            {
                requestBuilder.SetServices(scope.ServiceProvider);
                return(await executor.ExecuteAsync(requestBuilder.Create()));
            }
        }
Exemple #4
0
        public async Task ExecuteStitchedQueryBuilderWithRenamedType()
        {
            // arrange
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .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())
            {
                IReadOnlyQueryRequest request =
                    QueryRequestBuilder.New()
                    .SetQuery(@"
                            query a($id: ID!) {
                                a: customer2(customerId: $id) {
                                    bar: foo
                                    contracts {
                                        id
                                        ... life
                                        ... on Other {
                                            expiryDate
                                        }
                                    }
                                }
                            }

                            fragment life on Life
                            {
                                premium
                            }")
                    .SetVariableValue("id", "Q3VzdG9tZXIKZDE=")
                    .SetServices(scope.ServiceProvider)
                    .Create();

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }
        public async Task ExecuteStitchedQueryBuilderVariableArguments()
        {
            // arrange
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .RenameField("customer",
                                                             new FieldReference("Customer", "name"), "foo")
                                                .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! $bar: String) {
                        contracts(customerId: $id)
                        {
                            id
                            customerId
                            ... foo
                        }
                    }

                    fragment foo on LifeInsuranceContract
                    {
                        foo(bar: $bar)
                    }
                ");
                request.VariableValues = new Dictionary <string, object>
                {
                    { "id", "Q3VzdG9tZXIteDE=" },
                    { "bar", "this variable is passed to remote query!" }
                };
                request.Services = scope.ServiceProvider;

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }
Exemple #6
0
        public async Task ExecuteStitchedQueryBuilder()
        {
            // arrange
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .RenameField("customer",
                                                             new FieldReference("Customer", "name"), "foo")
                                                .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())
            {
                IReadOnlyQueryRequest request =
                    QueryRequestBuilder.New()
                    .SetQuery(@"
                        {
                            a: customer(id: ""Q3VzdG9tZXIKZDE="") {
                                bar: foo
                                contracts {
                                    id
                                }
                            }

                            b: customer(id: ""Q3VzdG9tZXIKZDE="") {
                                foo
                                contracts {
                                    id
                                }
                            }
                        }")
                    .SetServices(scope.ServiceProvider)
                    .Create();

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }
Exemple #7
0
        public async Task HttpErrorsHavePathSet()
        {
            // 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")
                                                .AddExtensionsFromString(
                                                    FileResource.Open("StitchingExtensions.graphql"))
                                                .AddSchemaConfiguration(c =>
                                                                        c.RegisterType <PaginationAmountType>()));

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

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

            // have to replace the http client after the schema is built
            connections["customer"] = new HttpClient(new ServiceUnavailableDelegatingHandler())
            {
                BaseAddress = connections["customer"].BaseAddress
            };


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

                result = await executor.ExecuteAsync(request);
            }

            // assert
            result.MatchSnapshot(options => options.IgnoreField("Errors[0].Exception.StackTrace"));
        }
Exemple #8
0
        public async Task AddErrorFilter()
        {
            // 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.RegisterType <PaginationAmountType>())
                                                .AddExecutionConfiguration(b =>
            {
                b.AddErrorFilter(error =>
                                 error.AddExtension("STITCH", "SOMETHING"));
            }));

            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="") {
                                contracts {
                                    id
                                    ... on LifeInsuranceContract {
                                        error
                                    }
                                }
                            }
                        }")
                    .SetServices(scope.ServiceProvider)
                    .Create();

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }
        public async Task ExecuteStitchingQueryWithUnion()
        {
            // arrange
            var request = new QueryRequest(FileResource.Open(
                                               "StitchingQueryWithUnion.graphql"));

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

            // assert
            result.MatchSnapshot();
        }
Exemple #10
0
        public async Task StitchedMutationWithRenamedFieldArgument()
        {
            // arrange
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .RenameFieldArgument(
                                                    "Mutation", "createCustomer", "input", "input2")
                                                .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())
            {
                IReadOnlyQueryRequest request =
                    QueryRequestBuilder.New()
                    .SetQuery(@"
                        mutation {
                            createCustomer(input2: { name: ""a"" })
                            {
                                customer {
                                    name
                                    contracts {
                                        id
                                    }
                                }
                            }
                        }")
                    .SetServices(scope.ServiceProvider)
                    .Create();

                result = await executor.ExecuteAsync(request);
            }

            // assert
            Snapshot.Match(result);
        }
Exemple #11
0
        public async Task ExecuteStitchedQueryWithComputedField()
        {
            // 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("StitchingComputed.graphql"),
                c =>
            {
                c.Map(new FieldReference("Customer", "foo"),
                      next => context =>
                {
                    var obj = context
                              .Parent <IReadOnlyDictionary <string, object> >();
                    context.Result = obj["name"] + "_" + obj["id"];
                    return(Task.CompletedTask);
                });
                c.RegisterType <DateTimeType>();
            });

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            var request = QueryRequestBuilder.New()
                          .SetQuery(
                FileResource.Open("StitchingQueryComputedField.graphql"))
                          .SetServices(services)
                          .Create();

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

            // act
            IExecutionResult result = await executor.ExecuteAsync(request);

            // assert
            Snapshot.Match(result);
        }
Exemple #12
0
        public async Task ExecuteStitchingQueryWithArguments()
        {
            // arrange
            var request = QueryRequestBuilder.New()
                          .SetQuery(FileResource.Open(
                                        "StitchingQueryWithArguments.graphql"))
                          .Create();

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

            // assert
            result.MatchSnapshot();
        }
        public async Task ExecuteStitchingQueryWithVariables()
        {
            // arrange
            var request = QueryRequestBuilder.New()
                          .SetQuery(FileResource.Open(
                                        "StitchingQueryWithVariables.graphql"))
                          .SetVariableValue("customerId", "Q3VzdG9tZXIteDE=")
                          .Create();

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

            // assert
            result.MatchSnapshot();
        }
        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: ""Q3VzdG9tZXIteDE="") {
                                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();
        }
        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);
        }