Skip to content

Commit

Permalink
Add SetUser extension to IQueryRequestBuilder (#6093)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler authored Apr 27, 2023
1 parent fb8e5a2 commit 3dfa4ec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task AuthorizeOnExtension()
QueryRequestBuilder
.New()
.SetQuery("{ bar }")
.AddGlobalState(nameof(ClaimsPrincipal), new ClaimsPrincipal())
.SetUser(new ClaimsPrincipal())
.Create());

result.MatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Security.Claims;

namespace HotChocolate.Execution;

Expand Down Expand Up @@ -84,6 +85,14 @@ public static IQueryRequestBuilder SetMaximumAllowedExecutionDepth(
int maximumAllowedDepth) =>
builder.SetGlobalState(WellKnownContextData.MaxAllowedExecutionDepth, maximumAllowedDepth);

/// <summary>
/// Sets the user for this request.
/// </summary>
public static IQueryRequestBuilder SetUser(
this IQueryRequestBuilder builder,
ClaimsPrincipal claimsPrincipal) =>
builder.SetGlobalState(nameof(ClaimsPrincipal), claimsPrincipal);

/// <summary>
/// Registers a cleanup task for execution resources with the <see cref="IQueryResultBuilder"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ public async Task Assert_UserState_Exists()
}
}
""")
.SetGlobalState(nameof(ClaimsPrincipal), new ClaimsPrincipal()));
.SetUser(new ClaimsPrincipal()));

// assert
Snapshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task GetUserClaims()
.ExecuteRequestAsync(
QueryRequestBuilder.New()
.SetQuery("{ foo }")
.SetGlobalState(nameof(ClaimsPrincipal), user)
.SetUser(user)
.Create())
.MatchSnapshotAsync();
}
Expand Down

0 comments on commit 3dfa4ec

Please sign in to comment.