|
1 | 1 | namespace FSharp.Data.GraphQL.Validation |
2 | 2 |
|
3 | 3 | open FSharp.Data.GraphQL |
| 4 | +open FSharp.Data.GraphQL.Types.Introspection |
4 | 5 | open System |
| 6 | +open System.Security.Cryptography |
| 7 | +open System.Text |
| 8 | +open System.Text.Json |
5 | 9 |
|
6 | 10 | type ValidationResultKey = |
7 | 11 | { DocumentId : string |
8 | | - SchemaId : int } |
| 12 | + SchemaId : string } |
9 | 13 |
|
10 | 14 | type ValidationResultProducer = |
11 | 15 | unit -> ValidationResult<GQLProblemDetails> |
12 | 16 |
|
13 | 17 | type IValidationResultCache = |
14 | 18 | abstract GetOrAdd : ValidationResultProducer -> ValidationResultKey -> ValidationResult<GQLProblemDetails> |
15 | 19 |
|
| 20 | +module SchemaId = |
| 21 | + let private formatByteAsLowerHex (value : byte) = |
| 22 | + value.ToString("x2", System.Globalization.CultureInfo.InvariantCulture) |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Computes a deterministic schema identifier from an introspection schema. |
| 26 | + /// </summary> |
| 27 | + /// <param name="introspectionSchema">The introspection schema to hash.</param> |
| 28 | + /// <returns>A lowercase hexadecimal SHA-256 hash string that uniquely identifies the schema structure.</returns> |
| 29 | + let fromIntrospectionSchema (introspectionSchema : IntrospectionSchema) = |
| 30 | + let options = JsonSerializerOptions() |
| 31 | + options.WriteIndented <- false |
| 32 | + options.DefaultIgnoreCondition <- System.Text.Json.Serialization.JsonIgnoreCondition.Never |
| 33 | + let json = JsonSerializer.Serialize(introspectionSchema, options) |
| 34 | + let jsonBytes = Encoding.UTF8.GetBytes json |
| 35 | + use sha256 = SHA256.Create() |
| 36 | + let hash = sha256.ComputeHash jsonBytes |
| 37 | + hash |
| 38 | + |> Seq.map formatByteAsLowerHex |
| 39 | + |> String.concat "" |
16 | 40 |
|
17 | 41 | /// An in-memory cache for the results of schema/document validations, with a lifetime of 30 seconds. |
18 | 42 | type MemoryValidationResultCache () = |
|
0 commit comments