|
1 | 1 | namespace FSharp.Data.GraphQL.Samples.StarWarsApi
|
2 | 2 |
|
| 3 | +open System.Text.Json.Serialization |
3 | 4 | open FSharp.Data.GraphQL
|
4 | 5 | open FSharp.Data.GraphQL.Types
|
5 | 6 | open FSharp.Data.GraphQL.Server.Relay
|
@@ -27,9 +28,15 @@ type Droid =
|
27 | 28 | AppearsIn : Episode list
|
28 | 29 | PrimaryFunction : string option }
|
29 | 30 |
|
| 31 | +type PatchPlanet = |
| 32 | + { Name : string option Skippable |
| 33 | + SatelitesCount : int Skippable |
| 34 | + IsMoon : bool option Skippable } |
| 35 | + |
30 | 36 | type Planet =
|
31 | 37 | { Id : string
|
32 |
| - Name : string option |
| 38 | + mutable Name : string option |
| 39 | + mutable SatelitesCount : int |
33 | 40 | mutable IsMoon : bool option }
|
34 | 41 |
|
35 | 42 | member x.SetMoon b =
|
@@ -94,12 +101,15 @@ module Schema =
|
94 | 101 | let planets =
|
95 | 102 | [ { Id = "1"
|
96 | 103 | Name = Some "Tatooine"
|
| 104 | + SatelitesCount = 2 |
97 | 105 | IsMoon = Some false}
|
98 | 106 | { Id = "2"
|
99 | 107 | Name = Some "Endor"
|
| 108 | + SatelitesCount = 1 |
100 | 109 | IsMoon = Some true}
|
101 | 110 | { Id = "3"
|
102 | 111 | Name = Some "Death Star"
|
| 112 | + SatelitesCount = 0 |
103 | 113 | IsMoon = Some false} ]
|
104 | 114 |
|
105 | 115 | let getHuman id = humans |> List.tryFind (fun h -> h.Id = id)
|
@@ -228,10 +238,22 @@ module Schema =
|
228 | 238 | fields = [
|
229 | 239 | Define.Field ("id", StringType, "The id of the planet", (fun _ p -> p.Id))
|
230 | 240 | Define.Field ("name", Nullable StringType, "The name of the planet.", (fun _ p -> p.Name))
|
| 241 | + Define.Field ("satelitesCount", IntType, "The number of satelites of the planet.", (fun _ p -> p.SatelitesCount)) |
231 | 242 | Define.Field ("isMoon", Nullable BooleanType, "Is that a moon?", (fun _ p -> p.IsMoon))
|
232 | 243 | ]
|
233 | 244 | )
|
234 | 245 |
|
| 246 | + and PatchPlanetType = |
| 247 | + Define.InputObject<PatchPlanet> ( |
| 248 | + name = "InputPatchPlanet", |
| 249 | + description = "A planet in the Star Wars universe.", |
| 250 | + fields = [ |
| 251 | + Define.SkippableInput ("name", Nullable StringType) |
| 252 | + Define.SkippableInput ("satelitesCount", IntType) |
| 253 | + Define.SkippableInput ("isMoon", Nullable BooleanType) |
| 254 | + ] |
| 255 | + ) |
| 256 | + |
235 | 257 | and RootType =
|
236 | 258 | Define.Object<Root> (
|
237 | 259 | name = "Root",
|
@@ -299,6 +321,21 @@ module Schema =
|
299 | 321 | //).WithAuthorizationPolicies(Policies.CanSetMoon)
|
300 | 322 | // For build verification purposes
|
301 | 323 | ).WithAuthorizationPolicies(Policies.Dummy)
|
| 324 | + Define.Field( |
| 325 | + "patchPlanet", |
| 326 | + PlanetType, |
| 327 | + [ Define.Input ("id", StringType); Define.Input ("planet", PatchPlanetType) ], |
| 328 | + resolve = (fun ctx _ -> |
| 329 | + match getPlanet (ctx.Arg ("id")) with |
| 330 | + | None -> raise (GQLMessageException "Planet not found") |
| 331 | + | Some planet -> |
| 332 | + let patch = ctx.Arg<PatchPlanet> "planet" |
| 333 | + patch.Name |> Skippable.iter (fun n -> planet.Name <- n) |
| 334 | + patch.SatelitesCount |> Skippable.iter (fun s -> planet.SatelitesCount <- s) |
| 335 | + patch.IsMoon |> Skippable.iter (fun m -> planet.IsMoon <- m) |
| 336 | + planet |
| 337 | + ) |
| 338 | + ) |
302 | 339 | ]
|
303 | 340 | )
|
304 | 341 |
|
|
0 commit comments