Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class GraphQLWebSocketRequest : Dictionary<string, object>, IEquatable<Gr
public const string PAYLOAD_KEY = "payload";

/// <summary>
/// The Identifier of the Response
/// The Identifier of the request
/// </summary>
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;
}

Expand All @@ -27,16 +27,16 @@ public string Id
/// </summary>
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;
}

/// <summary>
/// The payload of the websocket request
/// </summary>
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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/GraphQL.Client/GraphQLHttpClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
public Action<ClientWebSocketOptions> ConfigureWebsocketOptions { get; set; } = options => { };

/// <summary>
/// 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.
/// </summary>
public Func<GraphQLHttpClientOptions, object?> ConfigureWebSocketConnectionInitPayload { get; set; } = options => null;
}
}
13 changes: 7 additions & 6 deletions src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
Observable.Create<GraphQLResponse<TResponse>>(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
Expand All @@ -114,7 +115,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
{
Id = startRequest.Id,
Type = GraphQLWebSocketMessageType.GQL_CONNECTION_INIT,
Payload = new GraphQLRequest()
Payload = Options.ConfigureWebSocketConnectionInitPayload(Options)
};

var observable = Observable.Create<GraphQLResponse<TResponse>>(o =>
Expand Down Expand Up @@ -179,8 +180,8 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons

try
{
Debug.WriteLine($"sending close message on subscription {startRequest.Id}");
await QueueWebSocketRequest(closeRequest);
Debug.WriteLine($"sending stop message on subscription {startRequest.Id}");
await QueueWebSocketRequest(stopRequest);
}
// do not break on disposing
catch (OperationCanceledException) { }
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL.Primitives/GraphQLRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GraphQLRequest : Dictionary<string, object>, IEquatable<GraphQLRequ
/// </summary>
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;
}

Expand All @@ -27,7 +27,7 @@ public string Query
/// </summary>
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;
}

Expand All @@ -36,7 +36,7 @@ public string? OperationName
/// </summary>
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;
}

Expand Down