-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backported inline DataLoader fix from 13 (#4916)
- Loading branch information
1 parent
ee6184e
commit 4b991aa
Showing
9 changed files
with
214 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/HotChocolate/Core/test/Fetching.Tests/InlineBatchDataLoaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Tests; | ||
using HotChocolate.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Fetching; | ||
|
||
public class InlineBatchDataLoaderTests | ||
{ | ||
[Fact] | ||
public async Task LoadWithDifferentDataLoader() | ||
{ | ||
// arrange | ||
IRequestExecutor executor = | ||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.BuildRequestExecutorAsync(); | ||
|
||
// act | ||
var result1 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
var result2 = await executor.ExecuteAsync("{ byKey(key: \"def\") }").ToJsonAsync(); | ||
|
||
// assert | ||
Assert.NotEqual(result1, result2); | ||
} | ||
|
||
[Fact] | ||
public async Task LoadWithSingleKeyDataLoader() | ||
{ | ||
// arrange | ||
IRequestExecutor executor = | ||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.BuildRequestExecutorAsync(); | ||
|
||
// act | ||
var result1 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
var result2 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
|
||
// assert | ||
Assert.Equal(result1, result2); | ||
} | ||
|
||
public class Query | ||
{ | ||
public async Task<string> GetByKey(string key, IResolverContext context) | ||
{ | ||
return await context | ||
.BatchDataLoader<string, string>( | ||
(keys, _) => | ||
Task.FromResult<IReadOnlyDictionary<string, string>>( | ||
keys.ToDictionary(t => t, _ => key)), | ||
key) | ||
.LoadAsync("abc", context.RequestAborted); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/HotChocolate/Core/test/Fetching.Tests/InlineCacheDataLoaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Tests; | ||
using HotChocolate.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Fetching; | ||
|
||
public class InlineCacheDataLoaderTests | ||
{ | ||
[Fact] | ||
public async Task LoadWithDifferentDataLoader() | ||
{ | ||
// arrange | ||
IRequestExecutor executor = | ||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.BuildRequestExecutorAsync(); | ||
|
||
// act | ||
var result1 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
var result2 = await executor.ExecuteAsync("{ byKey(key: \"def\") }").ToJsonAsync(); | ||
|
||
// assert | ||
Assert.NotEqual(result1, result2); | ||
} | ||
|
||
[Fact] | ||
public async Task LoadWithSingleKeyDataLoader() | ||
{ | ||
// arrange | ||
IRequestExecutor executor = | ||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.BuildRequestExecutorAsync(); | ||
|
||
// act | ||
var result1 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
var result2 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
|
||
// assert | ||
Assert.Equal(result1, result2); | ||
} | ||
|
||
public class Query | ||
{ | ||
public async Task<string> GetByKey(string key, IResolverContext context) | ||
{ | ||
return await context | ||
.CacheDataLoader<string, string>( | ||
(_, _) => Task.FromResult(key), | ||
key) | ||
.LoadAsync("abc", context.RequestAborted); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/HotChocolate/Core/test/Fetching.Tests/InlineGroupDataLoaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Tests; | ||
using HotChocolate.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Fetching; | ||
|
||
public class InlineGroupDataLoaderTests | ||
{ | ||
[Fact] | ||
public async Task LoadWithDifferentDataLoader() | ||
{ | ||
// arrange | ||
IRequestExecutor executor = | ||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.BuildRequestExecutorAsync(); | ||
|
||
// act | ||
var result1 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
var result2 = await executor.ExecuteAsync("{ byKey(key: \"def\") }").ToJsonAsync(); | ||
|
||
// assert | ||
Assert.NotEqual(result1, result2); | ||
} | ||
|
||
[Fact] | ||
public async Task LoadWithSingleKeyDataLoader() | ||
{ | ||
// arrange | ||
IRequestExecutor executor = | ||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.BuildRequestExecutorAsync(); | ||
|
||
// act | ||
var result1 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
var result2 = await executor.ExecuteAsync("{ byKey(key: \"abc\") }").ToJsonAsync(); | ||
|
||
// assert | ||
Assert.Equal(result1, result2); | ||
} | ||
|
||
public class Query | ||
{ | ||
public async Task<string[]> GetByKey(string key, IResolverContext context) | ||
{ | ||
return await context | ||
.GroupDataLoader<string, string>( | ||
(keys, _) => Task.FromResult(keys.ToLookup(t => t, _ => key)), | ||
key) | ||
.LoadAsync("abc", context.RequestAborted); | ||
} | ||
} | ||
} |