Skip to content

Commit b013e1b

Browse files
Copilotxperiandri
andcommitted
Add tests for ID scalar with Guid values
Agent-Logs-Url: https://github.com/fsprojects/FSharp.Data.GraphQL/sessions/d2fe5523-e81b-4d08-813e-b7f106345347 Co-authored-by: xperiandri <2365592+xperiandri@users.noreply.github.com>
1 parent 8fcdc11 commit b013e1b

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,73 @@ let ``Execution handles errors: additional error added and when null returned fr
603603
ensureRequestError result <| fun errors ->
604604
result.DocumentId |> notEquals Unchecked.defaultof<int>
605605
errors |> equals expectedErrors
606+
607+
type PersonWithGuidId =
608+
{ Id: Guid
609+
Name: string }
610+
611+
[<Fact>]
612+
let ``Execution handles ID scalar: serializes Guid field via AutoField`` () =
613+
let id1 = Guid.Parse "d6c684d9-aaaa-4e88-bbb2-0bb584f1661d"
614+
let id2 = Guid.Parse "d6c684d9-bbbb-4e88-bbb2-0bb584f1661d"
615+
let people =
616+
[ { Id = id1; Name = "Person A" }
617+
{ Id = id2; Name = "Person B" } ]
618+
619+
let PersonType =
620+
Define.Object<PersonWithGuidId>(
621+
"Person",
622+
[ Define.AutoField("id", IDType)
623+
Define.AutoField("name", StringType) ])
624+
625+
let QueryRoot =
626+
Define.Object("Query",
627+
[ Define.Field("people", ListOf PersonType, fun _ () -> people) ])
628+
629+
let schema = Schema(query = QueryRoot)
630+
let executor = Executor(schema)
631+
632+
let result = sync <| executor.AsyncExecute("{people{id name}}", getMockInputContext)
633+
let expected =
634+
NameValueLookup.ofList [
635+
"people", upcast [
636+
box <| NameValueLookup.ofList [ "id", box (string id1); "name", box "Person A" ]
637+
box <| NameValueLookup.ofList [ "id", box (string id2); "name", box "Person B" ]
638+
]
639+
]
640+
ensureDirect result <| fun data errors ->
641+
empty errors
642+
data |> equals (upcast expected)
643+
644+
[<Fact>]
645+
let ``Execution handles ID scalar: serializes Guid field via explicit Field resolve`` () =
646+
let id1 = Guid.Parse "d6c684d9-aaaa-4e88-bbb2-0bb584f1661d"
647+
let id2 = Guid.Parse "d6c684d9-bbbb-4e88-bbb2-0bb584f1661d"
648+
let people =
649+
[ { Id = id1; Name = "Person A" }
650+
{ Id = id2; Name = "Person B" } ]
651+
652+
let PersonType =
653+
Define.Object<PersonWithGuidId>(
654+
"Person",
655+
[ Define.Field("id", IDType, fun _ (p : PersonWithGuidId) -> string p.Id)
656+
Define.AutoField("name", StringType) ])
657+
658+
let QueryRoot =
659+
Define.Object("Query",
660+
[ Define.Field("people", ListOf PersonType, fun _ () -> people) ])
661+
662+
let schema = Schema(query = QueryRoot)
663+
let executor = Executor(schema)
664+
665+
let result = sync <| executor.AsyncExecute("{people{id name}}", getMockInputContext)
666+
let expected =
667+
NameValueLookup.ofList [
668+
"people", upcast [
669+
box <| NameValueLookup.ofList [ "id", box (string id1); "name", box "Person A" ]
670+
box <| NameValueLookup.ofList [ "id", box (string id2); "name", box "Person B" ]
671+
]
672+
]
673+
ensureDirect result <| fun data errors ->
674+
empty errors
675+
data |> equals (upcast expected)

tests/FSharp.Data.GraphQL.Tests/Variables and Inputs/CoercionTests.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,17 @@ let ``ID coerces input`` () =
112112
// We have no idea that it is an enum
113113
testCoercion IDType "enum" (Variable (JsonDocument.Parse "\"enum\"").RootElement)
114114
testCoercionError IDType "Inline value 'enum' of type enum cannot be converted into identifier" (InlineConstant (EnumValue "enum"))
115+
116+
let private testOutputCoercion graphQLType (expected: 'a) (input: obj) =
117+
let (Scalar scalar) = graphQLType
118+
match scalar.CoerceOutput input with
119+
| Some actual -> equals (box expected) actual
120+
| None -> Assert.Fail $"Expected CoerceOutput of %A{input} to return Some but got None"
121+
122+
[<Fact>]
123+
let ``ID coerces output`` () =
124+
testOutputCoercion IDType "abc" ("abc" : obj)
125+
testOutputCoercion IDType "123" (123 : obj)
126+
testOutputCoercion IDType "123" (123L : obj)
127+
let guid = Guid.Parse "d6c684d9-aaaa-4e88-bbb2-0bb584f1661d"
128+
testOutputCoercion IDType (string guid) (guid : obj)

0 commit comments

Comments
 (0)