Skip to content

Commit 32a8f05

Browse files
hth313xperiandri
authored andcommitted
Fix deserialization of ID type
Deserialization of ID types caused an exception if the field was nullable.
1 parent 298b58b commit 32a8f05

File tree

4 files changed

+1602
-3
lines changed

4 files changed

+1602
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,16 @@ module internal JsonValueHelper =
355355
| None -> failwith "Item type is a non null type, but no underlying type exists on the schema definition of the type."
356356
| TypeKind.SCALAR ->
357357
match schemaField.SchemaTypeRef.Name with
358+
| Some "String" | Some "ID" ->
359+
s |> makeSomeIfNeeded
358360
| Some "URI" ->
359-
System.Uri(s) |> makeSomeIfNeeded
361+
s |> System.Uri |> makeSomeIfNeeded
360362
| Some "Date" ->
361363
match DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None) with
362364
| (true, d) -> makeSomeIfNeeded d
363365
| _ -> failwith "A string was received in the query response, and the schema recognizes it as a date and time sring, but the conversion failed."
364366
| Some _ ->
365-
makeSomeIfNeeded s
367+
s |> makeSomeIfNeeded
366368
| _ -> failwith "A string type was received in the query response item, but the matching schema field is not a string based type."
367369
| TypeKind.ENUM when schemaField.SchemaTypeRef.Name.IsSome -> EnumBase(schemaField.SchemaTypeRef.Name.Value, 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 or an enum type."

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="NameValueLookupTests.fs" />
5555
<Compile Include="SubscriptionTests.fs" />
5656
<Compile Include="AstExtensionsTests.fs" />
57+
<Compile Include="ResponseJsonTests.fs" />
5758
<Compile Include="Program.fs" />
5859
</ItemGroup>
5960

@@ -65,6 +66,13 @@
6566
<ItemGroup>
6667
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Shared\FSharp.Data.GraphQL.Shared.fsproj" />
6768
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server\FSharp.Data.GraphQL.Server.fsproj" />
69+
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Client.DesignTime\FSharp.Data.GraphQL.Client.DesignTime.fsproj" />
6870
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server.Middleware\FSharp.Data.GraphQL.Server.Middleware.fsproj" />
6971
</ItemGroup>
70-
</Project>
72+
</Project>
73+
<ItemGroup Condition="$(OS) == Unix">
74+
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
75+
</ItemGroup>
76+
<Import Project="..\..\netfx.props" />
77+
<Import Project="..\..\.paket\Paket.Restore.targets" />
78+
</Project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module FSharp.Data.GraphQL.Tests.ResponseJsonTests
2+
3+
open System
4+
open Xunit
5+
open Helpers
6+
open FSharp.Data.GraphQL
7+
open FSharp.Data.GraphQL.Client
8+
9+
// Local provider should be able to be created from local introspection json file.
10+
// This schema is adjusted to have some less common type names in order to test them.
11+
type Provider = GraphQLProvider<"introspection-altered-types.json">
12+
13+
14+
[<Fact>]
15+
let ``Should be able to parse nullable ID``() =
16+
let op = Provider.Operation<"""query TestQuery {
17+
hero(id:"1000") {
18+
id,
19+
name,
20+
appearsIn,
21+
homePlanet,
22+
friends {
23+
... on Human {
24+
name,
25+
id
26+
}
27+
... on Droid {
28+
name,
29+
id
30+
}
31+
}
32+
}
33+
}""">
34+
let xop = op()
35+
let result1 = xop.ParseResult("""{
36+
"documentId": 2018203290,
37+
"data": {
38+
"hero": {
39+
"id": "1000",
40+
"__typename": "Human",
41+
"name": "Luke Skywalker",
42+
"appearsIn": [
43+
"NewHope",
44+
"Empire",
45+
"Jedi"
46+
],
47+
"homePlanet": "Tatooine",
48+
"friends": [
49+
{
50+
"name": "Han Solo",
51+
"id": "1002",
52+
"__typename": "Human"
53+
},
54+
{
55+
"name": "Leia Organa",
56+
"id": "1003",
57+
"__typename": "Human"
58+
},
59+
{
60+
"name": "C-3PO",
61+
"id": "2000",
62+
"__typename": "Droid"
63+
},
64+
{
65+
"name": "R2-D2",
66+
"id": "2001",
67+
"__typename": "Droid"
68+
}
69+
]
70+
}
71+
}
72+
}""")
73+
()

0 commit comments

Comments
 (0)