|
2 | 2 |
|
3 | 3 | namespace HotChocolate;
|
4 | 4 |
|
5 |
| -internal static class AuthorizeSchemaBuilderExtensions |
| 5 | +/// <summary> |
| 6 | +/// Provides extension methods for the schema builder. |
| 7 | +/// </summary> |
| 8 | +public static class AuthorizeSchemaBuilderExtensions |
6 | 9 | {
|
| 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> |
7 | 22 | public static ISchemaBuilder AddAuthorizeDirectiveType(this ISchemaBuilder builder)
|
8 | 23 | {
|
9 | 24 | if (builder is null)
|
10 | 25 | {
|
11 | 26 | throw new ArgumentNullException(nameof(builder));
|
12 | 27 | }
|
13 | 28 |
|
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 | + } |
16 | 43 |
|
17 |
| - return builder |
18 |
| - .AddDirectiveType(authorize) |
19 |
| - .AddDirectiveType(allowAnonymous) |
20 |
| - .TryAddSchemaDirective(authorize) |
21 |
| - .TryAddSchemaDirective(allowAnonymous) |
22 |
| - .TryAddTypeInterceptor<AuthorizationTypeInterceptor>(); |
| 44 | + return builder; |
23 | 45 | }
|
24 | 46 | }
|
0 commit comments