Skip to content

Commit 95e30b5

Browse files
committed
fixing ws subprot. impl: sending correct payload format
Data should be in property "data" and errors should be in property "errors"
1 parent 2fa3df2 commit 95e30b5

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/FSharp.Data.GraphQL.Server.AspNetCore/GraphQLWebsocketMiddleware.fs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,33 +169,27 @@ type GraphQLWebSocketMiddleware<'Root>
169169
let sendMsg = sendMessageViaSocket serializerOptions socket
170170
let rcv () = socket |> rcvMsgViaSocket serializerOptions
171171

172-
let sendOutput id (output : Output) =
173-
match output.TryGetValue "errors" with
174-
| true, theValue ->
175-
// The specification says: "This message terminates the operation and no further messages will be sent."
176-
subscriptions
177-
|> GraphQLSubscriptionsManagement.removeSubscription (id)
178-
sendMsg (Error (id, unbox theValue))
179-
| false, _ -> sendMsg (Next (id, output))
172+
let sendOutput id (output : SubscriptionExecutionResult) =
173+
sendMsg (Next (id, output))
180174

181175
let sendSubscriptionResponseOutput id subscriptionResult =
182176
match subscriptionResult with
183-
| SubscriptionResult output -> output |> sendOutput id
177+
| SubscriptionResult output -> { Data = ValueSome output; Errors = [] } |> sendOutput id
184178
| SubscriptionErrors (output, errors) ->
185179
logger.LogWarning ("Subscription errors: {subscriptionErrors}", (String.Join ('\n', errors |> Seq.map (fun x -> $"- %s{x.Message}"))))
186-
Task.FromResult ()
180+
{ Data = ValueNone; Errors = errors } |> sendOutput id
187181

188182
let sendDeferredResponseOutput id deferredResult =
189183
match deferredResult with
190184
| DeferredResult (obj, path) ->
191185
let output = obj :?> Dictionary<string, obj>
192-
output |> sendOutput id
186+
{ Data = ValueSome output; Errors = [] } |> sendOutput id
193187
| DeferredErrors (obj, errors, _) ->
194188
logger.LogWarning (
195189
"Deferred response errors: {deferredErrors}",
196190
(String.Join ('\n', errors |> Seq.map (fun x -> $"- %s{x.Message}")))
197191
)
198-
Task.FromResult ()
192+
{ Data = ValueNone; Errors = errors } |> sendOutput id
199193

200194
let sendDeferredResultDelayedBy (ct : CancellationToken) (ms : int) id deferredResult : Task = task {
201195
do! Task.Delay (ms, ct)
@@ -208,16 +202,16 @@ type GraphQLWebSocketMiddleware<'Root>
208202
(subscriptions, socket, observableOutput, serializerOptions)
209203
|> addClientSubscription id sendSubscriptionResponseOutput
210204
| Deferred (data, errors, observableOutput) ->
211-
do! data |> sendOutput id
205+
do! { Data = ValueSome data; Errors = [] } |> sendOutput id
212206
if errors.IsEmpty then
213207
(subscriptions, socket, observableOutput, serializerOptions)
214208
|> addClientSubscription id (sendDeferredResultDelayedBy cancellationToken 5000)
215209
else
216210
()
217-
| Direct (data, _) -> do! data |> sendOutput id
211+
| Direct (data, _) -> do! { Data = ValueSome data; Errors = [] } |> sendOutput id
218212
| RequestError problemDetails ->
219213
logger.LogWarning("Request errors:\n{errors}", problemDetails)
220-
214+
do! { Data = ValueNone; Errors = problemDetails } |> sendOutput id
221215
}
222216

223217
let logMsgReceivedWithOptionalPayload optionalPayload (msgAsStr : string) =

src/FSharp.Data.GraphQL.Shared/WebSockets.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace FSharp.Data.GraphQL.Shared.WebSockets
33
open System
44
open System.Collections.Generic
55
open System.Text.Json
6+
open FSharp.Data.GraphQL
67
open FSharp.Data.GraphQL.Shared
78

89
type InvalidWebsocketMessageException (explanation : string) =
@@ -15,8 +16,10 @@ type SubscriptionsDict = IDictionary<SubscriptionId, SubscriptionUnsubscriber *
1516

1617
type RawMessage = { Id : string voption; Type : string; Payload : JsonDocument voption }
1718

19+
type SubscriptionExecutionResult = { Data : Output voption; Errors : GQLProblemDetails list }
20+
1821
type ServerRawPayload =
19-
| ExecutionResult of Output
22+
| ExecutionResult of SubscriptionExecutionResult
2023
| ErrorMessages of NameValueLookup list
2124
| CustomResponse of JsonDocument
2225

@@ -35,7 +38,7 @@ type ServerMessage =
3538
| ConnectionAck
3639
| ServerPing
3740
| ServerPong of JsonDocument voption
38-
| Next of id : string * payload : Output
41+
| Next of id : string * payload : SubscriptionExecutionResult
3942
| Error of id : string * err : NameValueLookup list
4043
| Complete of id : string
4144

0 commit comments

Comments
 (0)