Skip to content

Commit ca0c7db

Browse files
authored
Merge pull request #294 from graphql-dotnet/add-websocket-connection-params
Add websocket ConnectionParams object
2 parents aa38225 + 937dc9c commit ca0c7db

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

src/GraphQL.Client.Abstractions.Websocket/GraphQLWebSocketRequest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public class GraphQLWebSocketRequest : Dictionary<string, object>, IEquatable<Gr
1414
public const string PAYLOAD_KEY = "payload";
1515

1616
/// <summary>
17-
/// The Identifier of the Response
17+
/// The Identifier of the request
1818
/// </summary>
1919
public string Id
2020
{
21-
get => ContainsKey(ID_KEY) ? (string)this[ID_KEY] : null;
21+
get => TryGetValue(ID_KEY, out object value) ? (string)value : null;
2222
set => this[ID_KEY] = value;
2323
}
2424

@@ -27,16 +27,16 @@ public string Id
2727
/// </summary>
2828
public string Type
2929
{
30-
get => ContainsKey(TYPE_KEY) ? (string)this[TYPE_KEY] : null;
30+
get => TryGetValue(TYPE_KEY, out object value) ? (string)value : null;
3131
set => this[TYPE_KEY] = value;
3232
}
3333

3434
/// <summary>
3535
/// The payload of the websocket request
3636
/// </summary>
37-
public GraphQLRequest Payload
37+
public object? Payload
3838
{
39-
get => ContainsKey(PAYLOAD_KEY) ? (GraphQLRequest)this[PAYLOAD_KEY] : null;
39+
get => TryGetValue(PAYLOAD_KEY, out object value) ? value : null;
4040
set => this[PAYLOAD_KEY] = value;
4141
}
4242

src/GraphQL.Client/GraphQLHttpClientOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,11 @@ public class GraphQLHttpClientOptions
5656
/// Configure additional websocket options (i.e. headers). This will not be invoked on Windows 7 when targeting .NET Framework 4.x.
5757
/// </summary>
5858
public Action<ClientWebSocketOptions> ConfigureWebsocketOptions { get; set; } = options => { };
59+
60+
/// <summary>
61+
/// Sets the `ConnectionParams` object sent with the GQL_CONNECTION_INIT message on establishing a GraphQL websocket connection.
62+
/// See https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md#gql_connection_init.
63+
/// </summary>
64+
public Func<GraphQLHttpClientOptions, object?> ConfigureWebSocketConnectionInitPayload { get; set; } = options => null;
5965
}
6066
}

src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
9898
Observable.Create<GraphQLResponse<TResponse>>(async observer =>
9999
{
100100
Debug.WriteLine($"Create observable thread id: {Thread.CurrentThread.ManagedThreadId}");
101-
await _client.Options.PreprocessRequest(request, _client);
101+
var preprocessedRequest = await _client.Options.PreprocessRequest(request, _client);
102+
102103
var startRequest = new GraphQLWebSocketRequest
103104
{
104105
Id = Guid.NewGuid().ToString("N"),
105106
Type = GraphQLWebSocketMessageType.GQL_START,
106-
Payload = request
107+
Payload = preprocessedRequest
107108
};
108-
var closeRequest = new GraphQLWebSocketRequest
109+
var stopRequest = new GraphQLWebSocketRequest
109110
{
110111
Id = startRequest.Id,
111112
Type = GraphQLWebSocketMessageType.GQL_STOP
@@ -114,7 +115,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
114115
{
115116
Id = startRequest.Id,
116117
Type = GraphQLWebSocketMessageType.GQL_CONNECTION_INIT,
117-
Payload = new GraphQLRequest()
118+
Payload = Options.ConfigureWebSocketConnectionInitPayload(Options)
118119
};
119120

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

180181
try
181182
{
182-
Debug.WriteLine($"sending close message on subscription {startRequest.Id}");
183-
await QueueWebSocketRequest(closeRequest);
183+
Debug.WriteLine($"sending stop message on subscription {startRequest.Id}");
184+
await QueueWebSocketRequest(stopRequest);
184185
}
185186
// do not break on disposing
186187
catch (OperationCanceledException) { }

src/GraphQL.Primitives/GraphQLRequest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class GraphQLRequest : Dictionary<string, object>, IEquatable<GraphQLRequ
1818
/// </summary>
1919
public string Query
2020
{
21-
get => ContainsKey(QUERY_KEY) ? (string)this[QUERY_KEY] : null;
21+
get => TryGetValue(QUERY_KEY, out object value) ? (string)value : null;
2222
set => this[QUERY_KEY] = value;
2323
}
2424

@@ -27,7 +27,7 @@ public string Query
2727
/// </summary>
2828
public string? OperationName
2929
{
30-
get => ContainsKey(OPERATION_NAME_KEY) ? (string)this[OPERATION_NAME_KEY] : null;
30+
get => TryGetValue(OPERATION_NAME_KEY, out object value) ? (string)value : null;
3131
set => this[OPERATION_NAME_KEY] = value;
3232
}
3333

@@ -36,7 +36,7 @@ public string? OperationName
3636
/// </summary>
3737
public object? Variables
3838
{
39-
get => ContainsKey(VARIABLES_KEY) ? this[VARIABLES_KEY] : null;
39+
get => TryGetValue(VARIABLES_KEY, out object value) ? value : null;
4040
set => this[VARIABLES_KEY] = value;
4141
}
4242

0 commit comments

Comments
 (0)