Skip to content

Commit e59d406

Browse files
feat(graphql): Introduce a new class GraphQLProtocol with bettern naming
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 2afd583 commit e59d406

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,36 @@ class SocketClientConfig {
142142
}
143143

144144
/// All the protocol supported by the library
145-
@Deprecated("`SocketSubProtocol`is deprecated and will be removed in the version 5.2.0, consider to use `GraphQLProtocol`")
145+
@Deprecated(
146+
"`SocketSubProtocol`is deprecated and will be removed in the version 5.2.0, consider to use `GraphQLProtocol`")
146147
class SocketSubProtocol {
147148
SocketSubProtocol._();
149+
148150
/// graphql-ws: The new (not to be confused with the graphql-ws library).
149151
/// NB. This protocol is it no longer maintained, please consider
150152
/// to use `SocketSubProtocol.graphqlTransportWs`.
151153
static const String graphqlWs = GraphQLProtocol.graphqlWs;
152-
/// graphql-transport-ws: New ws protocol used by most Apollo Server instances
154+
155+
/// graphql-transport-ws: New ws protocol used by most Apollo Server instances
153156
/// with subscriptions enabled use this library.
154-
/// N.B: not to be confused with the graphql-ws library that implement the
157+
/// N.B: not to be confused with the graphql-ws library that implement the
155158
/// old ws protocol.
156159
static const String graphqlTransportWs = GraphQLProtocol.graphqlWs;
157160
}
158161

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`.
159169
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.
160175
static const String graphqlTransportWs = "graphql-transport-ws";
161176
}
162177

@@ -171,7 +186,7 @@ class SocketSubProtocol {
171186
class SocketClient {
172187
SocketClient(
173188
this.url, {
174-
this.protocol = SocketSubProtocol.graphqlWs,
189+
this.protocol = GraphQLProtocol.graphqlWs,
175190
this.config = const SocketClientConfig(),
176191
@visibleForTesting this.randomBytesForUuid,
177192
@visibleForTesting this.onMessage,
@@ -259,14 +274,14 @@ class SocketClient {
259274
await config.connect(uri: Uri.parse(url), protocols: [protocol]);
260275
socketChannel = connection.forGraphQL();
261276

262-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
277+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
263278
_connectionStateController.add(SocketConnectionState.handshake);
264279
} else {
265280
_connectionStateController.add(SocketConnectionState.connected);
266281
}
267282
print('Initialising connection');
268283
_write(initOperation);
269-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
284+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
270285
// wait for ack
271286
// this blocks to prevent ping from being called before ack is recieved
272287
await _messages.firstWhere(
@@ -275,10 +290,10 @@ class SocketClient {
275290
}
276291

277292
if (config.inactivityTimeout != null) {
278-
if (protocol == SocketSubProtocol.graphqlWs) {
293+
if (protocol == GraphQLProtocol.graphqlWs) {
279294
_disconnectOnKeepAliveTimeout(_messages);
280295
}
281-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
296+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
282297
_enqueuePing();
283298
}
284299
}
@@ -289,7 +304,7 @@ class SocketClient {
289304
onMessage!(message);
290305
}
291306

292-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
307+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
293308
if (message.type == 'ping') {
294309
_write(PongMessage());
295310
} else if (message.type == 'pong') {
@@ -506,7 +521,7 @@ class SocketClient {
506521
id,
507522
serialize(payload),
508523
);
509-
if (protocol == SocketSubProtocol.graphqlTransportWs) {
524+
if (protocol == GraphQLProtocol.graphqlTransportWs) {
510525
operation = SubscribeOperation(
511526
id,
512527
serialize(payload),
@@ -524,7 +539,7 @@ class SocketClient {
524539
_subscriptionInitializers.remove(id);
525540

526541
sub?.cancel();
527-
if (protocol == SocketSubProtocol.graphqlWs &&
542+
if (protocol == GraphQLProtocol.graphqlWs &&
528543
_connectionStateController.value == SocketConnectionState.connected &&
529544
socketChannel != null) {
530545
_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)