Skip to content

Commit c2b4542

Browse files
committed
Implemented collections check
1 parent 7a9b21d commit c2b4542

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/FSharp.Data.GraphQL.Server/ReflectionHelper.fs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ module internal ReflectionHelper =
132132

133133
let [<Literal>] OptionTypeName = "Microsoft.FSharp.Core.FSharpOption`1"
134134
let [<Literal>] ValueOptionTypeName = "Microsoft.FSharp.Core.FSharpValueOption`1"
135+
let [<Literal>] ListTypeName = "Microsoft.FSharp.Collections.FSharpList`1"
136+
let [<Literal>] ArrayTypeName = "System.Array`1"
137+
let [<Literal>] IEnumerableTypeName = "System.Collections.IEnumerable"
138+
let [<Literal>] IEnumerableGenericTypeName = "System.Collections.Generic.IEnumerable`1"
135139

136140
let isParameterOptional (p: ParameterInfo) =
137141
p.IsOptional
@@ -146,20 +150,42 @@ module internal ReflectionHelper =
146150
else ty
147151

148152
let isAssignableWithUnwrap (from: Type) (``to``: Type) =
153+
154+
let checkCollections (from: Type) (``to``: Type) =
155+
if
156+
// TODO: Implement support of other types of collections using collection initializers
157+
(``to``.FullName.StartsWith ListTypeName || ``to``.FullName.StartsWith ArrayTypeName)
158+
&& (from.IsGenericType
159+
&& from.GenericTypeArguments[0].IsAssignableTo(``to``.GenericTypeArguments[0])
160+
&& from.GetInterfaces()
161+
|> Array.exists (
162+
fun i -> i.FullName.StartsWith IEnumerableGenericTypeName
163+
|| i.FullName = IEnumerableTypeName
164+
)
165+
)
166+
167+
then
168+
let fromType = from.GetGenericArguments()[0]
169+
let toType = ``to``.GetGenericArguments()[0]
170+
fromType.IsAssignableTo toType
171+
else
172+
false
173+
149174
let from =
150175
if from.FullName.StartsWith OptionTypeName || from.FullName.StartsWith ValueOptionTypeName then
151-
from.GetGenericArguments().[0]
176+
from.GetGenericArguments()[0]
152177
else from
153178
let ``to`` =
154179
if ``to``.FullName.StartsWith OptionTypeName || ``to``.FullName.StartsWith ValueOptionTypeName then
155-
``to``.GetGenericArguments().[0]
180+
``to``.GetGenericArguments()[0]
156181
else ``to``
157182

158-
let result = from.IsAssignableTo ``to``
183+
let result = from.IsAssignableTo ``to`` || checkCollections from ``to``
159184
if result then result
160185
else
161186
if from.FullName.StartsWith OptionTypeName || from.FullName.StartsWith ValueOptionTypeName then
162-
from.GetGenericArguments().[0].IsAssignableTo ``to``
187+
let from = from.GetGenericArguments()[0]
188+
from.IsAssignableTo ``to`` || checkCollections from ``to``
163189
else result
164190

165191
let matchConstructor (t: Type) (fields: string []) =

0 commit comments

Comments
 (0)