Skip to content

Commit 4b73731

Browse files
committed
Fixed crash and added more descriptive exception message
1 parent 0a3b9e6 commit 4b73731

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

samples/star-wars-api/AuthorizationMiddleware.fs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ type internal CustomPolicyFieldDefinition<'Val, 'Res> (source : FieldDef<'Val, '
4949
((%%expr : ResolveFieldContext -> 'Val -> 'Res) ctx input)
5050
|> async.Return)
5151
@>
52-
let compiledResolver = LeafExpressionConverter.EvaluateQuotation resolver
53-
Expr.WithValue (compiledResolver, resolver.Type, resolver)
52+
try
53+
let compiledResolver = LeafExpressionConverter.EvaluateQuotation resolver
54+
Expr.WithValue (compiledResolver, resolver.Type, resolver)
55+
with :? NotSupportedException as ex ->
56+
let message =
57+
$"F# compiler cannot convert '{source.Name}' field resolver expression to LINQ, use function instead"
58+
raise (NotSupportedException (message, ex))
5459

5560
match source.Resolve with
5661
| Sync (input, output, expr) -> Async (input, output, changeSyncResolver expr)
@@ -87,9 +92,10 @@ module TypeSystemExtensions =
8792
|> Seq.where (fun r -> not r.Succeeded)
8893
|> Seq.collect (fun r -> r.Failure.FailedRequirements)
8994

90-
if Seq.isEmpty requirements
91-
then return Ok ()
92-
else return Error "Forbidden"
95+
if Seq.isEmpty requirements then
96+
return Ok ()
97+
else
98+
return Error "Forbidden"
9399
}
94100

95101
[<Literal>]

samples/star-wars-api/Schema.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ module Schema =
277277
let setMoon (ctx : ResolveFieldContext) (_ : Root) = option {
278278
let! planet = getPlanet (ctx.Arg ("id"))
279279
ignore (planet.SetMoon (Some (ctx.Arg ("isMoon"))))
280-
schemaConfig.SubscriptionProvider.Publish<Planet> "watchMoon" planet
281-
schemaConfig.LiveFieldSubscriptionProvider.Publish<Planet> "Planet" "isMoon" planet
280+
ctx.Schema.SubscriptionProvider.Publish<Planet> "watchMoon" planet
281+
ctx.Schema.LiveFieldSubscriptionProvider.Publish<Planet> "Planet" "isMoon" planet
282282
return planet
283283
}
284284
Define.Field(
@@ -290,9 +290,9 @@ module Schema =
290290
// Using complex lambda crashes
291291
//(fun ctx _ -> option {
292292
// let! planet = getPlanet (ctx.Arg ("id"))
293-
// ignore (planet.SetMoon (Some (ctx.Arg ("isMoon"))))
294-
// schemaConfig.SubscriptionProvider.Publish<Planet> "watchMoon" planet
295-
// schemaConfig.LiveFieldSubscriptionProvider.Publish<Planet> "Planet" "isMoon" planet
293+
// let planet = planet.SetMoon (Some (ctx.Arg ("isMoon")))
294+
// ctx.Schema.SubscriptionProvider.Publish<Planet> "watchMoon" planet
295+
// ctx.Schema.LiveFieldSubscriptionProvider.Publish<Planet> "Planet" "isMoon" planet
296296
// return planet
297297
//})
298298
).WithAuthorizationPolicies(Policies.CanSetMoon)

0 commit comments

Comments
 (0)