@@ -30,12 +30,12 @@ type Planet =
3030 { Id : string
3131 Name : string option
3232 mutable IsMoon : bool option }
33+
3334 member x.SetMoon b =
3435 x.IsMoon <- b
3536 x
3637
37- type Root =
38- { RequestId: string }
38+ type Root = { RequestId : string }
3939
4040type Character =
4141 | Human of Human
@@ -86,6 +86,7 @@ module Schema =
8686 seq {
8787 for h in humans do
8888 yield h.Id, Human h
89+
8990 for d in droids do
9091 yield d.Id, Droid d
9192 }
@@ -100,171 +101,198 @@ module Schema =
100101 IsMoon = Some true }
101102 { Id = " 3"
102103 Name = Some " Death Star"
103- IsMoon = Some false }]
104+ IsMoon = Some false } ]
104105
105- let getHuman id =
106- humans |> List.tryFind ( fun h -> h.Id = id)
106+ let getHuman id = humans |> List.tryFind ( fun h -> h.Id = id)
107107
108- let getDroid id =
109- droids |> List.tryFind ( fun d -> d.Id = id)
108+ let getDroid id = droids |> List.tryFind ( fun d -> d.Id = id)
110109
111- let getPlanet id =
112- planets |> List.tryFind ( fun p -> p.Id = id)
110+ let getPlanet id = planets |> List.tryFind ( fun p -> p.Id = id)
113111
114- let characters =
115- ( humans |> List.map Human) @ ( droids |> List.map Droid)
112+ let characters = ( humans |> List.map Human) @ ( droids |> List.map Droid)
116113
117- let matchesId id = function
114+ let matchesId id =
115+ function
118116 | Human h -> h.Id = id
119117 | Droid d -> d.Id = id
120118
121- let getCharacter id =
122- characters |> List.tryFind ( matchesId id)
119+ let getCharacter id = characters |> List.tryFind ( matchesId id)
123120
124121 let EpisodeType =
125- Define.Enum(
122+ Define.Enum (
126123 name = " Episode" ,
127124 description = " One of the films in the Star Wars Trilogy." ,
128- options = [
129- Define.EnumValue( " NewHope" , Episode.NewHope, " Released in 1977." )
130- Define.EnumValue( " Empire" , Episode.Empire, " Released in 1980." )
131- Define.EnumValue( " Jedi" , Episode.Jedi, " Released in 1983." ) ])
125+ options =
126+ [ Define.EnumValue ( " NewHope" , Episode.NewHope, " Released in 1977." )
127+ Define.EnumValue ( " Empire" , Episode.Empire, " Released in 1980." )
128+ Define.EnumValue ( " Jedi" , Episode.Jedi, " Released in 1983." ) ]
129+ )
132130
133131 let rec CharacterType =
134- Define.Union(
132+ Define.Union (
135133 name = " Character" ,
136134 description = " A character in the Star Wars Trilogy." ,
137135 options = [ HumanType; DroidType ],
138- resolveValue = ( fun o ->
139- match o with
140- | Human h -> box h
141- | Droid d -> upcast d),
142- resolveType = ( fun o ->
143- match o with
144- | Human _ -> upcast HumanType
145- | Droid _ -> upcast DroidType))
136+ resolveValue =
137+ ( fun o ->
138+ match o with
139+ | Human h -> box h
140+ | Droid d -> upcast d),
141+ resolveType =
142+ ( fun o ->
143+ match o with
144+ | Human _ -> upcast HumanType
145+ | Droid _ -> upcast DroidType)
146+ )
146147
147148 and HumanType : ObjectDef < Human > =
148- Define.Object< Human>(
149+ Define.Object< Human> (
149150 name = " Human" ,
150151 description = " A humanoid creature in the Star Wars universe." ,
151152 isTypeOf = ( fun o -> o :? Human),
152- fieldsFn = fun () ->
153- [
154- Define.Field( " id" , String, " The id of the human." , fun _ ( h : Human ) -> h.Id)
155- Define.Field( " name" , Nullable String, " The name of the human." , fun _ ( h : Human ) -> h.Name)
156- Define.Field( " friends" ,
157- ConnectionOf CharacterType,
158- " The friends of the human, or an empty list if they have none." ,
159- Connection.allArgs,
160- fun ctx human ->
161- let totalCount = human.Friends.Length
162- let friends , hasNextPage =
163- match ctx with
164- | SliceInfo( Forward( n, after)) ->
165- match after with
166- | Some ( GlobalId( " Friend" , id)) ->
167- let i = human.Friends |> List.indexed |> List.pick ( fun ( i , e ) -> if e = id then Some i else None)
168- human.Friends |> List.skip ( i+ 1 ) |> List.take n,
169- i+ 1 + n < totalCount
170- | None ->
171- human.Friends |> List.take n,
172- n < totalCount
173- | _ -> failwithf " Cursor %A is not a Friend's global id" after
174- | _ -> human.Friends, false
175- let edges = friends |> Seq.map ( fun b -> { Cursor = toGlobalId " Friend" ( string b); Node = characterMap[ b] }) |> Seq.toList
176- let headCursor = edges |> List.tryHead |> Option.map ( fun edge -> edge.Cursor)
177- let pi = { HasNextPage = hasNextPage; EndCursor = headCursor; StartCursor = None; HasPreviousPage = false }
178- let con = { TotalCount = Some totalCount; PageInfo = pi; Edges = edges }
179- con
180- )
181- Define.Field( " appearsIn" , ListOf EpisodeType, " Which movies they appear in." , fun _ ( h : Human ) -> h.AppearsIn)
182- Define.Field( " homePlanet" , Nullable String, " The home planet of the human, or null if unknown." , fun _ h -> h.HomePlanet)
183- ])
153+ fieldsFn =
154+ fun () ->
155+ [ Define.Field ( " id" , String, " The id of the human." , ( fun _ ( h : Human ) -> h.Id))
156+ Define.Field ( " name" , Nullable String, " The name of the human." , ( fun _ ( h : Human ) -> h.Name))
157+ Define.Field (
158+ " friends" ,
159+ ConnectionOf CharacterType,
160+ " The friends of the human, or an empty list if they have none." ,
161+ Connection.allArgs,
162+ fun ctx human ->
163+ let totalCount = human.Friends.Length
164+
165+ let friends , hasNextPage =
166+ match ctx with
167+ | SliceInfo ( Forward ( n, after)) ->
168+ match after with
169+ | Some ( GlobalId ( " Friend" , id)) ->
170+ let i =
171+ human.Friends
172+ |> List.indexed
173+ |> List.pick ( fun ( i , e ) -> if e = id then Some i else None)
174+
175+ human.Friends |> List.skip ( i + 1 ) |> List.take n, i + 1 + n < totalCount
176+ | None -> human.Friends |> List.take n, n < totalCount
177+ | _ -> failwithf " Cursor %A is not a Friend's global id" after
178+ | _ -> human.Friends, false
179+
180+ let edges =
181+ friends
182+ |> Seq.map ( fun b -> { Cursor = toGlobalId " Friend" ( string b); Node = characterMap[ b] })
183+ |> Seq.toList
184+
185+ let headCursor =
186+ edges
187+ |> List.tryHead
188+ |> Option.map ( fun edge -> edge.Cursor)
189+
190+ let pi =
191+ { HasNextPage = hasNextPage
192+ EndCursor = headCursor
193+ StartCursor = None
194+ HasPreviousPage = false }
195+
196+ let con = { TotalCount = Some totalCount; PageInfo = pi; Edges = edges }
197+ con
198+ )
199+ Define.Field ( " appearsIn" , ListOf EpisodeType, " Which movies they appear in." , ( fun _ ( h : Human ) -> h.AppearsIn))
200+ Define.Field ( " homePlanet" , Nullable String, " The home planet of the human, or null if unknown." , ( fun _ h -> h.HomePlanet)) ]
201+ )
184202
185203 and DroidType =
186- Define.Object< Droid>(
204+ Define.Object< Droid> (
187205 name = " Droid" ,
188206 description = " A mechanical creature in the Star Wars universe." ,
189207 isTypeOf = ( fun o -> o :? Droid),
190- fieldsFn = fun () ->
191- [
192- Define.Field( " id" , String, " The id of the droid." , fun _ ( d : Droid ) -> d.Id)
193- Define.Field( " name" , Nullable String, " The name of the Droid." , fun _ ( d : Droid ) -> d.Name)
194- Define.Field( " friends" , ListOf ( Nullable CharacterType), " The friends of the Droid, or an empty list if they have none." ,
195- fun _ ( d : Droid ) -> d.Friends |> List.map getCharacter |> List.toSeq) .WithQueryWeight( 0.5 )
196- Define.Field( " appearsIn" , ListOf EpisodeType, " Which movies they appear in." , fun _ d -> d.AppearsIn)
197- Define.Field( " primaryFunction" , Nullable String, " The primary function of the droid." , fun _ d -> d.PrimaryFunction)
198- ])
208+ fieldsFn =
209+ fun () ->
210+ [ Define.Field ( " id" , String, " The id of the droid." , ( fun _ ( d : Droid ) -> d.Id))
211+ Define.Field ( " name" , Nullable String, " The name of the Droid." , ( fun _ ( d : Droid ) -> d.Name))
212+ Define
213+ .Field(
214+ " friends" ,
215+ ListOf ( Nullable CharacterType),
216+ " The friends of the Droid, or an empty list if they have none." ,
217+ fun _ ( d : Droid ) -> d.Friends |> List.map getCharacter |> List.toSeq
218+ )
219+ .WithQueryWeight ( 0.5 )
220+ Define.Field ( " appearsIn" , ListOf EpisodeType, " Which movies they appear in." , ( fun _ d -> d.AppearsIn))
221+ Define.Field ( " primaryFunction" , Nullable String, " The primary function of the droid." , ( fun _ d -> d.PrimaryFunction)) ]
222+ )
199223
200224 and PlanetType =
201- Define.Object< Planet>(
225+ Define.Object< Planet> (
202226 name = " Planet" ,
203227 description = " A planet in the Star Wars universe." ,
204228 isTypeOf = ( fun o -> o :? Planet),
205- fieldsFn = fun () ->
206- [
207- Define.Field( " id" , String, " The id of the planet" , fun _ p -> p.Id)
208- Define.Field( " name" , Nullable String, " The name of the planet." , fun _ p -> p.Name)
209- Define.Field( " isMoon" , Nullable Boolean, " Is that a moon?" , fun _ p -> p.IsMoon)
210- ] )
229+ fieldsFn =
230+ fun () ->
231+ [ Define.Field ( " id" , String, " The id of the planet" , ( fun _ p -> p.Id) )
232+ Define.Field ( " name" , Nullable String, " The name of the planet." , ( fun _ p -> p.Name) )
233+ Define.Field ( " isMoon" , Nullable Boolean, " Is that a moon?" , ( fun _ p -> p.IsMoon)) ]
234+ )
211235
212236 and RootType =
213- Define.Object< Root>(
237+ Define.Object< Root> (
214238 name = " Root" ,
215239 description = " The Root type to be passed to all our resolvers." ,
216240 isTypeOf = ( fun o -> o :? Root),
217- fieldsFn = fun () ->
218- [
219- Define.Field( " requestId" , String, " The ID of the client." , fun _ ( r : Root ) -> r.RequestId)
220- ])
241+ fieldsFn = fun () -> [ Define.Field ( " requestId" , String, " The ID of the client." , ( fun _ ( r : Root ) -> r.RequestId)) ]
242+ )
221243
222244 let Query =
223- Define.Object< Root>(
245+ let inputs = [ Define.Input ( " id" , String) ]
246+ Define.Object< Root> (
224247 name = " Query" ,
225- fields = [
226- Define.Field( " hero" , Nullable HumanType, " Gets human hero" , [ Define.Input( " id" , String) ], fun ctx _ -> getHuman ( ctx.Arg( " id" )))
227- Define.Field( " droid" , Nullable DroidType, " Gets droid" , [ Define.Input( " id" , String) ], fun ctx _ -> getDroid ( ctx.Arg( " id" )))
228- Define.Field( " planet" , Nullable PlanetType, " Gets planet" , [ Define.Input( " id" , String) ], fun ctx _ -> getPlanet ( ctx.Arg( " id" )))
229- Define.Field( " characters" , ListOf CharacterType, " Gets characters" , fun _ _ -> characters) ])
248+ fields =
249+ [ Define.Field ( " hero" , Nullable HumanType, " Gets human hero" , inputs, fun ctx _ -> getHuman ( ctx.Arg ( " id" )))
250+ Define.Field ( " droid" , Nullable DroidType, " Gets droid" , inputs, ( fun ctx _ -> getDroid ( ctx.Arg ( " id" ))))
251+ Define.Field ( " planet" , Nullable PlanetType, " Gets planet" , inputs, fun ctx _ -> getPlanet ( ctx.Arg ( " id" )))
252+ Define.Field ( " characters" , ListOf CharacterType, " Gets characters" , ( fun _ _ -> characters)) ]
253+ )
230254
231255 let Subscription =
232- Define.SubscriptionObject< Root>(
256+ Define.SubscriptionObject< Root> (
233257 name = " Subscription" ,
234- fields = [
235- Define.SubscriptionField(
236- " watchMoon" ,
237- RootType,
238- PlanetType,
239- " Watches to see if a planet is a moon." ,
240- [ Define.Input( " id" , String) ],
241- ( fun ctx _ p -> if ctx.Arg( " id" ) = p.Id then Some p else None)) ])
258+ fields =
259+ [ Define.SubscriptionField (
260+ " watchMoon" ,
261+ RootType,
262+ PlanetType,
263+ " Watches to see if a planet is a moon." ,
264+ [ Define.Input ( " id" , String) ],
265+ ( fun ctx _ p -> if ctx.Arg ( " id" ) = p.Id then Some p else None)
266+ ) ]
267+ )
242268
243269 let schemaConfig = SchemaConfig.Default
244270
245271 let Mutation =
246- Define.Object< Root>(
272+ Define.Object< Root> (
247273 name = " Mutation" ,
248- fields = [
249- Define.Field(
250- " setMoon" ,
251- Nullable PlanetType,
252- " Defines if a planet is actually a moon or not." ,
253- [ Define.Input( " id" , String); Define.Input( " isMoon" , Boolean) ],
254- fun ctx _ ->
255- getPlanet ( ctx.Arg( " id" ))
256- |> Option.map ( fun x ->
257- x.SetMoon( Some( ctx.Arg( " isMoon" ))) |> ignore
258- schemaConfig.SubscriptionProvider.Publish< Planet> " watchMoon" x
259- schemaConfig.LiveFieldSubscriptionProvider.Publish< Planet> " Planet" " isMoon" x
260- x))])
261-
262- let schema : ISchema < Root > = upcast Schema( Query, Mutation, Subscription, schemaConfig)
274+ fields =
275+ [ Define.Field (
276+ " setMoon" ,
277+ Nullable PlanetType,
278+ " Defines if a planet is actually a moon or not." ,
279+ [ Define.Input ( " id" , String); Define.Input ( " isMoon" , Boolean) ],
280+ fun ctx _ ->
281+ getPlanet ( ctx.Arg ( " id" ))
282+ |> Option.map ( fun x ->
283+ x.SetMoon ( Some ( ctx.Arg ( " isMoon" ))) |> ignore
284+ schemaConfig.SubscriptionProvider.Publish< Planet> " watchMoon" x
285+ schemaConfig.LiveFieldSubscriptionProvider.Publish< Planet> " Planet" " isMoon" x
286+ x)
287+ ) ]
288+ )
289+
290+ let schema : ISchema < Root > = upcast Schema ( Query, Mutation, Subscription, schemaConfig)
263291
264292 let middlewares =
265- [ Define.QueryWeightMiddleware( 2.0 , true )
266- Define.ObjectListFilterMiddleware< Human, Character option>( true )
267- Define.ObjectListFilterMiddleware< Droid, Character option>( true )
268- Define.LiveQueryMiddleware() ]
293+ [ Define.QueryWeightMiddleware ( 2.0 , true )
294+ Define.ObjectListFilterMiddleware< Human, Character option> ( true )
295+ Define.ObjectListFilterMiddleware< Droid, Character option> ( true )
296+ Define.LiveQueryMiddleware () ]
269297
270- let executor = Executor( schema, middlewares)
298+ let executor = Executor ( schema, middlewares)
0 commit comments