Skip to content

Commit 2004dab

Browse files
Andrii Chebukinxperiandri
authored andcommitted
Implemented converting Option and ValueOption returned from scalar to the underlying type
1 parent 251cfcc commit 2004dab

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let private wrapOptionalNone (outputType: Type) (inputType: Type) =
2929
else
3030
null
3131

32-
let private wrapOptional (outputType: Type) value=
32+
let private normalizeOptional (outputType: Type) value=
3333
match value with
3434
| null -> wrapOptionalNone outputType typeof<obj>
3535
| value ->
@@ -43,7 +43,17 @@ let private wrapOptional (outputType: Type) value=
4343
let valuesome, _, _ = ReflectionHelper.vOptionOfType expectedOutputType
4444
valuesome value
4545
else
46-
value
46+
let realInputType = inputType.GenericTypeArguments[0]
47+
if inputType.FullName.StartsWith ReflectionHelper.OptionTypeName && outputType.IsAssignableFrom realInputType then
48+
let _, _, getValue = ReflectionHelper.optionOfType realInputType
49+
// none is null so it is already covered above
50+
getValue value
51+
elif inputType.FullName.StartsWith ReflectionHelper.ValueOptionTypeName && outputType.IsAssignableFrom realInputType then
52+
let _, valueNone, getValue = ReflectionHelper.vOptionOfType realInputType
53+
if value = valueNone then null
54+
else getValue value
55+
else
56+
value
4757
else
4858
value
4959

@@ -154,7 +164,7 @@ let rec internal compileByType (inputObjectPath: FieldPath) (inputSource : Input
154164
match Map.tryFind field.Name props with
155165
| None -> Ok <| wrapOptionalNone param.ParameterType field.TypeDef.Type
156166
| Some prop ->
157-
field.ExecuteInput prop variables |> Result.map (wrapOptional param.ParameterType)
167+
field.ExecuteInput prop variables |> Result.map (normalizeOptional param.ParameterType)
158168
|> attachErrorExtensionsIfScalar inputSource inputObjectPath originalInputDef field
159169
| ValueNone -> Ok <| wrapOptionalNone param.ParameterType typeof<obj>)
160170

@@ -181,7 +191,7 @@ let rec internal compileByType (inputObjectPath: FieldPath) (inputSource : Input
181191
field.ExecuteInput (VariableName field.Name) objectFields
182192
// TODO: Take into account variable name
183193
|> attachErrorExtensionsIfScalar inputSource inputObjectPath originalInputDef field
184-
return wrapOptional param.ParameterType value
194+
return normalizeOptional param.ParameterType value
185195
| ValueNone -> return wrapOptionalNone param.ParameterType typeof<obj>
186196
})
187197

@@ -220,7 +230,7 @@ let rec internal compileByType (inputObjectPath: FieldPath) (inputSource : Input
220230
|> splitSeqErrorsList
221231
let mappedValues =
222232
mappedValues
223-
|> Seq.map (wrapOptional innerDef.Type)
233+
|> Seq.map (normalizeOptional innerDef.Type)
224234
|> Seq.toList
225235

226236
if isArray then

0 commit comments

Comments
 (0)