Skip to content

Commit

Permalink
ID type deserialization fix with field tests (#427)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
xperiandri and hth313 authored Sep 2, 2024
1 parent fee841a commit 2437f4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/FSharp.Data.GraphQL.Client/BaseTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ module internal JsonValueHelper =
| Some "Date" ->
match DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None) with
| (true, d) -> box d
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time sring, but the conversion failed."
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time string, but the conversion failed."
| Some _ ->
box s
| _ -> failwith "A string type was received in the query response item, but the matching schema field is not a string based type."
Expand All @@ -357,14 +357,16 @@ module internal JsonValueHelper =
| None -> failwith "Item type is a non null type, but no underlying type exists on the schema definition of the type."
| TypeKind.SCALAR ->
match schemaField.SchemaTypeRef.Name with
| Some "String" | Some "ID" ->
s |> makeSomeIfNeeded
| Some "URI" ->
System.Uri(s) |> makeSomeIfNeeded
s |> System.Uri |> makeSomeIfNeeded
| Some "Date" ->
match DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None) with
| (true, d) -> makeSomeIfNeeded d
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time sring, but the conversion failed."
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time string, but the conversion failed."
| Some _ ->
makeSomeIfNeeded s
s |> makeSomeIfNeeded
| _ -> failwith "A string type was received in the query response item, but the matching schema field is not a string based type."
| TypeKind.ENUM when schemaField.SchemaTypeRef.Name.IsSome -> EnumBase(schemaField.SchemaTypeRef.Name.Value, s) |> makeSomeIfNeeded
| _ -> 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."
Expand Down
7 changes: 6 additions & 1 deletion tests/FSharp.Data.GraphQL.IntegrationTests.Server/Schema.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type InputField =
StringOption : string option
IntOption : int option
Uri : System.Uri
Guid : System.Guid }
Guid : System.Guid
GuidOption : System.Guid option }

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

let UploadedFileType =
Expand Down

0 comments on commit 2437f4e

Please sign in to comment.