|
| 1 | +using HotChocolate.Execution.Relay; |
| 2 | +using HotChocolate.Types.Relay; |
| 3 | + |
| 4 | +namespace HotChocolate.Execution.Options; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Configuration options for GraphQL Global ID serialization behavior. |
| 8 | +/// </summary> |
| 9 | +public struct NodeIdSerializerOptions |
| 10 | +{ |
| 11 | + private int _maxIdLength = 1024; |
| 12 | + private int _maxCachedTypeNames = 1024; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Initializes a new instance of the <see cref="NodeIdSerializerOptions"/> struct |
| 16 | + /// with default values. |
| 17 | + /// </summary> |
| 18 | + public NodeIdSerializerOptions() |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Gets or sets the maximum allowed length in characters for a formatted Global ID string. |
| 24 | + /// </summary> |
| 25 | + /// <value> |
| 26 | + /// The maximum length of a Global ID string. Default is 1024 characters. |
| 27 | + /// </value> |
| 28 | + /// <remarks> |
| 29 | + /// <para> |
| 30 | + /// This limit prevents potential denial-of-service attacks through extremely large ID strings |
| 31 | + /// and guards against memory exhaustion during ID parsing operations. |
| 32 | + /// </para> |
| 33 | + /// <para> |
| 34 | + /// Consider increasing this value for applications with complex composite IDs, long type names, |
| 35 | + /// or when using less efficient encoding formats. The actual memory usage depends on the |
| 36 | + /// chosen <see cref="Format"/>: |
| 37 | + /// </para> |
| 38 | + /// <list type="bullet"> |
| 39 | + /// <item><description>Base64: ~33% larger than original data</description></item> |
| 40 | + /// <item><description>Hex: 100% larger than original data</description></item> |
| 41 | + /// <item><description>Base36: Variable size, generally larger than Base64</description></item> |
| 42 | + /// </list> |
| 43 | + /// </remarks> |
| 44 | + /// <exception cref="ArgumentOutOfRangeException"> |
| 45 | + /// Thrown when set to a value less than 128 characters. |
| 46 | + /// </exception> |
| 47 | + public int MaxIdLength |
| 48 | + { |
| 49 | + readonly get => _maxIdLength; |
| 50 | + set |
| 51 | + { |
| 52 | + if (value < 128) |
| 53 | + { |
| 54 | + throw new ArgumentOutOfRangeException( |
| 55 | + nameof(value), |
| 56 | + value, |
| 57 | + "MaxIdLength must be at least 128."); |
| 58 | + } |
| 59 | + |
| 60 | + _maxIdLength = value; |
| 61 | + } |
| 62 | + } |
| 63 | + /// <summary> |
| 64 | + /// Gets or sets a value indicating whether to use the new Hot Chocolate Global ID format. |
| 65 | + /// </summary> |
| 66 | + /// <value> |
| 67 | + /// <c>true</c> to use the new format; <c>false</c> to use the legacy format. |
| 68 | + /// Default is <c>true</c>. |
| 69 | + /// </value> |
| 70 | + /// <remarks> |
| 71 | + /// <para> |
| 72 | + /// The new format uses a simple delimiter (":") between type name and internal ID: |
| 73 | + /// <c>TypeName:InternalId</c> |
| 74 | + /// </para> |
| 75 | + /// <para> |
| 76 | + /// The legacy format includes additional type indicator bytes for backward compatibility: |
| 77 | + /// <c>TypeName\n[TypeCode]InternalId</c> |
| 78 | + /// </para> |
| 79 | + /// <para> |
| 80 | + /// New applications should use the new simpler format (<c>true</c>) for better performance |
| 81 | + /// and smaller encoded identifier sizes. Set to <c>false</c> only when compatibility with |
| 82 | + /// older Hot Chocolate versions is required. |
| 83 | + /// </para> |
| 84 | + /// </remarks> |
| 85 | + public bool OutputNewIdFormat { get; set; } = true; |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Gets or sets the encoding format used for Global ID serialization. |
| 89 | + /// </summary> |
| 90 | + /// <value> |
| 91 | + /// A <see cref="NodeIdSerializerFormat"/> value specifying the encoding format. |
| 92 | + /// Default is <see cref="NodeIdSerializerFormat.UrlSafeBase64"/>. |
| 93 | + /// </value> |
| 94 | + /// <remarks> |
| 95 | + /// <para>Available formats:</para> |
| 96 | + /// <list type="table"> |
| 97 | + /// <listheader> |
| 98 | + /// <term>Format</term> |
| 99 | + /// <description>Characteristics</description> |
| 100 | + /// </listheader> |
| 101 | + /// <item> |
| 102 | + /// <term><see cref="NodeIdSerializerFormat.Base64"/></term> |
| 103 | + /// <description>Standard Base64 encoding. Compact but contains URL-unsafe characters (+, /, =)</description> |
| 104 | + /// </item> |
| 105 | + /// <item> |
| 106 | + /// <term><see cref="NodeIdSerializerFormat.UrlSafeBase64"/></term> |
| 107 | + /// <description>URL-safe Base64 (- and _ instead of + and /). Recommended for web applications</description> |
| 108 | + /// </item> |
| 109 | + /// <item> |
| 110 | + /// <term><see cref="NodeIdSerializerFormat.UpperHex"/></term> |
| 111 | + /// <description>Uppercase hexadecimal. Human-readable, larger than Base64</description> |
| 112 | + /// </item> |
| 113 | + /// <item> |
| 114 | + /// <term><see cref="NodeIdSerializerFormat.LowerHex"/></term> |
| 115 | + /// <description>Lowercase hexadecimal. Human-readable, larger than Base64</description> |
| 116 | + /// </item> |
| 117 | + /// <item> |
| 118 | + /// <term><see cref="NodeIdSerializerFormat.Base36"/></term> |
| 119 | + /// <description>Mathematical Base36 encoding (0-9, A-Z). Case-insensitive, preserves trailing zeros</description> |
| 120 | + /// </item> |
| 121 | + /// </list> |
| 122 | + /// <para> |
| 123 | + /// Performance considerations: |
| 124 | + /// Base64 formats offer the best size/performance ratio for most applications. |
| 125 | + /// Hex formats are more human-readable but produce larger output. |
| 126 | + /// Base36 provides mathematical properties useful for numeric-heavy IDs. |
| 127 | + /// </para> |
| 128 | + /// </remarks> |
| 129 | + public NodeIdSerializerFormat Format { get; set; } = NodeIdSerializerFormat.UrlSafeBase64; |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Gets or sets the maximum number of type names to cache for improved performance. |
| 133 | + /// </summary> |
| 134 | + /// <value> |
| 135 | + /// The maximum number of type name byte arrays to cache. Default is 1024. |
| 136 | + /// Minimum value is 128. |
| 137 | + /// </value> |
| 138 | + /// <remarks> |
| 139 | + /// <para> |
| 140 | + /// The serializer caches UTF-8 encoded type names to avoid repeated string-to-byte |
| 141 | + /// conversions during Global ID formatting. This significantly improves performance |
| 142 | + /// for frequently used type names. |
| 143 | + /// </para> |
| 144 | + /// <para> |
| 145 | + /// Memory usage: Each cached entry stores the type name as a UTF-8 byte array. |
| 146 | + /// For typical GraphQL type names (5-20 characters), this uses roughly 10-40 bytes |
| 147 | + /// per entry plus caching overhead. |
| 148 | + /// </para> |
| 149 | + /// <para> |
| 150 | + /// Tuning guidelines: |
| 151 | + /// </para> |
| 152 | + /// <list type="bullet"> |
| 153 | + /// <item><description>Small schemas (<50 types): 128-256</description></item> |
| 154 | + /// <item><description>Medium schemas (50-200 types): 512-1024</description></item> |
| 155 | + /// <item><description>Large schemas (>200 types): 1024-2048</description></item> |
| 156 | + /// <item><description>Dynamic schemas: Consider higher values</description></item> |
| 157 | + /// </list> |
| 158 | + /// </remarks> |
| 159 | + /// <exception cref="ArgumentOutOfRangeException"> |
| 160 | + /// Thrown when set to a value less than 128 items. |
| 161 | + /// </exception> |
| 162 | + public int MaxCachedTypeNames |
| 163 | + { |
| 164 | + readonly get => _maxCachedTypeNames; |
| 165 | + set |
| 166 | + { |
| 167 | + if (value < 128) |
| 168 | + { |
| 169 | + throw new ArgumentOutOfRangeException( |
| 170 | + nameof(value), |
| 171 | + value, |
| 172 | + "MaxCachedTypeNames must be at least 128."); |
| 173 | + } |
| 174 | + |
| 175 | + _maxCachedTypeNames = value; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments