This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
155 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
namespace GraphQLTransportWS | ||
|
||
open System | ||
open System.Text.Json | ||
open System.Text.Json.Serialization | ||
|
||
[<Sealed>] | ||
type WebSocketServerMessageConverter() = | ||
inherit JsonConverter<WebSocketServerMessage>() | ||
|
||
override __.Read(reader: byref<Utf8JsonReader>, typeToConvert: Type, options: JsonSerializerOptions) = | ||
ConnectionAck | ||
|
||
override __.Write(writer: Utf8JsonWriter, value: WebSocketServerMessage, options: JsonSerializerOptions) = | ||
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(JsonSerializer.Serialize(result), skipInputValidation = true) | ||
endSerializedObj() | ||
| Complete(id) -> | ||
beginSerializedObjWith "type" "complete" | ||
thenAddProperty "id" id | ||
endSerializedObj() | ||
|
||
|
||
|
||
// writer.WriteStartObject(propertyName = ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
<GenerateProgramFile>false</GenerateProgramFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Tests.fs" /> | ||
<Compile Include="Program.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Main\Main.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module Program = let [<EntryPoint>] main _ = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Tests | ||
|
||
open GraphQLTransportWS | ||
open System | ||
open System.Text.Json | ||
open Xunit | ||
|
||
[<Fact>] | ||
let ``Serializes ServerPing correctly`` () = | ||
let jsonOptions = new JsonSerializerOptions() | ||
jsonOptions.Converters.Add(new WebSocketServerMessageConverter()) | ||
|
||
let input: WebSocketServerMessage = | ||
ServerPing (Some "Peekaboo!") | ||
|
||
let result = JsonSerializer.Serialize(input, jsonOptions) | ||
|
||
Assert.Equal("{\"type\":\"ping\",\"payload\":\"Peekaboo!\"}", result) | ||
|
||
[<Fact>] | ||
let ``Serializes Error correctly`` () = | ||
let jsonOptions = new JsonSerializerOptions() | ||
jsonOptions.Converters.Add(new WebSocketServerMessageConverter()) | ||
|
||
let input: WebSocketServerMessage = | ||
Error ("myId", [ "An error ocurred during GraphQL execution" ]) | ||
|
||
let result = JsonSerializer.Serialize(input, jsonOptions) | ||
|
||
Assert.Equal("{\"type\":\"error\",\"id\":\"myId\",\"payload\":[{\"message\":\"An error ocurred during GraphQL execution\"}]}", result) |