Skip to content

Commit

Permalink
Fixed issue where mutation the result object would drop the ContextDa…
Browse files Browse the repository at this point in the history
…ta (#5211)
  • Loading branch information
michaelstaib committed Jul 5, 2022
1 parent 21294eb commit ca8855d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 19 deletions.
Expand Up @@ -25,8 +25,8 @@ public class HttpPostMiddlewareBase : MiddlewareBase
{
RequestParser = requestParser ??
throw new ArgumentNullException(nameof(requestParser));
DiagnosticEvents = diagnosticEvents
?? throw new ArgumentNullException(nameof(diagnosticEvents));
DiagnosticEvents = diagnosticEvents ??
throw new ArgumentNullException(nameof(diagnosticEvents));
}

protected IHttpRequestParser RequestParser { get; }
Expand Down Expand Up @@ -153,15 +153,15 @@ public virtual async Task InvokeAsync(HttpContext context)
// Most GraphQL requests will be of this type where we want to execute
// a single GraphQL query or mutation.
case 1:
{
result = await ExecuteSingleAsync(
context,
requestExecutor,
requestInterceptor,
DiagnosticEvents,
requests[0]);
break;
}
{
result = await ExecuteSingleAsync(
context,
requestExecutor,
requestInterceptor,
DiagnosticEvents,
requests[0]);
break;
}

// if the HTTP request body contains more than one GraphQL request than
// we need to execute a request batch where we need to execute multiple
Expand Down
Expand Up @@ -12,6 +12,8 @@
using Xunit;
using HotChocolate.AspNetCore.Instrumentation;
using System;
using System.Net;
using HotChocolate.Execution.Options;

namespace HotChocolate.AspNetCore;

Expand Down Expand Up @@ -319,6 +321,34 @@ public async Task Aggregate_Diagnostic_All_Listeners_Are_Triggered()
Assert.True(listenerB.Triggered);
}

[Fact]
public async Task Apollo_Tracing_Invalid_Field()
{
// arrange
TestServer server = CreateStarWarsServer(
configureServices: s => s
.AddGraphQLServer()
.AddApolloTracing());

// act
ClientRawResult response = await server.PostRawAsync(
new ClientQueryRequest
{
Query =
@"{
hero123(episode: NEW_HOPE)
{
name
}
}"

},
enableApolloTracing: true);

// assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task Ensure_Multipart_Format_Is_Correct_With_Defer()
{
Expand Down
@@ -1,3 +1,5 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Net;
Expand Down Expand Up @@ -72,7 +74,7 @@ public static class TestServerExtensions
ClientQueryRequest request,
string operationNames,
string path = "/graphql",
Func<string, string> createOperationParameter = null)
Func<string, string>? createOperationParameter = null)
{
createOperationParameter ??= s => "batchOperations=[" + s + "]";
HttpResponseMessage response =
Expand Down Expand Up @@ -159,13 +161,15 @@ public static class TestServerExtensions
public static async Task<ClientRawResult> PostRawAsync(
this TestServer testServer,
ClientQueryRequest request,
string path = "/graphql")
string path = "/graphql",
bool enableApolloTracing = false)
{
HttpResponseMessage response =
await SendPostRequestAsync(
testServer,
JsonConvert.SerializeObject(request),
path);
path,
enableApolloTracing: enableApolloTracing);

if (response.StatusCode == HttpStatusCode.NotFound)
{
Expand Down Expand Up @@ -311,7 +315,7 @@ public static class TestServerExtensions
public static Task<HttpResponseMessage> SendGetRequestAsync(
this TestServer testServer,
string query,
string path = null) =>
string? path = null) =>
testServer.CreateClient().GetAsync($"{CreateUrl(path)}/?{query}");

public static string CreateUrl(string? path)
Expand Down
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection.Metadata;

#nullable enable

Expand Down Expand Up @@ -153,15 +154,28 @@ public static QueryResultBuilder FromResult(IQueryResult result)
builder._errors = new List<IError>(result.Errors);
}

if (result.Extensions is ExtensionData d)
if (result.Extensions is ExtensionData ext)
{
builder._extensionData = new ExtensionData(d);
builder._extensionData = new ExtensionData(ext);
}
else if (result.Extensions is not null)
{
builder._extensionData = new ExtensionData(result.Extensions);
}

if (result.ContextData is ExtensionData cd)
{
builder._contextData = new ExtensionData(cd);
}
else if (result.ContextData is not null)
{
builder._contextData = new ExtensionData(result.ContextData);
}

builder._label = result.Label;
builder._path = result.Path;
builder._hasNext = result.HasNext;

return builder;
}

Expand Down
4 changes: 2 additions & 2 deletions src/HotChocolate/Core/src/Types.Scalars.Upload/StreamFile.cs
Expand Up @@ -62,13 +62,13 @@ public class StreamFile : IFile
CancellationToken cancellationToken = default)
{
#if NETSTANDARD2_0 || NETSTANDARD2_1
using Stream stream = OpenReadStream();
using Stream stream = OpenReadStream();
#else
await using Stream stream = OpenReadStream();
#endif

#if NETSTANDARD2_0
await stream.CopyToAsync(target, 1024, cancellationToken).ConfigureAwait(false);
await stream.CopyToAsync(target, 1024, cancellationToken).ConfigureAwait(false);
#else
await stream.CopyToAsync(target, cancellationToken).ConfigureAwait(false);
#endif
Expand Down

0 comments on commit ca8855d

Please sign in to comment.