Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
"deserializing" ClientComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
valbers committed Feb 7, 2023
1 parent a525ee4 commit 04f4b14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Main/WebSocketMessagesMappers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module GraphQLWsMessageRawMapping =
| _ -> failwith "payload was expected to be a string, but it wasn't"
| None -> None

let requireId (raw : GraphQLWsMessageRaw) : string =
match raw.Id with
| Some s -> s
| None -> failwith "property \"id\" is required but was not there"

let toWebSocketClientMessage (raw : GraphQLWsMessageRaw) : WebSocketClientMessage =
match raw.Type with
| None ->
Expand All @@ -19,7 +24,9 @@ module GraphQLWsMessageRawMapping =
| Some "ping" ->
ClientPing (raw.Payload |> requirePayloadToBeAnOptionalString)
| Some "pong" ->
ClientPong (raw.Payload |> requirePayloadToBeAnOptionalString)
ClientPong (raw.Payload |> requirePayloadToBeAnOptionalString)
| Some "complete" ->
ClientComplete (raw |> requireId)
| Some other ->
failwithf "type \"%s\" is not supported as a client message type" other

16 changes: 16 additions & 0 deletions tests/unit-tests/SerializationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,19 @@ let ``Deserializes ClientPong with payload correctly`` () =
| ClientPong (Some "pong!") -> () // <-- expected
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A'" other)

[<Fact>]
let ``Deserializes ClientComplete correctly``() =
let serializerOptions = new JsonSerializerOptions()
serializerOptions.Converters.Add(new GraphQLWsMessageConverter())

let input = "{\"id\": \"65fca2b5-f149-4a70-a055-5123dea4628f\", \"type\":\"complete\"}"

let resultRaw = JsonSerializer.Deserialize<GraphQLWsMessageRaw>(input, serializerOptions)
let result = resultRaw |> GraphQLWsMessageRawMapping.toWebSocketClientMessage

match result with
| ClientComplete id ->
Assert.Equal("65fca2b5-f149-4a70-a055-5123dea4628f", id)
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A'" other)

0 comments on commit 04f4b14

Please sign in to comment.