From f122129d0b584221091862ed5cbd59293d62dacb Mon Sep 17 00:00:00 2001 From: Glen Date: Thu, 31 Oct 2024 14:27:02 +0200 Subject: [PATCH] Added missing ParallelExecutable flag to node and nodes fields (#7661) --- .../Types/Relay/NodeFieldTypeInterceptor.cs | 4 +- .../Integration/DataLoader/DataLoaderTests.cs | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/HotChocolate/Core/src/Types/Types/Relay/NodeFieldTypeInterceptor.cs b/src/HotChocolate/Core/src/Types/Types/Relay/NodeFieldTypeInterceptor.cs index 96f6b95e39d..6d88de1aa87 100644 --- a/src/HotChocolate/Core/src/Types/Types/Relay/NodeFieldTypeInterceptor.cs +++ b/src/HotChocolate/Core/src/Types/Types/Relay/NodeFieldTypeInterceptor.cs @@ -94,7 +94,7 @@ private static void CreateNodeField( }; }), }, - Flags = FieldFlags.GlobalIdNodeField + Flags = FieldFlags.ParallelExecutable | FieldFlags.GlobalIdNodeField }; // In the projection interceptor we want to change the context data that is on this field @@ -134,7 +134,7 @@ private static void CreateNodesField( }; }), }, - Flags = FieldFlags.GlobalIdNodesField + Flags = FieldFlags.ParallelExecutable | FieldFlags.GlobalIdNodesField }; // In the projection interceptor we want to change the context data that is on this field diff --git a/src/HotChocolate/Core/test/Execution.Tests/Integration/DataLoader/DataLoaderTests.cs b/src/HotChocolate/Core/test/Execution.Tests/Integration/DataLoader/DataLoaderTests.cs index 957668c1b4d..64ef4ccfe24 100644 --- a/src/HotChocolate/Core/test/Execution.Tests/Integration/DataLoader/DataLoaderTests.cs +++ b/src/HotChocolate/Core/test/Execution.Tests/Integration/DataLoader/DataLoaderTests.cs @@ -59,6 +59,43 @@ await ExpectValid( await snapshot.MatchMarkdownAsync(); } + [Fact] + public async Task FetchMultipleNodesDataLoader() + { + var batchFetchCount = 0; + + await ExpectValid( + """ + { + a: node(id: "RW50aXR5OjE==") { ... on Entity { id } } + b: node(id: "RW50aXR5OjI==") { ... on Entity { id } } + } + """, + configure: b => b + .AddGraphQL() + .AddGlobalObjectIdentification() + .AddObjectType(descriptor => + { + descriptor + .ImplementsNode() + .IdField(e => e.Id) + .ResolveNode( + async (ctx, id) => await ctx.BatchDataLoader( + (keys, _) => + { + batchFetchCount++; + + return Task.FromResult>( + keys.ToDictionary(t => t, _ => new Entity { Id = id })); + }) + .LoadAsync(id)) + .Resolve(ctx => ctx.Parent().Id); + }) + .AddQueryType()); + + Assert.Equal(1, batchFetchCount); + } + [LocalFact] public async Task FetchDataLoader() { @@ -667,4 +704,9 @@ public CounterDataLoader(DataLoaderOptions options) : base(options) protected override Task LoadSingleAsync(string key, CancellationToken cancellationToken) => Task.FromResult(key + Counter); } + + public class Entity + { + public int Id { get; set; } + } }