Skip to content

Commit 26b6b11

Browse files
authored
Fixed load fusion schema settings (#8533)
1 parent 5d7a5c2 commit 26b6b11

File tree

6 files changed

+138
-14
lines changed

6 files changed

+138
-14
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Configuration/InMemoryFusionConfigurationProvider.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ namespace HotChocolate.Fusion.Configuration;
88

99
public sealed class InMemoryFusionConfigurationProvider : IFusionConfigurationProvider
1010
{
11-
public InMemoryFusionConfigurationProvider(DocumentNode schemaDocument, JsonDocument? schemaSettings)
11+
private readonly JsonDocumentOwner? _schemaSettings;
12+
13+
public InMemoryFusionConfigurationProvider(DocumentNode schemaDocument, JsonDocumentOwner? schemaSettings)
1214
{
1315
ArgumentNullException.ThrowIfNull(schemaDocument);
1416

1517
Configuration = new FusionConfiguration(
1618
schemaDocument,
1719
new JsonDocumentOwner(
18-
schemaSettings ?? JsonDocument.Parse("{ }"),
20+
schemaSettings?.Document ?? JsonDocument.Parse("{ }"),
1921
EmptyMemoryOwner.Instance));
22+
_schemaSettings = schemaSettings;
2023
}
2124

2225
public FusionConfiguration Configuration { get; }
@@ -29,7 +32,10 @@ public IDisposable Subscribe(IObserver<FusionConfiguration> observer)
2932
}
3033

3134
public ValueTask DisposeAsync()
32-
=> ValueTask.CompletedTask;
35+
{
36+
_schemaSettings?.Dispose();
37+
return ValueTask.CompletedTask;
38+
}
3339

3440
private sealed class EmptyMemoryOwner : IMemoryOwner<byte>
3541
{

src/HotChocolate/Fusion-vnext/src/Fusion.Execution/DependencyInjection/CoreFusionGatewayBuilderExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json;
2+
using HotChocolate.Buffers;
13
using HotChocolate.Fusion.Configuration;
24
using HotChocolate.Language;
35

@@ -39,13 +41,14 @@ public static IFusionGatewayBuilder AddFileSystemConfiguration(
3941

4042
public static IFusionGatewayBuilder AddInMemoryConfiguration(
4143
this IFusionGatewayBuilder builder,
42-
DocumentNode schemaDocument)
44+
DocumentNode schemaDocument,
45+
JsonDocumentOwner? schemaSettings = null)
4346
{
4447
ArgumentNullException.ThrowIfNull(builder);
4548
ArgumentNullException.ThrowIfNull(schemaDocument);
4649

4750
return Configure(
4851
builder,
49-
setup => setup.DocumentProvider = _ => new InMemoryFusionConfigurationProvider(schemaDocument, null));
52+
setup => setup.DocumentProvider = _ => new InMemoryFusionConfigurationProvider(schemaDocument, schemaSettings));
5053
}
5154
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Execution/FusionRequestExecutorManager.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,19 @@ private SourceSchemaClientConfigurations CreateClientConfigurations(
213213
{
214214
foreach (var sourceSchema in sourceSchemas.EnumerateObject())
215215
{
216-
if (sourceSchema.Value.TryGetProperty("http", out var http))
216+
if (sourceSchema.Value.TryGetProperty("transports", out var transports))
217217
{
218-
var httpClient = new SourceSchemaHttpClientConfiguration(
219-
sourceSchema.Name,
220-
httpClientName: "Fusion",
221-
new Uri(http.GetProperty("url").GetString()!));
218+
if (transports.TryGetProperty("http", out var http))
219+
{
220+
var hasClientName = http.TryGetProperty("clientName", out var clientName);
222221

223-
configurations.Add(httpClient);
222+
var httpClient = new SourceSchemaHttpClientConfiguration(
223+
sourceSchema.Name,
224+
httpClientName: hasClientName ? clientName.GetString()! : "fusion",
225+
new Uri(http.GetProperty("url").GetString()!));
226+
227+
configurations.Add(httpClient);
228+
}
224229
}
225230
}
226231
}

src/HotChocolate/Fusion-vnext/test/Fusion.AspNetCore.Tests/BookStoreTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,67 @@ public async Task Fetch_Book_From_SourceSchema1()
4545
response.MatchSnapshot();
4646
}
4747

48+
[Fact]
49+
public async Task Fetch_Book_From_SourceSchema1_With_Settings()
50+
{
51+
// arrange
52+
using var server1 = CreateSourceSchema(
53+
"a",
54+
b => b.AddQueryType<SourceSchema1.Query>());
55+
56+
using var server2 = CreateSourceSchema(
57+
"b",
58+
b => b.AddQueryType<SourceSchema2.Query>());
59+
60+
// act
61+
using var gateway = await CreateCompositeSchemaAsync(
62+
[
63+
("a", server1),
64+
("b", server2),
65+
],
66+
schemaSettings:
67+
"""
68+
{
69+
"sourceSchemas": {
70+
"a": {
71+
"transports": {
72+
"http": {
73+
"clientName": "a",
74+
"url": "http://localhost:5000/graphql"
75+
}
76+
}
77+
},
78+
"b": {
79+
"transports": {
80+
"http": {
81+
"clientName": "b",
82+
"url": "http://localhost:5000/graphql"
83+
}
84+
}
85+
}
86+
}
87+
}
88+
""");
89+
90+
// assert
91+
using var client = GraphQLHttpClient.Create(gateway.CreateClient());
92+
93+
using var result = await client.PostAsync(
94+
"""
95+
{
96+
bookById(id: 1) {
97+
id
98+
title
99+
}
100+
}
101+
""",
102+
new Uri("http://localhost:5000/graphql"));
103+
104+
// act
105+
using var response = await result.ReadAsResultAsync();
106+
response.MatchSnapshot();
107+
}
108+
48109
[Fact]
49110
public async Task Fetch_Book_From_SourceSchema1_Two_Requests()
50111
{

src/HotChocolate/Fusion-vnext/test/Fusion.AspNetCore.Tests/FusionTestBase.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using System.Buffers;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Text.Json;
14
using HotChocolate.AspNetCore;
5+
using HotChocolate.Buffers;
26
using HotChocolate.Execution;
37
using HotChocolate.Execution.Configuration;
48
using HotChocolate.Fusion.Logging;
@@ -43,7 +47,8 @@ public TestServer CreateSourceSchema(
4347
public async Task<TestServer> CreateCompositeSchemaAsync(
4448
(string SchemaName, TestServer Server)[] sourceSchemaServers,
4549
Action<IServiceCollection>? configureServices = null,
46-
Action<IApplicationBuilder>? configureApplication = null)
50+
Action<IApplicationBuilder>? configureApplication = null,
51+
[StringSyntax("json")] string? schemaSettings = null)
4752
{
4853
var sourceSchemas = new List<SourceSchemaText>();
4954
var gatewayServices = new ServiceCollection();
@@ -54,7 +59,11 @@ public async Task<TestServer> CreateCompositeSchemaAsync(
5459
var schemaDocument = await server.Services.GetSchemaAsync(name);
5560
sourceSchemas.Add(new SourceSchemaText(name, schemaDocument.ToString()));
5661
gatewayServices.AddHttpClient(name, server);
57-
gatewayBuilder.AddHttpClientConfiguration(name, new Uri("http://localhost:5000/graphql"));
62+
63+
if (schemaSettings is null)
64+
{
65+
gatewayBuilder.AddHttpClientConfiguration(name, new Uri("http://localhost:5000/graphql"));
66+
}
5867
}
5968

6069
var compositionLog = new CompositionLog();
@@ -66,7 +75,14 @@ public async Task<TestServer> CreateCompositeSchemaAsync(
6675
throw new InvalidOperationException(result.Errors[0].Message);
6776
}
6877

69-
gatewayBuilder.AddInMemoryConfiguration(result.Value.ToSyntaxNode());
78+
JsonDocumentOwner? settings = null;
79+
if (schemaSettings is not null)
80+
{
81+
var body = JsonDocument.Parse(schemaSettings);
82+
settings = new JsonDocumentOwner(body, new EmptyMemoryOwner());
83+
}
84+
85+
gatewayBuilder.AddInMemoryConfiguration(result.Value.ToSyntaxNode(), settings);
7086
gatewayBuilder.AddHttpRequestInterceptor<OperationPlanHttpRequestInterceptor>();
7187
gatewayBuilder.ModifyRequestOptions(o => o.CollectOperationPlanTelemetry = false);
7288

@@ -125,4 +141,11 @@ public override ValueTask OnCreateAsync(
125141
return base.OnCreateAsync(context, requestExecutor, requestBuilder, cancellationToken);
126142
}
127143
}
144+
145+
private class EmptyMemoryOwner : IMemoryOwner<byte>
146+
{
147+
public Memory<byte> Memory => default;
148+
149+
public void Dispose() { }
150+
}
128151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"data": {
3+
"bookById": {
4+
"id": 1,
5+
"title": "C# in Depth"
6+
}
7+
},
8+
"extensions": {
9+
"fusion": {
10+
"operationPlan": {
11+
"operation": {
12+
"document": "{\n bookById(id: 1) {\n id\n title\n }\n}",
13+
"hash": "da31ef9bfb0a7c42de9b1578b454d0af"
14+
},
15+
"nodes": [
16+
{
17+
"id": 1,
18+
"type": "Operation",
19+
"schema": "a",
20+
"operation": "query Op_da31ef9b_1 {\n bookById(id: 1) {\n id\n title\n }\n}"
21+
}
22+
]
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)