Skip to content

Commit 2437f4e

Browse files
xperiandrihth313
andauthored
ID type deserialization fix with field tests (#427)
* Fixed deserialization of ID type Deserialization of ID types caused an exception if the field was nullable. * Added fields for testing --------- Co-authored-by: Håkan Thörngren <hth313@gmail.com>
1 parent fee841a commit 2437f4e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/FSharp.Data.GraphQL.Client/BaseTypes.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ module internal JsonValueHelper =
348348
| Some "Date" ->
349349
match DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None) with
350350
| (true, d) -> box d
351-
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time sring, but the conversion failed."
351+
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time string, but the conversion failed."
352352
| Some _ ->
353353
box s
354354
| _ -> failwith "A string type was received in the query response item, but the matching schema field is not a string based type."
@@ -357,14 +357,16 @@ module internal JsonValueHelper =
357357
| None -> failwith "Item type is a non null type, but no underlying type exists on the schema definition of the type."
358358
| TypeKind.SCALAR ->
359359
match schemaField.SchemaTypeRef.Name with
360+
| Some "String" | Some "ID" ->
361+
s |> makeSomeIfNeeded
360362
| Some "URI" ->
361-
System.Uri(s) |> makeSomeIfNeeded
363+
s |> System.Uri |> makeSomeIfNeeded
362364
| Some "Date" ->
363365
match DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None) with
364366
| (true, d) -> makeSomeIfNeeded d
365-
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time sring, but the conversion failed."
367+
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time string, but the conversion failed."
366368
| Some _ ->
367-
makeSomeIfNeeded s
369+
s |> makeSomeIfNeeded
368370
| _ -> failwith "A string type was received in the query response item, but the matching schema field is not a string based type."
369371
| TypeKind.ENUM when schemaField.SchemaTypeRef.Name.IsSome -> EnumBase(schemaField.SchemaTypeRef.Name.Value, s) |> makeSomeIfNeeded
370372
| _ -> failwith "A string type was received in the query response item, but the matching schema field is not a string based type or an enum type."

tests/FSharp.Data.GraphQL.IntegrationTests.Server/Schema.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ type InputField =
2020
StringOption : string option
2121
IntOption : int option
2222
Uri : System.Uri
23-
Guid : System.Guid }
23+
Guid : System.Guid
24+
GuidOption : System.Guid option }
2425

2526
type Input =
2627
{ Single : InputField option
@@ -74,6 +75,10 @@ module Schema =
7475
Define.AutoField("intOption", Nullable IntType, description = "An integer option value.")
7576
Define.AutoField("uri", UriType, description = "An URI value.")
7677
Define.AutoField("guid", GuidType, description = "A Guid value.")
78+
Define.Field("guidId", IDType, description = "A Guid Id value.", resolve = fun _ o -> o.Guid |> string)
79+
Define.Field("stringId", IDType, description = "A String Id value.", resolve = fun _ o -> o.String)
80+
Define.Field("guidIdOption", Nullable IDType, description = "A Guid Id value.", resolve = fun _ o -> o.GuidOption |> Option.map string)
81+
Define.Field("stringIdOption", Nullable IDType, description = "A String Id value.", resolve = fun _ o -> o.StringOption)
7782
Define.Field("deprecated", StringType, resolve = (fun _ x -> x.String), description = "A string value through a deprecated field.", deprecationReason = "This field is deprecated.", args = []) ])
7883

7984
let UploadedFileType =

0 commit comments

Comments
 (0)