Skip to content

Commit 71cd8d6

Browse files
Copilotxperiandri
andcommitted
Optimize SchemaId serialization and improve code formatting
Agent-Logs-Url: https://github.com/fsprojects/FSharp.Data.GraphQL/sessions/fec46b15-afce-40da-85b4-306eae430ce0 Co-authored-by: xperiandri <2365592+xperiandri@users.noreply.github.com>
1 parent 86e53c8 commit 71cd8d6

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

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

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

1013
type ValidationResultKey =
1114
{ DocumentId : string
@@ -18,16 +21,17 @@ type IValidationResultCache =
1821
abstract GetOrAdd : ValidationResultProducer -> ValidationResultKey -> ValidationResult<GQLProblemDetails>
1922

2023
module SchemaId =
24+
2125
let private formatByteAsLowerHex (value : byte) =
2226
value.ToString("x2", System.Globalization.CultureInfo.InvariantCulture)
2327

2428
// Note: UnsafeRelaxedJsonEscaping is used here only for deterministic hashing,
2529
// not for output to untrusted contexts. The JSON is never exposed externally.
2630
let private jsonOptions = JsonSerializerOptions(
2731
WriteIndented = false,
28-
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never,
32+
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
2933
PropertyNamingPolicy = null,
30-
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
34+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
3135
)
3236

3337
/// <summary>
@@ -36,12 +40,13 @@ module SchemaId =
3640
/// <param name="introspectionSchema">The introspection schema to hash.</param>
3741
/// <returns>A lowercase hexadecimal SHA-256 hash string that uniquely identifies the schema structure.</returns>
3842
let fromIntrospectionSchema (introspectionSchema : IntrospectionSchema) =
39-
let json = JsonSerializer.Serialize(introspectionSchema, jsonOptions)
40-
let jsonBytes = Encoding.UTF8.GetBytes json
43+
use stream = new MemoryStream()
44+
JsonSerializer.Serialize(stream, introspectionSchema, jsonOptions)
45+
stream.Position <- 0L
4146
// Note: Creating SHA256 instance per call is acceptable since schema ID computation
4247
// happens infrequently (typically once per schema during validation cache key creation)
4348
use sha256 = SHA256.Create()
44-
let hash = sha256.ComputeHash jsonBytes
49+
let hash = sha256.ComputeHash stream
4550
hash
4651
|> Seq.map formatByteAsLowerHex
4752
|> String.concat ""

0 commit comments

Comments
 (0)