Skip to content

Commit 1865894

Browse files
authored
Added more helpers to handle result data objects. (#6941)
1 parent 0ef04a7 commit 1865894

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/HotChocolate/Core/src/Execution/Internal/MiddlewareContextMarshal.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public static class MiddlewareContextMarshal
1818
/// <param name="context">
1919
/// The resolver context.
2020
/// </param>
21-
/// <returns></returns>
21+
/// <returns>
22+
/// Returns the result data of the current resolver context.
23+
/// </returns>
2224
public static ObjectResult? GetParentResultUnsafe(IResolverContext context)
2325
{
2426
if (context is null)
@@ -30,4 +32,54 @@ public static class MiddlewareContextMarshal
3032
? middlewareContext.ParentResult
3133
: null;
3234
}
35+
36+
/// <summary>
37+
/// Gets the parent result data of the current <paramref name="resultData"/>.
38+
/// </summary>
39+
/// <param name="resultData">
40+
/// The result data for which to get the parent.
41+
/// </param>
42+
/// <typeparam name="T">
43+
/// The type of the result data.
44+
/// </typeparam>
45+
/// <returns>
46+
/// Returns the parent result data of the current <paramref name="resultData"/>.
47+
/// </returns>
48+
/// <exception cref="ArgumentNullException">
49+
/// Throws if <paramref name="resultData"/> is <c>null</c>.
50+
/// </exception>
51+
public static ResultData? GetParent<T>(T resultData) where T : ResultData
52+
{
53+
if (resultData == null)
54+
{
55+
throw new ArgumentNullException(nameof(resultData));
56+
}
57+
58+
return resultData.Parent;
59+
}
60+
61+
/// <summary>
62+
/// Gets the index under which the <paramref name="resultData"/> is stored in the parent result.
63+
/// </summary>
64+
/// <param name="resultData">
65+
/// The result data for which to get the parent index.
66+
/// </param>
67+
/// <typeparam name="T">
68+
/// The type of the result data.
69+
/// </typeparam>
70+
/// <returns>
71+
/// Returns the index under which the <paramref name="resultData"/> is stored in the parent result.
72+
/// </returns>
73+
/// <exception cref="ArgumentNullException">
74+
/// Throws if <paramref name="resultData"/> is <c>null</c>.
75+
/// </exception>
76+
public static int GetParentIndex<T>(T resultData) where T : ResultData
77+
{
78+
if (resultData == null)
79+
{
80+
throw new ArgumentNullException(nameof(resultData));
81+
}
82+
83+
return resultData.ParentIndex;
84+
}
3385
}

0 commit comments

Comments
 (0)