Skip to content

Commit 3070cde

Browse files
authored
Added internal unsafe helper to access the raw GraphQL result data. (#6937)
1 parent defa50a commit 3070cde

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using HotChocolate.Execution.Processing;
3+
using HotChocolate.Resolvers;
4+
5+
namespace HotChocolate.Execution.Internal;
6+
7+
/// <summary>
8+
/// An unsafe class that provides a set of methods to access the
9+
/// underlying data representations of the middleware context.
10+
/// </summary>
11+
public static class MiddlewareContextMarshal
12+
{
13+
/// <summary>
14+
/// Gets access to the result data of an object in the GraphQL execution.
15+
/// ResultData is pooled and writing to it can corrupt the result.
16+
/// Multiple threads might be writing into the result object.
17+
/// </summary>
18+
/// <param name="context">
19+
/// The resolver context.
20+
/// </param>
21+
/// <returns></returns>
22+
public static ObjectResult? GetParentResultUnsafe(IResolverContext context)
23+
{
24+
if (context is null)
25+
{
26+
throw new ArgumentNullException(nameof(context));
27+
}
28+
29+
return context is MiddlewareContext middlewareContext
30+
? middlewareContext.ParentResult
31+
: null;
32+
}
33+
}

src/HotChocolate/Core/src/Execution/Processing/MiddlewareContext.Global.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using HotChocolate.Execution.Properties;

src/HotChocolate/Core/src/Execution/Processing/PathHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace HotChocolate.Execution.Processing;
55

66
internal static class PathHelper
77
{
8+
private const int _initialPathLength = 64;
9+
810
public static Path CreatePathFromContext(ObjectResult parent)
911
=> CreatePath(parent);
1012

@@ -18,7 +20,7 @@ public static Path CreatePathFromContext(ISelection selection, ResultData parent
1820

1921
private static Path CreatePath(ResultData parent, object segmentValue)
2022
{
21-
object[] segments = ArrayPool<object>.Shared.Rent(64);
23+
var segments = ArrayPool<object>.Shared.Rent(_initialPathLength);
2224
segments[0] = segmentValue;
2325
var length = Build(segments, parent);
2426
var path = CreatePath(parent.PatchPath, segments, length);
@@ -28,7 +30,7 @@ private static Path CreatePath(ResultData parent, object segmentValue)
2830

2931
private static Path CreatePath(ResultData parent)
3032
{
31-
var segments = ArrayPool<object>.Shared.Rent(64);
33+
var segments = ArrayPool<object>.Shared.Rent(_initialPathLength);
3234
var length = Build(segments, parent, 0);
3335
var path = CreatePath(parent.PatchPath, segments, length);
3436
ArrayPool<object>.Shared.Return(segments);

0 commit comments

Comments
 (0)