diff --git a/src/GraphQL.Client.Abstractions.Websocket/GraphQLWebSocketRequest.cs b/src/GraphQL.Client.Abstractions.Websocket/GraphQLWebSocketRequest.cs index 623e4271..f83fe5aa 100644 --- a/src/GraphQL.Client.Abstractions.Websocket/GraphQLWebSocketRequest.cs +++ b/src/GraphQL.Client.Abstractions.Websocket/GraphQLWebSocketRequest.cs @@ -14,11 +14,11 @@ public class GraphQLWebSocketRequest : Dictionary, IEquatable - /// The Identifier of the Response + /// The Identifier of the request /// public string Id { - get => ContainsKey(ID_KEY) ? (string)this[ID_KEY] : null; + get => TryGetValue(ID_KEY, out object value) ? (string)value : null; set => this[ID_KEY] = value; } @@ -27,16 +27,16 @@ public string Id /// public string Type { - get => ContainsKey(TYPE_KEY) ? (string)this[TYPE_KEY] : null; + get => TryGetValue(TYPE_KEY, out object value) ? (string)value : null; set => this[TYPE_KEY] = value; } /// /// The payload of the websocket request /// - public GraphQLRequest Payload + public object? Payload { - get => ContainsKey(PAYLOAD_KEY) ? (GraphQLRequest)this[PAYLOAD_KEY] : null; + get => TryGetValue(PAYLOAD_KEY, out object value) ? value : null; set => this[PAYLOAD_KEY] = value; } diff --git a/src/GraphQL.Client/GraphQLHttpClientOptions.cs b/src/GraphQL.Client/GraphQLHttpClientOptions.cs index 6e2c3a46..c0054813 100644 --- a/src/GraphQL.Client/GraphQLHttpClientOptions.cs +++ b/src/GraphQL.Client/GraphQLHttpClientOptions.cs @@ -56,5 +56,11 @@ public class GraphQLHttpClientOptions /// Configure additional websocket options (i.e. headers). This will not be invoked on Windows 7 when targeting .NET Framework 4.x. /// public Action ConfigureWebsocketOptions { get; set; } = options => { }; + + /// + /// Sets the `ConnectionParams` object sent with the GQL_CONNECTION_INIT message on establishing a GraphQL websocket connection. + /// See https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md#gql_connection_init. + /// + public Func ConfigureWebSocketConnectionInitPayload { get; set; } = options => null; } } diff --git a/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs b/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs index dfdf39c4..28af82d6 100644 --- a/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs +++ b/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs @@ -98,14 +98,15 @@ public IObservable> CreateSubscriptionStream>(async observer => { Debug.WriteLine($"Create observable thread id: {Thread.CurrentThread.ManagedThreadId}"); - await _client.Options.PreprocessRequest(request, _client); + var preprocessedRequest = await _client.Options.PreprocessRequest(request, _client); + var startRequest = new GraphQLWebSocketRequest { Id = Guid.NewGuid().ToString("N"), Type = GraphQLWebSocketMessageType.GQL_START, - Payload = request + Payload = preprocessedRequest }; - var closeRequest = new GraphQLWebSocketRequest + var stopRequest = new GraphQLWebSocketRequest { Id = startRequest.Id, Type = GraphQLWebSocketMessageType.GQL_STOP @@ -114,7 +115,7 @@ public IObservable> CreateSubscriptionStream>(o => @@ -179,8 +180,8 @@ public IObservable> CreateSubscriptionStream, IEquatable public string Query { - get => ContainsKey(QUERY_KEY) ? (string)this[QUERY_KEY] : null; + get => TryGetValue(QUERY_KEY, out object value) ? (string)value : null; set => this[QUERY_KEY] = value; } @@ -27,7 +27,7 @@ public string Query /// public string? OperationName { - get => ContainsKey(OPERATION_NAME_KEY) ? (string)this[OPERATION_NAME_KEY] : null; + get => TryGetValue(OPERATION_NAME_KEY, out object value) ? (string)value : null; set => this[OPERATION_NAME_KEY] = value; } @@ -36,7 +36,7 @@ public string? OperationName /// public object? Variables { - get => ContainsKey(VARIABLES_KEY) ? this[VARIABLES_KEY] : null; + get => TryGetValue(VARIABLES_KEY, out object value) ? value : null; set => this[VARIABLES_KEY] = value; }