Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ID type deserialization fix with field tests #427

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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