Skip to content

Commit ff6221b

Browse files
committed
Simplified isAssignableWithUnwrap implementation
1 parent 15150f1 commit ff6221b

2 files changed

Lines changed: 16 additions & 17 deletions

File tree

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module internal ReflectionHelper =
149149
ty.GetGenericArguments().[0]
150150
else ty
151151

152-
let isAssignableWithUnwrap (from: Type) (``to``: Type) =
152+
let rec isAssignableWithUnwrap (from: Type) (``to``: Type) =
153153

154154
let checkCollections (from: Type) (``to``: Type) =
155155
if
@@ -176,17 +176,16 @@ module internal ReflectionHelper =
176176
from.GetGenericArguments()[0]
177177
else from
178178
let actualTo =
179-
if ``to``.FullName.StartsWith OptionTypeName || ``to``.FullName.StartsWith ValueOptionTypeName then
179+
if ``to``.FullName.StartsWith OptionTypeName ||
180+
``to``.FullName.StartsWith ValueOptionTypeName
181+
then
180182
``to``.GetGenericArguments()[0]
181183
else ``to``
182184

183185
let result = actualFrom.IsAssignableTo actualTo || checkCollections actualFrom actualTo
184-
if result then result
185-
else
186-
if actualFrom.FullName.StartsWith OptionTypeName || actualFrom.FullName.StartsWith ValueOptionTypeName then
187-
let actualFrom = actualFrom.GetGenericArguments()[0]
188-
actualFrom.IsAssignableTo actualTo || checkCollections actualFrom actualTo
189-
else result
186+
if result then true
187+
elif actualFrom <> from || actualTo <> ``to`` then isAssignableWithUnwrap actualFrom actualTo
188+
else false
190189

191190
let matchConstructor (t: Type) (fields: string []) =
192191
if FSharpType.IsRecord(t, true) then FSharpValue.PreComputeRecordConstructorInfo(t, true)

src/FSharp.Data.GraphQL.Shared/Validation.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ module Ast =
274274
|> ValueOption.map _.TypeCondition
275275
|> ValueOption.defaultValue x.ParentType
276276

277-
let private tryFindInArrayOption (finder : 'T -> bool) = ValueOption.ofOption >> ValueOption.bind (Array.tryFind finder >> ValueOption.ofOption)
277+
let private tryFindInArrayOption (finder : 'T -> bool) =
278+
ValueOption.ofOption
279+
>> ValueOption.bind (Array.tryFind finder >> ValueOption.ofOption)
278280

279281
let private onAllSelections (ctx : ValidationContext) (onSelection : SelectionInfo -> ValidationResult<GQLProblemDetails>) =
280282
let rec traverseSelections selection =
@@ -360,7 +362,9 @@ module Ast =
360362
|> ValueOption.defaultValue List.empty
361363
| FragmentSpread fragSpread ->
362364
voption {
363-
let! fragDef = ctx.FragmentDefinitions |> List.tryFind (fun def -> def.Name.IsSome && def.Name.Value = fragSpread.Name)
365+
let! fragDef =
366+
ctx.FragmentDefinitions
367+
|> List.tryFind (fun def -> def.Name.IsSome && def.Name.Value = fragSpread.Name)
364368
let! typeCondition = fragDef.TypeCondition
365369
let! fragType = ctx.Schema.TryGetTypeByName typeCondition
366370
let fragType = Spread (fragSpread.Name, fragSpread.Directives, fragType)
@@ -898,10 +902,6 @@ module Ast =
898902
|> ValidationResult.collect (checkFragmentSpreadIsPossibleInSelection))
899903

900904
let private checkInputValue (schemaInfo : SchemaInfo) (variables : VariableDefinition list option) (selection : SelectionInfo) =
901-
let rec getFieldMap (fields : (string * IntrospectionTypeRef) seq) : Map<string, IntrospectionTypeRef> =
902-
(Map.empty, fields)
903-
||> Seq.fold (fun acc (name, tref) -> Map.add name tref acc)
904-
905905
let rec checkIsCoercible (tref : IntrospectionTypeRef) (argName : string) (value : InputValue) =
906906
let canNotCoerce =
907907
AstError.AsResult (
@@ -954,8 +954,7 @@ module Ast =
954954
let fieldMap =
955955
itype.InputFields
956956
|> Option.defaultValue [||]
957-
|> Array.map (fun x -> x.Name, x.Type)
958-
|> getFieldMap
957+
|> Array.fold (fun acc inputVal -> Map.add inputVal.Name inputVal.Type acc) Map.empty
959958
let canCoerceFields =
960959
fieldMap
961960
|> ValidationResult.collect (fun kvp ->
@@ -1401,7 +1400,8 @@ module Ast =
14011400
| ValueSome operationName, _ ->
14021401
AstError.AsResult
14031402
$"A variable '$%s{varDef.VariableName}' is not used in operation '%s{operationName}'. Every variable must be used."
1404-
| ValueNone, _ -> AstError.AsResult $"A variable '$%s{varDef.VariableName}' is not used in operation. Every variable must be used.")
1403+
| ValueNone, _ ->
1404+
AstError.AsResult $"A variable '$%s{varDef.VariableName}' is not used in operation. Every variable must be used.")
14051405
| _ -> Success)
14061406

14071407
let rec private areTypesCompatible (variableTypeRef : IntrospectionTypeRef) (locationTypeRef : IntrospectionTypeRef) =

0 commit comments

Comments
 (0)