Skip to content

Commit

Permalink
Introduced Subscribe Resolver for IAsyncEnumerable<T> (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Dec 9, 2019
1 parent 293058b commit c5d4900
Show file tree
Hide file tree
Showing 55 changed files with 1,573 additions and 675 deletions.
1 change: 1 addition & 0 deletions src/Core/Abstractions/Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/Core/Abstractions/EventMessageAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace HotChocolate
{
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class EventMessageAttribute
: Attribute
{
}
}
23 changes: 2 additions & 21 deletions src/Core/Abstractions/Execution/IResponseStream.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace HotChocolate.Execution
{
public interface IResponseStream
: IDisposable
public interface IResponseStream : IAsyncEnumerable<IReadOnlyQueryResult>
{
/// <summary>
/// Defines if this stream is completed.
/// A completed response stream does not yield any new results.
/// </summary>
bool IsCompleted { get; }

/// <summary>
/// Reads the next result from the current response stream.
/// </summary>
Task<IReadOnlyQueryResult> ReadAsync();

/// <summary>
/// Reads the next result from the current response stream.
/// </summary>
Task<IReadOnlyQueryResult> ReadAsync(
CancellationToken cancellationToken);
}
}
8 changes: 8 additions & 0 deletions src/Core/Abstractions/Execution/WellKnownContextData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace HotChocolate.Execution
{
public static class WellKnownContextData
{
public const string Principal = "HotChocolate.Execution.Principal";
public const string EventMessage = "HotChocolate.Execution.EventMessage";
}
}
18 changes: 6 additions & 12 deletions src/Core/Core.Tests/Execution/Batching/BatchQueryExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ query getHuman {
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
await foreach (IReadOnlyQueryResult result in stream)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
Expand Down Expand Up @@ -138,9 +137,8 @@ mutation secondReview {
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
await foreach (IReadOnlyQueryResult result in stream)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
Expand Down Expand Up @@ -214,9 +212,8 @@ foo @export(as: ""b"")
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
await foreach (IReadOnlyQueryResult result in stream)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
Expand Down Expand Up @@ -324,9 +321,8 @@ foo @export(as: ""b"")
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
await foreach (IReadOnlyQueryResult result in stream)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
Expand Down Expand Up @@ -390,9 +386,8 @@ public async Task Add_Value_To_Variable_List()
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
await foreach (IReadOnlyQueryResult result in stream)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
Expand Down Expand Up @@ -464,9 +459,8 @@ public async Task Convert_List_To_Single_Value_With_Converters()
await executor.ExecuteAsync(batch, CancellationToken.None);

var results = new List<IReadOnlyQueryResult>();
while (!stream.IsCompleted)
await foreach (IReadOnlyQueryResult result in stream)
{
IReadOnlyQueryResult result = await stream.ReadAsync();
if (result != null)
{
results.Add(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,15 @@ public async Task SubscribeToReview()
}
}");

IReadOnlyQueryResult eventResult;
IReadOnlyQueryResult eventResult = null;
using (var cts = new CancellationTokenSource(2000))
{
eventResult = await responseStream.ReadAsync();
await foreach (IReadOnlyQueryResult item in
responseStream.WithCancellation(cts.Token))
{
eventResult = item;
break;
}
}

eventResult.MatchSnapshot();
Expand Down
9 changes: 5 additions & 4 deletions src/Core/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.1" />
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<ProjectReference Include="..\Types\Types.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Types\Types.csproj" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.1" />
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit c5d4900

Please sign in to comment.