-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked the handling of async results within the resolver task (#7152)
- Loading branch information
1 parent
51933a6
commit 07388b4
Showing
131 changed files
with
1,976 additions
and
283 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
58 changes: 58 additions & 0 deletions
58
src/HotChocolate/Core/src/Abstractions/DefaultAsyncEnumerableExecutable.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,58 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using static HotChocolate.ExecutableErrorHelper; | ||
|
||
namespace HotChocolate; | ||
|
||
internal sealed class DefaultAsyncEnumerableExecutable<T>(IAsyncEnumerable<T> source) : Executable<T> | ||
{ | ||
public override object Source => source; | ||
|
||
public override async ValueTask<T?> FirstOrDefaultAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await using var enumerator = source.GetAsyncEnumerator(cancellationToken); | ||
return await enumerator.MoveNextAsync() ? enumerator.Current : default; | ||
} | ||
|
||
public override async ValueTask<T?> SingleOrDefaultAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await using var enumerator = source.GetAsyncEnumerator(cancellationToken); | ||
|
||
if (await enumerator.MoveNextAsync()) | ||
{ | ||
var result = enumerator.Current; | ||
|
||
if (await enumerator.MoveNextAsync()) | ||
{ | ||
throw new GraphQLException(SequenceContainsMoreThanOneElement()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public override async ValueTask<List<T>> ToListAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var result = new List<T>(); | ||
|
||
await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false)) | ||
{ | ||
result.Add(element); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public override async IAsyncEnumerable<T> ToAsyncEnumerable( | ||
[EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false)) | ||
{ | ||
yield return element; | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/HotChocolate/Core/src/Abstractions/DefaultEnumerableExecutable.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,98 @@ | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using static HotChocolate.ExecutableErrorHelper; | ||
|
||
namespace HotChocolate; | ||
|
||
internal sealed class DefaultEnumerableExecutable(IEnumerable source) : IExecutable | ||
{ | ||
public object Source => source; | ||
|
||
public ValueTask<IList> ToListAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var list = new List<object?>(); | ||
|
||
foreach (var item in source) | ||
{ | ||
list.Add(item); | ||
} | ||
|
||
return new ValueTask<IList>(list); | ||
} | ||
|
||
public async IAsyncEnumerable<object?> ToAsyncEnumerable( | ||
[EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
if (source is IAsyncEnumerable<object?> stream) | ||
{ | ||
await foreach (var element in stream.WithCancellation(cancellationToken).ConfigureAwait(false)) | ||
{ | ||
yield return element; | ||
} | ||
} | ||
else | ||
{ | ||
foreach (var item in source) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
} | ||
|
||
public ValueTask<object?> FirstOrDefaultAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var enumerator = source.GetEnumerator(); | ||
|
||
try | ||
{ | ||
return enumerator.MoveNext() | ||
? new ValueTask<object?>(enumerator.Current) | ||
: new ValueTask<object?>(default(object?)); | ||
} | ||
finally | ||
{ | ||
if(enumerator is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
public ValueTask<object?> SingleOrDefaultAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var enumerator = source.GetEnumerator(); | ||
|
||
try | ||
{ | ||
if (enumerator.MoveNext()) | ||
{ | ||
var obj = enumerator.Current; | ||
|
||
if(enumerator.MoveNext()) | ||
{ | ||
throw new GraphQLException(SequenceContainsMoreThanOneElement()); | ||
} | ||
|
||
return new ValueTask<object?>(obj); | ||
} | ||
|
||
return new ValueTask<object?>(default(object?)); | ||
} | ||
finally | ||
{ | ||
if(enumerator is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
public string Print() => Source.ToString() ?? Source.GetType().FullName ?? Source.GetType().Name; | ||
|
||
public override string ToString() => Print(); | ||
} |
Oops, something went wrong.