Skip to content

Commit b202a13

Browse files
authored
Merge pull request #28 from Linq2GraphQL/add-debug-info
added client unique cache for schema
2 parents 70fecc8 + aba1c0e commit b202a13

5 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/Linq2GraphQL.Client/GraphBaseExecute.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq.Expressions;
1+
using Linq2GraphQL.Client.Schema;
2+
using System.Linq.Expressions;
23

34
namespace Linq2GraphQL.Client;
45

@@ -13,6 +14,7 @@ public abstract class GraphBaseExecute<T, TResult>
1314

1415
private readonly SemaphoreSlim _lock = new(1, 1);
1516
private bool intialized;
17+
private GraphQLSchema schema;
1618

1719
public GraphBaseExecute(GraphClient client, OperationType operationType, QueryNode queryNode,
1820
Expression<Func<T, TResult>> selector)
@@ -32,7 +34,7 @@ private async Task InitQueryAsync()
3234
{
3335
if (!intialized)
3436
{
35-
var schema = await client.GetSchemaForSafeModeAsync();
37+
schema = await client.GetSchemaForSafeModeAsync();
3638
QueryNode.SetAllUniqueVariableNames();
3739
QueryNode.AddPrimitiveChildren(true, schema);
3840
intialized = true;
@@ -43,12 +45,10 @@ private async Task InitQueryAsync()
4345
{
4446
_lock.Release();
4547
}
46-
47-
48-
49-
5048
}
5149

50+
public GraphQLSchema Schema => schema;
51+
5252
private string GetQueryVariablesString()
5353
{
5454
var args = QueryNode.GetAllActiveArguments();

src/Linq2GraphQL.Client/GraphClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class GraphClient
1111
{
1212
private readonly IMemoryCache cache;
1313
private readonly IOptions<GraphClientOptions> options;
14+
private Guid clientId = Guid.NewGuid();
1415

1516
public GraphClient(HttpClient httpClient, IOptions<GraphClientOptions> options, IServiceProvider provider)
1617
{
@@ -64,7 +65,9 @@ public async Task<GraphQLSchema> GetSchemaForSafeModeAsync()
6465
return null;
6566
}
6667

67-
return await cache.GetOrCreateAsync("__schema", async entry =>
68+
var cackeKey = "Linq2GraphQL_Schema_" + clientId;
69+
70+
return await cache.GetOrCreateAsync(cackeKey, async entry =>
6871
{
6972
var executor = new QueryExecutor<GraphQLSchema>(this);
7073

src/Linq2GraphQL.Client/QueryExecutor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ internal QueryExecutor(GraphClient client)
1717
internal async Task<T> ExecuteRequestAsync(string alias, GraphQLRequest graphRequest)
1818
{
1919
var json = JsonSerializer.Serialize(graphRequest, client.SerializerOptions);
20-
2120
using var response = await client.HttpClient.PostAsJsonAsync("", graphRequest, client.SerializerOptions);
2221

2322
if (!response.IsSuccessStatusCode)

test/Linq2GraphQL.Tests/QueryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public async Task Customers_WithIncludeSelect()
6464

6565
var result = await query.ExecuteAsync();
6666

67+
var schema = query.Schema;
68+
6769
var customer1 = result[0];
6870
Assert.NotEqual(default, customer1.CustomerId);
6971
Assert.Equal(default, customer1.CustomerName);

test/Linq2GraphQL.Tests/SampleClientFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public SampleClientFixture()
1919
sampleClient = new SampleClient(client, Options.Create(new GraphClientOptions
2020
{
2121
SubscriptionProtocol = SubscriptionProtocol.ServerSentEvents,
22-
UseSafeMode = false
22+
UseSafeMode = true
2323
}), application.Services);
2424
//Please note currently only ServerSentEvents work in test project
2525
}

0 commit comments

Comments
 (0)