Skip to content

Commit fdb0f4e

Browse files
Copilotxperiandri
andauthored
Address review feedback on literals and documentId hashing
Agent-Logs-Url: https://github.com/fsprojects/FSharp.Data.GraphQL/sessions/5d78ecae-85c3-42af-9ad0-1750b8aa7fff Co-authored-by: xperiandri <2365592+xperiandri@users.noreply.github.com>
1 parent ae74a63 commit fdb0f4e

7 files changed

Lines changed: 18 additions & 1596 deletions

File tree

Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</PackageReference>
1818
<PackageReference Update="FSharp.Control.FusionTasks" Version="2.6.*" />
1919
<PackageReference Update="FSharp.Control.Reactive" Version="6.*" />
20+
<PackageReference Update="FSharp.Data.LiteralProviders" Version="1.0.3" />
2021
<PackageReference Update="FSharp.SystemTextJson" Version="1.*" />
2122
<PackageReference Update="FsToolkit.ErrorHandling" Version="$(FsToolkitVersion)" />
2223
<PackageReference Update="FsToolkit.ErrorHandling.TaskResult" Version="$(FsToolkitVersion)" />

src/FSharp.Data.GraphQL.Server/Executor.fs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,18 @@ type Executor<'Root>(schema: ISchema<'Root>, middlewares : IExecutorMiddleware s
8383
let middlewaresList = Seq.toList middlewares
8484

8585
/// Generates a deterministic document identifier from the canonical query string.
86-
/// The SHA-256 hash is truncated to 32 bits to preserve the existing int32 documentId contract.
86+
/// The full SHA-256 hash is folded into 32 bits to preserve the existing int32 documentId contract.
8787
let getDocumentId (document : Document) =
8888
let canonicalQuery = document.ToQueryString()
8989
let queryBytes = Encoding.UTF8.GetBytes canonicalQuery
9090
let hash = SHA256.HashData queryBytes
91-
BinaryPrimitives.ReadInt32BigEndian(hash.AsSpan(0, 4))
91+
[ 0 .. 7 ]
92+
|> List.fold
93+
(fun acc index ->
94+
let start = index * 4
95+
let hashChunk = BinaryPrimitives.ReadInt32BigEndian(hash.AsSpan(start, 4))
96+
acc ^^^ hashChunk)
97+
0
9298

9399
let rec runMiddlewares (phaseSel : IExecutorMiddleware -> ('ctx -> ('ctx -> 'res) -> 'res) option)
94100
(initialCtx : 'ctx)

tests/FSharp.Data.GraphQL.IntegrationTests/integration-introspection.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"documentId": 1417518537,
2+
"documentId": 1890859023,
33
"data": {
44
"__schema": {
55
"queryType": {

tests/FSharp.Data.GraphQL.IntegrationTests/introspection.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"documentId": 1417518537,
2+
"documentId": 1890859023,
33
"data": {
44
"__schema": {
55
"queryType": {

tests/FSharp.Data.GraphQL.Tests/ExecutionTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,9 @@ let ``Execution when querying returns unique document id with response`` () =
385385
]))
386386
let query = "query Example { a, b, a }"
387387
// Deterministic SHA-256-based documentId for canonical `query Example { a b a }`,
388-
// using the first 4 bytes of the hash as a big-endian int32.
388+
// folding all 32 hash bytes into an int32 via 8 big-endian chunks.
389389
// Computed once via parse + ToQueryString + SHA-256 and kept fixed to catch regressions.
390-
let expectedDocumentId = -2063861555
390+
let expectedDocumentId = 154204461
391391
let result1 = sync <| Executor(schema).AsyncExecute(query, getMockInputContext, { A = "aa"; B = 2 })
392392
let result2 = sync <| Executor(schema).AsyncExecute(query, getMockInputContext, { A = "aa"; B = 2 })
393393
result1.DocumentId |> notEquals Unchecked.defaultof<int>

tests/FSharp.Data.GraphQL.Tests/FSharp.Data.GraphQL.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.Azure.Cosmos" />
19+
<PackageReference Include="FSharp.Data.LiteralProviders" />
1920
<PackageReference Include="Microsoft.NET.Test.Sdk" />
2021
<PackageReference Include="BenchmarkDotNet" />
2122
<PackageReference Include="Validus" />

0 commit comments

Comments
 (0)