Skip to content

Commit 360616d

Browse files
Merge pull request #1180 from zino-hofmann/macros/docs_ws
improve grapphql ws protocol docs
2 parents 0d8a3d0 + e59d406 commit 360616d

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

packages/graphql/lib/src/links/websocket_link/websocket_client.dart

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,37 @@ class SocketClientConfig {
141141
}
142142
}
143143

144+
/// All the protocol supported by the library
145+
@Deprecated(
146+
"`SocketSubProtocol`is deprecated and will be removed in the version 5.2.0, consider to use `GraphQLProtocol`")
144147
class SocketSubProtocol {
145148
SocketSubProtocol._();
146149

150+
/// graphql-ws: The new (not to be confused with the graphql-ws library).
151+
/// NB. This protocol is it no longer maintained, please consider
152+
/// to use `SocketSubProtocol.graphqlTransportWs`.
153+
static const String graphqlWs = GraphQLProtocol.graphqlWs;
154+
155+
/// graphql-transport-ws: New ws protocol used by most Apollo Server instances
156+
/// with subscriptions enabled use this library.
157+
/// N.B: not to be confused with the graphql-ws library that implement the
158+
/// old ws protocol.
159+
static const String graphqlTransportWs = GraphQLProtocol.graphqlWs;
160+
}
161+
162+
/// ALL protocol supported by the library
163+
class GraphQLProtocol {
164+
GraphQLProtocol._();
165+
166+
/// graphql-ws: The new (not to be confused with the graphql-ws library).
167+
/// NB. This protocol is it no longer maintained, please consider
168+
/// to use `SocketSubProtocol.graphqlTransportWs`.
147169
static const String graphqlWs = "graphql-ws";
170+
171+
/// graphql-transport-ws: New ws protocol used by most Apollo Server instances
172+
/// with subscriptions enabled use this library.
173+
/// N.B: not to be confused with the graphql-ws library that implement the
174+
/// old ws protocol.
148175
static const String graphqlTransportWs = "graphql-transport-ws";
149176
}
150177

@@ -159,7 +186,7 @@ class SocketSubProtocol {
159186
class SocketClient {
160187
SocketClient(
161188
this.url, {
162-
this.protocol = SocketSubProtocol.graphqlWs,
189+
this.protocol = GraphQLProtocol.graphqlWs,
163190
this.config = const SocketClientConfig(),
164191
@visibleForTesting this.randomBytesForUuid,
165192
@visibleForTesting this.onMessage,
@@ -247,14 +274,14 @@ class SocketClient {
247274
await config.connect(uri: Uri.parse(url), protocols: [protocol]);
248275
socketChannel = connection.forGraphQL();
249276

250-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
277+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
251278
_connectionStateController.add(SocketConnectionState.handshake);
252279
} else {
253280
_connectionStateController.add(SocketConnectionState.connected);
254281
}
255282
print('Initialising connection');
256283
_write(initOperation);
257-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
284+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
258285
// wait for ack
259286
// this blocks to prevent ping from being called before ack is recieved
260287
await _messages.firstWhere(
@@ -263,10 +290,10 @@ class SocketClient {
263290
}
264291

265292
if (config.inactivityTimeout != null) {
266-
if (protocol == SocketSubProtocol.graphqlWs) {
293+
if (protocol == GraphQLProtocol.graphqlWs) {
267294
_disconnectOnKeepAliveTimeout(_messages);
268295
}
269-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
296+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
270297
_enqueuePing();
271298
}
272299
}
@@ -277,7 +304,7 @@ class SocketClient {
277304
onMessage!(message);
278305
}
279306

280-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
307+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
281308
if (message.type == 'ping') {
282309
_write(PongMessage());
283310
} else if (message.type == 'pong') {
@@ -494,7 +521,7 @@ class SocketClient {
494521
id,
495522
serialize(payload),
496523
);
497-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
524+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
498525
operation = SubscribeOperation(
499526
id,
500527
serialize(payload),
@@ -512,7 +539,7 @@ class SocketClient {
512539
_subscriptionInitializers.remove(id);
513540

514541
sub?.cancel();
515-
if (protocol == SocketSubProtocol.graphqlWs &&
542+
if (protocol == GraphQLProtocol.graphqlWs &&
516543
_connectionStateController.value == SocketConnectionState.connected &&
517544
socketChannel != null) {
518545
_write(StopOperation(id));

packages/graphql/lib/src/links/websocket_link/websocket_link.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WebSocketLink extends Link {
1616
WebSocketLink(
1717
this.url, {
1818
this.config = const SocketClientConfig(),
19-
this.subProtocol = SocketSubProtocol.graphqlWs,
19+
this.subProtocol = GraphQLProtocol.graphqlWs,
2020
});
2121

2222
final String url;

packages/graphql/test/websocket_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SocketClient getTestClient({
1818
bool autoReconnect = true,
1919
Map<String, dynamic>? customHeaders,
2020
Duration delayBetweenReconnectionAttempts = const Duration(milliseconds: 1),
21-
String protocol = SocketSubProtocol.graphqlWs,
21+
String protocol = GraphQLProtocol.graphqlWs,
2222
}) =>
2323
SocketClient(
2424
wsUrl,
@@ -340,7 +340,7 @@ Future<void> main() async {
340340
controller = StreamController(sync: true);
341341
socketClient = getTestClient(
342342
controller: controller,
343-
protocol: SocketSubProtocol.graphqlTransportWs,
343+
protocol: GraphQLProtocol.graphqlTransportWs,
344344
wsUrl: wsUrl,
345345
);
346346
}));

0 commit comments

Comments
 (0)