|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using HotChocolate; |
| 3 | +using HotChocolate.Fusion.Configuration; |
| 4 | +using HotChocolate.Validation; |
| 5 | +using HotChocolate.Validation.Options; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | + |
| 8 | +namespace Microsoft.Extensions.DependencyInjection; |
| 9 | + |
| 10 | +public static partial class CoreFusionGatewayBuilderExtensions |
| 11 | +{ |
| 12 | + public static IFusionGatewayBuilder AddValidationVisitor<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>( |
| 13 | + this IFusionGatewayBuilder builder, |
| 14 | + bool isCacheable = true) |
| 15 | + where T : DocumentValidatorVisitor, new() |
| 16 | + { |
| 17 | + ArgumentNullException.ThrowIfNull(builder); |
| 18 | + return ConfigureValidation(builder, (_, b) => b.AddVisitor<T>(isCacheable: isCacheable)); |
| 19 | + } |
| 20 | + |
| 21 | + public static IFusionGatewayBuilder AddValidationVisitor<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>( |
| 22 | + this IFusionGatewayBuilder builder, |
| 23 | + Func<IServiceProvider, ValidationOptions, T> factory, |
| 24 | + bool isCacheable = true) |
| 25 | + where T : DocumentValidatorVisitor |
| 26 | + { |
| 27 | + ArgumentNullException.ThrowIfNull(builder); |
| 28 | + ArgumentNullException.ThrowIfNull(factory); |
| 29 | + |
| 30 | + return ConfigureValidation( |
| 31 | + builder, |
| 32 | + (_, b) => b.AddVisitor(factory, isCacheable: isCacheable)); |
| 33 | + } |
| 34 | + |
| 35 | + public static IFusionGatewayBuilder AddValidationRule<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>( |
| 36 | + this IFusionGatewayBuilder builder) |
| 37 | + where T : class, IDocumentValidatorRule, new() |
| 38 | + { |
| 39 | + ArgumentNullException.ThrowIfNull(builder); |
| 40 | + return ConfigureValidation(builder, (_, b) => b.AddRule<T>()); |
| 41 | + } |
| 42 | + |
| 43 | + public static IFusionGatewayBuilder AddValidationRule<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>( |
| 44 | + this IFusionGatewayBuilder builder, |
| 45 | + Func<IServiceProvider, ValidationOptions, T> factory) |
| 46 | + where T : class, IDocumentValidatorRule |
| 47 | + { |
| 48 | + ArgumentNullException.ThrowIfNull(builder); |
| 49 | + ArgumentNullException.ThrowIfNull(factory); |
| 50 | + |
| 51 | + return ConfigureValidation(builder, (_, b) => b.AddRule(factory)); |
| 52 | + } |
| 53 | + |
| 54 | + public static IFusionGatewayBuilder AddMaxExecutionDepthRule( |
| 55 | + this IFusionGatewayBuilder builder, |
| 56 | + int maxAllowedExecutionDepth, |
| 57 | + bool skipIntrospectionFields = false, |
| 58 | + bool allowRequestOverrides = false, |
| 59 | + Func<IServiceProvider, ValidationOptions, bool>? isEnabled = null) |
| 60 | + { |
| 61 | + ArgumentNullException.ThrowIfNull(builder); |
| 62 | + |
| 63 | + ConfigureValidation( |
| 64 | + builder, |
| 65 | + (_, b) => b.AddMaxExecutionDepthRule( |
| 66 | + maxAllowedExecutionDepth, |
| 67 | + skipIntrospectionFields, |
| 68 | + allowRequestOverrides, |
| 69 | + isEnabled)); |
| 70 | + return builder; |
| 71 | + } |
| 72 | + |
| 73 | + public static IFusionGatewayBuilder DisableIntrospection( |
| 74 | + this IFusionGatewayBuilder builder, |
| 75 | + bool disable = true) |
| 76 | + { |
| 77 | + ArgumentNullException.ThrowIfNull(builder); |
| 78 | + |
| 79 | + return ConfigureValidation( |
| 80 | + builder, |
| 81 | + (_, b) => b.ModifyOptions(o => o.DisableIntrospection = disable)); |
| 82 | + } |
| 83 | + |
| 84 | + public static IFusionGatewayBuilder DisableIntrospection( |
| 85 | + this IFusionGatewayBuilder builder, |
| 86 | + Func<IServiceProvider, ValidationOptions, bool> disable) |
| 87 | + { |
| 88 | + ArgumentNullException.ThrowIfNull(builder); |
| 89 | + ArgumentNullException.ThrowIfNull(disable); |
| 90 | + |
| 91 | + return ConfigureValidation( |
| 92 | + builder, |
| 93 | + (s, b) => b.ModifyOptions(o => o.DisableIntrospection = disable(s, o))); |
| 94 | + } |
| 95 | + |
| 96 | + public static IFusionGatewayBuilder SetMaxAllowedValidationErrors( |
| 97 | + this IFusionGatewayBuilder builder, |
| 98 | + int maxAllowedValidationErrors) |
| 99 | + { |
| 100 | + ArgumentNullException.ThrowIfNull(builder); |
| 101 | + |
| 102 | + ConfigureValidation( |
| 103 | + builder, |
| 104 | + (_, b) => b.ModifyOptions(o => o.MaxAllowedErrors = maxAllowedValidationErrors)); |
| 105 | + |
| 106 | + return builder; |
| 107 | + } |
| 108 | + |
| 109 | + public static IFusionGatewayBuilder SetIntrospectionAllowedDepth( |
| 110 | + this IFusionGatewayBuilder builder, |
| 111 | + ushort maxAllowedOfTypeDepth, |
| 112 | + ushort maxAllowedListRecursiveDepth) |
| 113 | + { |
| 114 | + ArgumentNullException.ThrowIfNull(builder); |
| 115 | + |
| 116 | + ConfigureValidation( |
| 117 | + builder, |
| 118 | + (_, b) => b.ModifyOptions(o => |
| 119 | + { |
| 120 | + o.MaxAllowedOfTypeDepth = maxAllowedOfTypeDepth; |
| 121 | + o.MaxAllowedListRecursiveDepth = maxAllowedListRecursiveDepth; |
| 122 | + })); |
| 123 | + |
| 124 | + return builder; |
| 125 | + } |
| 126 | + |
| 127 | + public static IFusionGatewayBuilder AddMaxAllowedFieldCycleDepthRule( |
| 128 | + this IFusionGatewayBuilder builder, |
| 129 | + ushort? defaultCycleLimit = 3, |
| 130 | + (SchemaCoordinate Coordinate, ushort MaxAllowed)[]? coordinateCycleLimits = null, |
| 131 | + Func<IServiceProvider, ValidationOptions, bool>? isEnabled = null) |
| 132 | + { |
| 133 | + ArgumentNullException.ThrowIfNull(builder); |
| 134 | + |
| 135 | + ConfigureValidation( |
| 136 | + builder, |
| 137 | + (_, b) => b.AddMaxAllowedFieldCycleDepthRule( |
| 138 | + defaultCycleLimit, |
| 139 | + coordinateCycleLimits, |
| 140 | + isEnabled)); |
| 141 | + |
| 142 | + return builder; |
| 143 | + } |
| 144 | + |
| 145 | + public static IFusionGatewayBuilder RemoveMaxAllowedFieldCycleDepthRule( |
| 146 | + this IFusionGatewayBuilder builder) |
| 147 | + { |
| 148 | + ArgumentNullException.ThrowIfNull(builder); |
| 149 | + |
| 150 | + ConfigureValidation(builder, (_, b) => b.RemoveMaxAllowedFieldCycleDepthRule()); |
| 151 | + return builder; |
| 152 | + } |
| 153 | + |
| 154 | + public static IFusionGatewayBuilder ConfigureValidation( |
| 155 | + this IFusionGatewayBuilder builder, |
| 156 | + Action<IServiceProvider, DocumentValidatorBuilder> configure) |
| 157 | + { |
| 158 | + return FusionSetupUtilities.Configure( |
| 159 | + builder, |
| 160 | + options => options.DocumentValidatorBuilderModifiers.Add(configure)); |
| 161 | + } |
| 162 | +} |
0 commit comments