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

Commit

Permalink
refactoring: radical renaming
Browse files Browse the repository at this point in the history
 To be closer to subprotocol's official terms (but also to remove
clutter)
  • Loading branch information
valbers committed Feb 7, 2023
1 parent 4a0957e commit 2c32165
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 145 deletions.
2 changes: 1 addition & 1 deletion src/Main/GraphQLWebsocketMiddleware.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type GraphQLWebSocketMiddleware<'Root>(next : RequestDelegate, applicationLifeti
return Some deserializedMsg
}

let sendMessageViaSocket (cancellationToken : CancellationToken) (jsonOptions: JsonOptions) (socket : WebSocket) (message : WebSocketServerMessage) =
let sendMessageViaSocket (cancellationToken : CancellationToken) (jsonOptions: JsonOptions) (socket : WebSocket) (message : ServerMessage) =
task {
if not (socket.State = WebSocketState.Open) then
printfn "ignoring message to be sent via socket, since its state is not 'Open', but '%A'" socket.State
Expand Down
5 changes: 0 additions & 5 deletions src/Main/Library.fs

This file was deleted.

7 changes: 3 additions & 4 deletions src/Main/Main.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
<ItemGroup>
<Compile Include="Rop/Rop.fs" />
<Compile Include="Rop/RopAsync.fs" />
<Compile Include="WebSocketMessages.fs" />
<Compile Include="WebSocketMessagesMappers.fs" />
<Compile Include="Messages.fs" />
<Compile Include="MessageMapping.fs" />
<Compile Include="GraphQLWebsocketMiddlewareOptions.fs" />
<Compile Include="JsonConverters.fs" />
<Compile Include="RawMessageConverter.fs" />
<Compile Include="GraphQLWebsocketMiddleware.fs" />
<Compile Include="Library.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
namespace GraphQLTransportWS

module GraphQLWsMessageRawMapping =
module MessageMapping =
open FSharp.Data.GraphQL

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

let requirePayloadToBeAnOptionalString (payload : GraphQLWsMessagePayloadRaw option) : string option =
let requirePayloadToBeAnOptionalString (payload : RawPayload option) : string option =
match payload with
| Some p ->
match p with
| StringPayload strPayload -> Some strPayload
| _ -> failwith "payload was expected to be a string, but it wasn't"
| None -> None

let requireSubscribePayload (executor : Executor<'a>) (payload : GraphQLWsMessagePayloadRaw option) : GraphQLQuery =
let requireSubscribePayload (executor : Executor<'a>) (payload : RawPayload option) : GraphQLQuery =
match payload with
| Some p ->
match p with
Expand All @@ -32,7 +32,7 @@ module GraphQLWsMessageRawMapping =
| None ->
failwith "payload is required for this message, but none was available"

let toWebSocketClientMessage (executor : Executor<'a>) (raw : GraphQLWsMessageRaw) : WebSocketClientMessage =
let toClientMessage (executor : Executor<'a>) (raw : RawMessage) : ClientMessage =
match raw.Type with
| None ->
failwithf "property \"type\" was not found in the client message"
Expand Down
14 changes: 7 additions & 7 deletions src/Main/WebSocketMessages.fs → src/Main/Messages.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ namespace GraphQLTransportWS
open FSharp.Data.GraphQL.Execution
open FSharp.Data.GraphQL.Types

type GraphQLWsMessageSubscribePayloadRaw =
type RawSubscribePayload =
{ OperationName : string option
Query : string option
Variables : string option
Extensions : string option }

type GraphQLWsMessagePayloadRaw =
type RawPayload =
| StringPayload of string
| SubscribePayload of GraphQLWsMessageSubscribePayloadRaw
| SubscribePayload of RawSubscribePayload

type GraphQLWsMessageRaw =
type RawMessage =
{ Id : string option
Type : string option
Payload : GraphQLWsMessagePayloadRaw option }
Payload : RawPayload option }

type GraphQLQuery =
{ ExecutionPlan : ExecutionPlan
Variables : Map<string, obj> }

type WebSocketClientMessage =
type ClientMessage =
| ConnectionInit of payload: string option
| ClientPing of payload: string option
| ClientPong of payload: string option
| Subscribe of id: string * query: GraphQLQuery
| ClientComplete of id: string
| InvalidMessage of explanation: string

type WebSocketServerMessage =
type ServerMessage =
| ConnectionAck
| ServerPing of payload: string option
| ServerPong of payload: string option
Expand Down
82 changes: 7 additions & 75 deletions src/Main/JsonConverters.fs → src/Main/RawMessageConverter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ open System.Text.Json
open System.Text.Json.Serialization

[<Sealed>]
type GraphQLWsMessageConverter() =
inherit JsonConverter<GraphQLWsMessageRaw>()
type RawMessageConverter() =
inherit JsonConverter<RawMessage>()

let getOptionalString (reader : byref<Utf8JsonReader>) =
if reader.TokenType.Equals(JsonTokenType.Null) then
Expand All @@ -20,7 +20,7 @@ type GraphQLWsMessageConverter() =
else
failwithf "was expecting a value for property \"%s\"" propertyName

let readSubscribePayload (reader : byref<Utf8JsonReader>) : GraphQLWsMessageSubscribePayloadRaw =
let readSubscribePayload (reader : byref<Utf8JsonReader>) : RawSubscribePayload =
let mutable operationName : string option = None
let mutable query : string option = None
let mutable variables : string option = None
Expand All @@ -42,7 +42,7 @@ type GraphQLWsMessageConverter() =
Variables = variables
Extensions = extensions }

let readPayload (reader : byref<Utf8JsonReader>) : GraphQLWsMessagePayloadRaw option =
let readPayload (reader : byref<Utf8JsonReader>) : RawPayload option =
if reader.Read() then
if reader.TokenType.Equals(JsonTokenType.String) then
StringPayload (reader.GetString())
Expand All @@ -57,13 +57,13 @@ type GraphQLWsMessageConverter() =
else
failwith "was expecting a value for property \"payload\""

override __.Read(reader : byref<Utf8JsonReader>, typeToConvert: Type, options: JsonSerializerOptions) : GraphQLWsMessageRaw =
override __.Read(reader : byref<Utf8JsonReader>, typeToConvert: Type, options: JsonSerializerOptions) : RawMessage =
if not (reader.TokenType.Equals(JsonTokenType.StartObject))
then raise (new JsonException((sprintf "reader's first token was not \"%A\", but \"%A\"" JsonTokenType.StartObject reader.TokenType)))
else
let mutable id : string option = None
let mutable theType : string option = None
let mutable payload : GraphQLWsMessagePayloadRaw option = None
let mutable payload : RawPayload option = None
while reader.Read() && (not (reader.TokenType.Equals(JsonTokenType.EndObject))) do
match reader.GetString() with
| "id" ->
Expand All @@ -78,73 +78,5 @@ type GraphQLWsMessageConverter() =
Type = theType
Payload = payload }

override __.Write(writer : Utf8JsonWriter, value : GraphQLWsMessageRaw, options : JsonSerializerOptions) =
override __.Write(writer : Utf8JsonWriter, value : RawMessage, options : JsonSerializerOptions) =
failwith "serializing a WebSocketClientMessage is not supported (yet(?))"

[<Sealed>]
type WebSocketServerMessageConverter() =
inherit JsonConverter<WebSocketServerMessage>()

override __.Read(reader: byref<Utf8JsonReader>, typeToConvert: Type, options: JsonSerializerOptions) =
failwith "deserializing a WebSocketServerMessage is not supported (yet(?))"

override __.Write(writer: Utf8JsonWriter, value: WebSocketServerMessage, options: JsonSerializerOptions) =
let serializeAny x = JsonSerializer.Serialize(x, options)
let thenAddProperty (propName: string) (propStrValue: string) =
writer.WritePropertyName(propName)
writer.WriteStringValue(propStrValue)

let beginSerializedObjWith (propName: string) (propStrValue: string) =
writer.WriteStartObject()
thenAddProperty propName propStrValue

let endSerializedObj () =
writer.WriteEndObject()

let addOptionalPayloadProperty (optionalPayload: string option) =
match optionalPayload with
| Some p ->
thenAddProperty "payload" p
| None ->
()

match value with
| Error (id, errs) ->
beginSerializedObjWith "type" "error"
thenAddProperty "id" id
writer.WriteStartArray("payload")
errs
|> List.iter
(fun err ->
writer.WriteStartObject()
writer.WritePropertyName("message")
writer.WriteStringValue(err)
writer.WriteEndObject()
)
writer.WriteEndArray()
endSerializedObj()
| ConnectionAck ->
beginSerializedObjWith "type" "connection_ack"
endSerializedObj()
| ServerPing p ->
beginSerializedObjWith "type" "ping"
addOptionalPayloadProperty p
endSerializedObj()
| ServerPong p ->
beginSerializedObjWith "type" "pong"
addOptionalPayloadProperty p
endSerializedObj()
| Next(id, result) ->
beginSerializedObjWith "type" "next"
thenAddProperty "id" id
writer.WritePropertyName("payload")
writer.WriteRawValue(serializeAny result, skipInputValidation = true)
endSerializedObj()
| Complete(id) ->
beginSerializedObjWith "type" "complete"
thenAddProperty "id" id
endSerializedObj()



// writer.WriteStartObject(propertyName = )
Loading

0 comments on commit 2c32165

Please sign in to comment.