Skip to content

Commit

Permalink
Merge pull request zino-hofmann#277 from micimize/ws-init-op
Browse files Browse the repository at this point in the history
Loosen websocket init constraints
  • Loading branch information
HofmannZ authored May 20, 2019
2 parents 22ff826 + 23ee826 commit 11bb328
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
See [GitHub Releases](https://github.com/zino-app/graphql-flutter/releases).

* Loosened `initPayload` to `dynamic` to support many use-cases,
Removed `InitOperation`'s excessive and inconsistent json encoding.
Old implmentation can still be utilized as `legacyInitPayload`
until deprecation

* Fixed broken anonymous operations
5 changes: 4 additions & 1 deletion packages/graphql/lib/src/core/raw_operation_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class RawOperationData {
Map<String, dynamic> variables;

String _operationName;
String _documentIdentifier;

/// The last operation name appearing in the contained document.
String get operationName {
Expand All @@ -34,6 +33,10 @@ class RawOperationData {
return _operationName;
}

String _documentIdentifier;

/// The client identifier for this operation,
// TODO remove $document from key? A bit redundant, though that's not the worst thing
String get _identifier {
_documentIdentifier ??=
operationName ?? 'UNNAMED/' + document.hashCode.toString();
Expand Down
23 changes: 20 additions & 3 deletions packages/graphql/lib/src/socket_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SocketClientConfig {
this.delayBetweenReconnectionAttempts = const Duration(seconds: 5),
this.compression = CompressionOptions.compressionDefault,
this.initPayload,
@deprecated this.legacyInitPayload,
});

/// Whether to reconnect to the server after detecting connection loss.
Expand All @@ -39,8 +40,24 @@ class SocketClientConfig {

final CompressionOptions compression;

/// The initial payload that will be sent to the server upon connection. Can be null.
final Map<String, String> initPayload;
/// The initial payload that will be sent to the server upon connection.
/// Can be null, but must be a valid json structure if provided.
final dynamic initPayload;

final Map<String, dynamic> legacyInitPayload;

InitOperation get initOperation {
if (legacyInitPayload != null) {
print(
'WARNING: Using a legacyInitPayload which will be removed soon. '
'If you need this particular payload serialization behavior, '
'please comment on this issue with details on your usecase: '
'https://github.com/zino-app/graphql-flutter/pull/277',
);
return LegacyInitOperation(legacyInitPayload);
}
return InitOperation(initPayload);
}
}

enum SocketConnectionState { NOT_CONNECTED, CONNECTING, CONNECTED }
Expand Down Expand Up @@ -100,7 +117,7 @@ class SocketClient {
compression: config.compression);
_connectionStateController.value = SocketConnectionState.CONNECTED;
print('Connected to websocket.');
_write(InitOperation(config.initPayload));
_write(config.initOperation);

_messageStream = _socket
.asBroadcastStream()
Expand Down
21 changes: 20 additions & 1 deletion packages/graphql/lib/src/websocket/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,26 @@ abstract class GraphQLSocketMessage extends JsonSerializable {
class InitOperation extends GraphQLSocketMessage {
InitOperation(this.payload) : super(MessageTypes.GQL_CONNECTION_INIT);

final Map<String, String> payload;
final dynamic payload;

@override
dynamic toJson() {
final Map<String, dynamic> jsonMap = <String, dynamic>{};
jsonMap['type'] = type;

if (payload != null) {
jsonMap['payload'] = payload;
}

return jsonMap;
}
}

@deprecated

/// The old implementation of [InitOperation]
class LegacyInitOperation extends InitOperation {
LegacyInitOperation(dynamic payload) : super(payload);

@override
dynamic toJson() {
Expand Down

0 comments on commit 11bb328

Please sign in to comment.