Skip to content

Commit 745189d

Browse files
Copilotxperiandri
andauthored
Simplify SchemaId generation to use GetHashCode
Agent-Logs-Url: https://github.com/fsprojects/FSharp.Data.GraphQL/sessions/70eb1609-2aab-4948-8d69-52c900443420 Co-authored-by: xperiandri <2365592+xperiandri@users.noreply.github.com>
1 parent 443e9e4 commit 745189d

1 file changed

Lines changed: 4 additions & 31 deletions

File tree

src/FSharp.Data.GraphQL.Shared/ValidationResultCache.fs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ namespace FSharp.Data.GraphQL.Validation
33
open FSharp.Data.GraphQL
44
open FSharp.Data.GraphQL.Types.Introspection
55
open System
6-
open System.IO
7-
open System.Security.Cryptography
8-
open System.Text
9-
open System.Text.Encodings.Web
10-
open System.Text.Json
11-
open System.Text.Json.Serialization
126

137
type ValidationResultKey =
148
{ DocumentId : string
@@ -21,35 +15,14 @@ type IValidationResultCache =
2115
abstract GetOrAdd : ValidationResultProducer -> ValidationResultKey -> ValidationResult<GQLProblemDetails>
2216

2317
module SchemaId =
24-
25-
let private formatByteAsLowerHex (value : byte) =
26-
value.ToString("x2", System.Globalization.CultureInfo.InvariantCulture)
27-
28-
// Note: UnsafeRelaxedJsonEscaping is used here only for deterministic hashing,
29-
// not for output to untrusted contexts. The JSON is never exposed externally.
30-
let private jsonOptions = JsonSerializerOptions(
31-
WriteIndented = false,
32-
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
33-
PropertyNamingPolicy = null,
34-
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
35-
)
36-
3718
/// <summary>
38-
/// Computes a deterministic schema identifier from an introspection schema.
19+
/// Computes an in-memory schema identifier from an introspection schema instance.
3920
/// </summary>
40-
/// <param name="introspectionSchema">The introspection schema to hash.</param>
41-
/// <returns>A lowercase hexadecimal SHA-256 hash string that uniquely identifies the schema structure.</returns>
21+
/// <param name="introspectionSchema">The introspection schema.</param>
22+
/// <returns>A string representation of the schema hash code.</returns>
4223
[<CompiledName("FromIntrospectionSchema")>]
4324
let fromIntrospectionSchema (introspectionSchema : IntrospectionSchema) =
44-
use stream = new MemoryStream()
45-
JsonSerializer.Serialize(stream, introspectionSchema, jsonOptions)
46-
// Note: Creating SHA256 instance per call is acceptable since schema ID computation
47-
// happens infrequently (typically once per schema during validation cache key creation)
48-
use sha256 = SHA256.Create()
49-
let hash = sha256.ComputeHash(stream.ToArray())
50-
hash
51-
|> Seq.map formatByteAsLowerHex
52-
|> String.concat ""
25+
introspectionSchema.GetHashCode().ToString(System.Globalization.CultureInfo.InvariantCulture)
5326

5427
/// An in-memory cache for the results of schema/document validations, with a lifetime of 30 seconds.
5528
type MemoryValidationResultCache () =

0 commit comments

Comments
 (0)