@@ -10,6 +10,7 @@ open System.Text.Json
1010open System.Text .Json .Serialization
1111open System.Threading
1212open System.Threading .Tasks
13+ open FSharp.Data .GraphQL .Shared
1314open Microsoft.AspNetCore .Http
1415open Microsoft.Extensions .Hosting
1516open Microsoft.Extensions .Logging
@@ -45,7 +46,7 @@ type GraphQLWebSocketMiddleware<'Root>
4546 | ServerPong p -> { Id = ValueNone; Type = " pong" ; Payload = p |> ValueOption.map CustomResponse }
4647 | Next ( id, payload) -> { Id = ValueSome id; Type = " next" ; Payload = ValueSome <| ExecutionResult payload }
4748 | Complete id -> { Id = ValueSome id; Type = " complete" ; Payload = ValueNone }
48- | Error ( id, errMsgs ) -> { Id = ValueSome id; Type = " error" ; Payload = ValueSome <| ErrorMessages errMsgs }
49+ | Error ( id, errMessages ) -> { Id = ValueSome id; Type = " error" ; Payload = ValueSome <| ErrorMessages errMessages }
4950 return JsonSerializer.Serialize ( raw, jsonSerializerOptions)
5051 }
5152
@@ -89,9 +90,9 @@ type GraphQLWebSocketMiddleware<'Root>
8990 && (( segmentResponse = null )
9091 || ( not segmentResponse.EndOfMessage)) do
9192 try
92- let! r = socket.ReceiveAsync ( new ArraySegment< byte> ( buffer), cancellationToken)
93+ let! r = socket.ReceiveAsync ( ArraySegment< byte>( buffer), cancellationToken)
9394 segmentResponse <- r
94- completeMessage.AddRange ( new ArraySegment< byte> ( buffer, 0 , r.Count))
95+ completeMessage.AddRange ( ArraySegment< byte>( buffer, 0 , r.Count))
9596 with : ? OperationCanceledException ->
9697 ()
9798
@@ -117,7 +118,7 @@ type GraphQLWebSocketMiddleware<'Root>
117118 else
118119 // TODO: Allocate string only if a debugger is attached
119120 let! serializedMessage = message |> serializeServerMessage jsonSerializerOptions
120- let segment = new ArraySegment< byte> ( System.Text.Encoding.UTF8.GetBytes ( serializedMessage))
121+ let segment = ArraySegment< byte>( System.Text.Encoding.UTF8.GetBytes ( serializedMessage))
121122 if not ( socket.State = WebSocketState.Open) then
122123 logger.LogTrace ( $" Ignoring message to be sent via socket, since its state is not '{nameof WebSocketState.Open}', but '{{state}}'" , socket.State)
123124 else
@@ -160,31 +161,32 @@ type GraphQLWebSocketMiddleware<'Root>
160161 tryToGracefullyCloseSocket ( WebSocketCloseStatus.NormalClosure, " Normal Closure" )
161162
162163 let handleMessages ( cancellationToken : CancellationToken ) ( httpContext : HttpContext ) ( socket : WebSocket ) : Task =
163- let subscriptions = new Dictionary< SubscriptionId, SubscriptionUnsubscriber * OnUnsubscribeAction> ()
164+ let subscriptions = Dictionary< SubscriptionId, SubscriptionUnsubscriber * OnUnsubscribeAction>()
164165 // ---------->
165166 // Helpers -->
166167 // ---------->
167168 let rcvMsgViaSocket = receiveMessageViaSocket ( CancellationToken.None)
168169
169170 let sendMsg = sendMessageViaSocket serializerOptions socket
170171 let rcv () = socket |> rcvMsgViaSocket serializerOptions
172+ let inputContext = fun () -> ( HttpContextRequestExecutionContext httpContext) :> IInputExecutionContext
171173
172174 let sendOutput id ( output : SubscriptionExecutionResult ) =
173175 sendMsg ( Next ( id, output))
174176
175177 let sendSubscriptionResponseOutput id subscriptionResult =
176178 match subscriptionResult with
177179 | SubscriptionResult output -> { Data = ValueSome output; Errors = [] } |> sendOutput id
178- | SubscriptionErrors ( output , errors) ->
180+ | SubscriptionErrors (_ , errors) ->
179181 logger.LogWarning ( " Subscription errors: {subscriptionErrors}" , ( String.Join ( '\n' , errors |> Seq.map ( fun x -> $" - %s {x.Message}" ))))
180182 { Data = ValueNone; Errors = errors } |> sendOutput id
181183
182184 let sendDeferredResponseOutput id deferredResult =
183185 match deferredResult with
184- | DeferredResult ( obj, path ) ->
186+ | DeferredResult ( obj, _ ) ->
185187 let output = obj :?> Dictionary< string, obj>
186188 { Data = ValueSome output; Errors = [] } |> sendOutput id
187- | DeferredErrors ( obj , errors, _) ->
189+ | DeferredErrors (_ , errors, _) ->
188190 logger.LogWarning (
189191 " Deferred response errors: {deferredErrors}" ,
190192 ( String.Join ( '\n' , errors |> Seq.map ( fun x -> $" - %s {x.Message}" )))
@@ -234,10 +236,10 @@ type GraphQLWebSocketMiddleware<'Root>
234236 && socket |> isSocketOpen do
235237 let! receivedMessage = rcv ()
236238 match receivedMessage with
237- | Result.Error failureMsgs ->
239+ | Result.Error failureMessages ->
238240 nameof InvalidMessage
239241 |> logMsgReceivedWithOptionalPayload ValueNone
240- match failureMsgs with
242+ match failureMessages with
241243 | InvalidMessage ( code, explanation) -> do ! socket.CloseAsync ( enum code, explanation, CancellationToken.None)
242244 | Ok ValueNone -> logger.LogTrace ( " WebSocket received empty message! State = '{socketState}'" , socket.State)
243245 | Ok ( ValueSome msg) ->
@@ -274,11 +276,11 @@ type GraphQLWebSocketMiddleware<'Root>
274276 let variables = query.Variables |> Skippable.toOption
275277 let! planExecutionResult =
276278 let root = options.RootFactory httpContext
277- options.SchemaExecutor.AsyncExecute ( query.Query, root, ?variables = variables)
279+ options.SchemaExecutor.AsyncExecute ( query.Query, inputContext , root, ?variables = variables)
278280 do ! planExecutionResult |> applyPlanExecutionResult id socket
279281 with ex ->
280282 logger.LogError ( ex, " Unexpected error during subscription with id '{id}'" , id)
281- do ! sendMsg ( Error ( id, [ new Shared.NameValueLookup ([ ( " subscription" , " Unexpected error during subscription" :> obj ) ])]))
283+ do ! sendMsg ( Error ( id, [ Shared.NameValueLookup([ ( " subscription" , " Unexpected error during subscription" :> obj ) ])]))
282284 | ClientComplete id ->
283285 " ClientComplete" |> logMsgWithIdReceived id
284286 subscriptions
@@ -287,7 +289,7 @@ type GraphQLWebSocketMiddleware<'Root>
287289 do ! socket |> tryToGracefullyCloseSocketWithDefaultBehavior
288290 with ex ->
289291 logger.LogError ( ex, " Cannot handle a message; dropping a websocket connection" )
290- // at this point, only something really weird must have happened.
292+ // At this point, only something really weird must have happened.
291293 // In order to avoid faulty state scenarios and unimagined damages,
292294 // just close the socket without further ado.
293295 do ! socket |> tryToGracefullyCloseSocketWithDefaultBehavior
@@ -344,7 +346,7 @@ type GraphQLWebSocketMiddleware<'Root>
344346 return Result.Error <| " {nameof ConnectionInit} timeout"
345347 }
346348
347- member __ .InvokeAsync ( ctx : HttpContext ) = task {
349+ member _ .InvokeAsync ( ctx : HttpContext ) = task {
348350 if not ( ctx.Request.Path = endpointUrl) then
349351 do ! next.Invoke ( ctx)
350352 else if ctx.WebSockets.IsWebSocketRequest then
0 commit comments