Skip to content

Commit 307c058

Browse files
committed
Made AddAuthorizeDirectiveType public for customizations. (#7836)
1 parent 63ab9f3 commit 307c058

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/HotChocolate/Core/src/Abstractions/WellKnownContextData.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,9 @@ public static class WellKnownContextData
338338
/// The key to determine whether the request is a warmup request.
339339
/// </summary>
340340
public const string IsWarmupRequest = "HotChocolate.AspNetCore.Warmup.IsWarmupRequest";
341+
342+
/// <summary>
343+
/// The key to determine whether the @authorize directive was already registered.
344+
/// </summary>
345+
public const string AreAuthorizeDirectivesRegistered = "HotChocolate.Authorization.AuthDirectivesRegistered";
341346
}

src/HotChocolate/Core/src/Authorization/Extensions/AuthorizeSchemaBuilderExtensions.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,45 @@
22

33
namespace HotChocolate;
44

5-
internal static class AuthorizeSchemaBuilderExtensions
5+
/// <summary>
6+
/// Provides extension methods for the schema builder.
7+
/// </summary>
8+
public static class AuthorizeSchemaBuilderExtensions
69
{
10+
/// <summary>
11+
/// Adds the authorize directive types to the schema.
12+
/// </summary>
13+
/// <param name="builder">
14+
/// The schema builder.
15+
/// </param>
16+
/// <returns>
17+
/// Returns the schema builder for configuration chaining.
18+
/// </returns>
19+
/// <exception cref="ArgumentNullException">
20+
/// The <paramref name="builder"/> is <c>null</c>.
21+
/// </exception>
722
public static ISchemaBuilder AddAuthorizeDirectiveType(this ISchemaBuilder builder)
823
{
924
if (builder is null)
1025
{
1126
throw new ArgumentNullException(nameof(builder));
1227
}
1328

14-
var authorize = new AuthorizeDirectiveType();
15-
var allowAnonymous = new AllowAnonymousDirectiveType();
29+
if (!builder.ContextData.ContainsKey(WellKnownContextData.AreAuthorizeDirectivesRegistered))
30+
{
31+
var authorize = new AuthorizeDirectiveType();
32+
var allowAnonymous = new AllowAnonymousDirectiveType();
33+
34+
builder
35+
.AddDirectiveType(authorize)
36+
.AddDirectiveType(allowAnonymous)
37+
.TryAddSchemaDirective(authorize)
38+
.TryAddSchemaDirective(allowAnonymous)
39+
.TryAddTypeInterceptor<AuthorizationTypeInterceptor>();
40+
41+
builder.SetContextData(WellKnownContextData.AreAuthorizeDirectivesRegistered, true);
42+
}
1643

17-
return builder
18-
.AddDirectiveType(authorize)
19-
.AddDirectiveType(allowAnonymous)
20-
.TryAddSchemaDirective(authorize)
21-
.TryAddSchemaDirective(allowAnonymous)
22-
.TryAddTypeInterceptor<AuthorizationTypeInterceptor>();
44+
return builder;
2345
}
2446
}

0 commit comments

Comments
 (0)