Skip to content

Commit c184046

Browse files
committed
Implemented a sample demonstrating approach of patching an entity
1 parent 1fa480e commit c184046

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

samples/star-wars-api/Schema.fs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace FSharp.Data.GraphQL.Samples.StarWarsApi
22

3+
open System.Text.Json.Serialization
34
open FSharp.Data.GraphQL
45
open FSharp.Data.GraphQL.Types
56
open FSharp.Data.GraphQL.Server.Relay
@@ -27,9 +28,15 @@ type Droid =
2728
AppearsIn : Episode list
2829
PrimaryFunction : string option }
2930

31+
type PatchPlanet =
32+
{ Name : string option Skippable
33+
SatelitesCount : int Skippable
34+
IsMoon : bool option Skippable }
35+
3036
type Planet =
3137
{ Id : string
32-
Name : string option
38+
mutable Name : string option
39+
mutable SatelitesCount : int
3340
mutable IsMoon : bool option }
3441

3542
member x.SetMoon b =
@@ -94,12 +101,15 @@ module Schema =
94101
let planets =
95102
[ { Id = "1"
96103
Name = Some "Tatooine"
104+
SatelitesCount = 2
97105
IsMoon = Some false}
98106
{ Id = "2"
99107
Name = Some "Endor"
108+
SatelitesCount = 1
100109
IsMoon = Some true}
101110
{ Id = "3"
102111
Name = Some "Death Star"
112+
SatelitesCount = 0
103113
IsMoon = Some false} ]
104114

105115
let getHuman id = humans |> List.tryFind (fun h -> h.Id = id)
@@ -228,10 +238,22 @@ module Schema =
228238
fields = [
229239
Define.Field ("id", StringType, "The id of the planet", (fun _ p -> p.Id))
230240
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))
231242
Define.Field ("isMoon", Nullable BooleanType, "Is that a moon?", (fun _ p -> p.IsMoon))
232243
]
233244
)
234245

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+
235257
and RootType =
236258
Define.Object<Root> (
237259
name = "Root",
@@ -299,6 +321,21 @@ module Schema =
299321
//).WithAuthorizationPolicies(Policies.CanSetMoon)
300322
// For build verification purposes
301323
).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+
)
302339
]
303340
)
304341

0 commit comments

Comments
 (0)