|
1 | 1 | namespace FSharp.Data.GraphQL.Samples.StarWarsApi |
2 | 2 |
|
| 3 | +open System.Linq |
3 | 4 | open System.Text.Json.Serialization |
| 5 | +open Microsoft.FSharp.Reflection |
4 | 6 | open FSharp.Data.GraphQL |
5 | 7 | open FSharp.Data.GraphQL.Types |
6 | 8 | open FSharp.Data.GraphQL.Server.Relay |
@@ -292,7 +294,27 @@ module Schema = |
292 | 294 | Define.Field ("hero", Nullable HumanType, "Gets human hero", inputs, fun ctx _ -> getHuman (ctx.Arg ("id"))) |
293 | 295 | Define.Field ("droid", Nullable DroidType, "Gets droid", inputs, (fun ctx _ -> getDroid (ctx.Arg ("id")))) |
294 | 296 | Define.Field ("planet", Nullable PlanetType, "Gets planet", inputs, fun ctx _ -> getPlanet (ctx.Arg ("id"))) |
295 | | - Define.Field ("characters", ListOf CharacterType, "Gets characters", (fun _ _ -> characters)) |
| 297 | + // Define discriminator expression for filtering |
| 298 | + let typeFilterOptions = |
| 299 | + let getUnionCaseName (c : Character) = |
| 300 | + match FSharpValue.GetUnionFields (c, typeof<Character>) with |
| 301 | + | case, _ -> case.Name |
| 302 | + ObjectListFilterLinqOptions<Character, string> ((fun c -> getUnionCaseName c), _.Name) |
| 303 | + // The default to compare is the full type name |
| 304 | + //ObjectListFilterLinqOptions<Character, string> (getDiscriminator = _.GetType().FullName) |
| 305 | + Define.Field ( |
| 306 | + "characters", |
| 307 | + ListOf CharacterType, |
| 308 | + "Gets characters", |
| 309 | + (fun ctx _ -> |
| 310 | + let filter = |
| 311 | + match ctx.ExecutionInfo.Kind with |
| 312 | + | ResolveCollection element -> element.ResolveAbstractionFilter ctx.Schema.TypeMap |
| 313 | + | _ -> invalidOp "Invalid execution info kind" |
| 314 | + characters.AsQueryable () |
| 315 | + // Apply filter if less then all union cases are requested |
| 316 | + |> ValueOption.foldBack (fun f q -> q.Apply (f, typeFilterOptions)) filter) |
| 317 | + ) |
296 | 318 | ] |
297 | 319 | ) |
298 | 320 |
|
|
0 commit comments