Skip to content

Commit

Permalink
Merge branch 'zino-hofmann:main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
dehypnosis authored and vincenzopalazzo committed Jul 22, 2022
2 parents b290073 + 360616d commit 3fa1aea
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/packages_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
release:
types: [created]
tags:
- v-*
- v-packages-*
jobs:
publishing_client:
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions packages/graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v5.1.2-beta.3

## Fixes
- Transform nested objects ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/92e495dacc5f7c23a648a4053ee1bd73fb9b924e)). @budde377 27-06-2022
- send connection_init message during handshake ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/e1b7f821d4727f70e64dd334e45f6c65a063adfd)). @othorin 20-05-2022


# v5.1.2-beta.2

## New Feature
Expand Down
14 changes: 14 additions & 0 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,20 @@ final HttpLink _httpLink = HttpLink(
final Link _link = _apqLink.concat(_httpLink);
```

## Q&A
### CSRF Error while uploading MultipartFile
If you are receiving csrf error from your apollo graphql server while uploading file,
your need to add some additional headers to the `HttpLink`:
<br>
Also ensure that you're add `contentType` to `MultipartFile` as `MediaType`

```dart
HttpLink httpLink = HttpLink('https://api.url/graphql', defaultHeaders: {
'Content-Type': 'application/json; charset=utf-8',
'X-Apollo-Operation-Name': 'post'
})
```

[build-status-badge]: https://img.shields.io/github/workflow/status/zino-hofmann/graphql-flutter/graphql-flutter%20Tests%20case?style=flat-square
[build-status-link]: https://github.com/zino-hofmann/graphql-flutter/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/zino-hofmann/graphql-flutter/beta?style=flat-square
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/changelog.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"package_name": "graphql",
"version": "v5.1.2-beta.2",
"version": "v5.1.2-beta.3",
"api": {
"name": "github",
"repository": "zino-hofmann/graphql-flutter",
Expand Down
25 changes: 24 additions & 1 deletion packages/graphql/lib/src/cache/hive_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ import 'package:hive/hive.dart';

import './store.dart';

Map<String, dynamic> _transformMap(Map<dynamic, dynamic> map) =>
Map<String, dynamic>.from(
{
for (var entry in map.entries) entry.key: _transformAny(entry.value),
},
);

dynamic _transformAny(dynamic object) {
if (object is Map) {
return _transformMap(object);
}
if (object is List) {
return _transformList(object);
}
return object;
}

List<dynamic> _transformList(List<dynamic> list) => List<dynamic>.from(
[
for (var element in list) _transformAny(element),
],
);

@immutable
class HiveStore extends Store {
/// Default box name for the `graphql/client.dart` cache store (`graphqlClientStore`)
Expand Down Expand Up @@ -50,7 +73,7 @@ class HiveStore extends Store {
Map<String, dynamic>? get(String dataId) {
final result = box.get(dataId);
if (result == null) return null;
return Map<String, dynamic>.from(result);
return _transformMap(result);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,37 @@ class SocketClientConfig {
}
}

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

/// graphql-ws: The new (not to be confused with the graphql-ws library).
/// NB. This protocol is it no longer maintained, please consider
/// to use `SocketSubProtocol.graphqlTransportWs`.
static const String graphqlWs = GraphQLProtocol.graphqlWs;

/// graphql-transport-ws: New ws protocol used by most Apollo Server instances
/// with subscriptions enabled use this library.
/// N.B: not to be confused with the graphql-ws library that implement the
/// old ws protocol.
static const String graphqlTransportWs = GraphQLProtocol.graphqlWs;
}

/// ALL protocol supported by the library
class GraphQLProtocol {
GraphQLProtocol._();

/// graphql-ws: The new (not to be confused with the graphql-ws library).
/// NB. This protocol is it no longer maintained, please consider
/// to use `SocketSubProtocol.graphqlTransportWs`.
static const String graphqlWs = "graphql-ws";

/// graphql-transport-ws: New ws protocol used by most Apollo Server instances
/// with subscriptions enabled use this library.
/// N.B: not to be confused with the graphql-ws library that implement the
/// old ws protocol.
static const String graphqlTransportWs = "graphql-transport-ws";
}

Expand All @@ -159,7 +186,7 @@ class SocketSubProtocol {
class SocketClient {
SocketClient(
this.url, {
this.protocol = SocketSubProtocol.graphqlWs,
this.protocol = GraphQLProtocol.graphqlWs,
this.config = const SocketClientConfig(),
@visibleForTesting this.randomBytesForUuid,
@visibleForTesting this.onMessage,
Expand Down Expand Up @@ -247,14 +274,14 @@ class SocketClient {
await config.connect(uri: Uri.parse(url), protocols: [protocol]);
socketChannel = connection.forGraphQL();

if (protocol == SocketSubProtocol.graphqlTransportWs) {
if (protocol == GraphQLProtocol.graphqlTransportWs) {
_connectionStateController.add(SocketConnectionState.handshake);
} else {
_connectionStateController.add(SocketConnectionState.connected);
}
print('Initialising connection');
_write(initOperation);
if (protocol == SocketSubProtocol.graphqlTransportWs) {
if (protocol == GraphQLProtocol.graphqlTransportWs) {
// wait for ack
// this blocks to prevent ping from being called before ack is recieved
await _messages.firstWhere(
Expand All @@ -263,10 +290,10 @@ class SocketClient {
}

if (config.inactivityTimeout != null) {
if (protocol == SocketSubProtocol.graphqlWs) {
if (protocol == GraphQLProtocol.graphqlWs) {
_disconnectOnKeepAliveTimeout(_messages);
}
if (protocol == SocketSubProtocol.graphqlTransportWs) {
if (protocol == GraphQLProtocol.graphqlTransportWs) {
_enqueuePing();
}
}
Expand All @@ -277,7 +304,7 @@ class SocketClient {
onMessage!(message);
}

if (protocol == SocketSubProtocol.graphqlTransportWs) {
if (protocol == GraphQLProtocol.graphqlTransportWs) {
if (message.type == 'ping') {
_write(PongMessage());
} else if (message.type == 'pong') {
Expand Down Expand Up @@ -494,7 +521,7 @@ class SocketClient {
id,
serialize(payload),
);
if (protocol == SocketSubProtocol.graphqlTransportWs) {
if (protocol == GraphQLProtocol.graphqlTransportWs) {
operation = SubscribeOperation(
id,
serialize(payload),
Expand All @@ -512,7 +539,7 @@ class SocketClient {
_subscriptionInitializers.remove(id);

sub?.cancel();
if (protocol == SocketSubProtocol.graphqlWs &&
if (protocol == GraphQLProtocol.graphqlWs &&
_connectionStateController.value == SocketConnectionState.connected &&
socketChannel != null) {
_write(StopOperation(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WebSocketLink extends Link {
WebSocketLink(
this.url, {
this.config = const SocketClientConfig(),
this.subProtocol = SocketSubProtocol.graphqlWs,
this.subProtocol = GraphQLProtocol.graphqlWs,
});

final String url;
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: graphql
description: A stand-alone GraphQL client for Dart, bringing all the features from a modern GraphQL client to one easy to use package.
version: 5.1.2-beta.2
version: 5.1.2-beta.3
homepage: https://github.com/zino-app/graphql-flutter/tree/master/packages/graphql

dependencies:
Expand Down
29 changes: 29 additions & 0 deletions packages/graphql/test/cache/store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ void main() {
expect(readData2, equals({'foo': 'bar'}));
expect(readData2, isA<Map<String, dynamic>>());
});
test("Can re-open and read nested data", () async {
final box1 = await HiveStore.openBox(
're-open-store',
path: path,
);
final store = HiveStore(box1);
final data = {
'foo': 'bar',
'bob': [
{'nested': true}
]
};
store.put("id", data);
final readData = await store.get("id");
expect(readData, equals(data));
expect(readData?['bob'], isA<List<dynamic>>());
expect(readData?['bob'][0], isA<Map<String, dynamic>>());
await box1.close();
final box2 = await HiveStore.openBox(
're-open-store',
path: path,
);
final store2 = HiveStore(box2);
final readData2 = await store2.get('id');
expect(readData2, equals(data));
expect(readData2, isA<Map<String, dynamic>>());
expect(readData2?['bob'], isA<List<dynamic>>());
expect(readData2?['bob'][0], isA<Map<String, dynamic>>());
});
test("Can put null", () async {
final box1 = await HiveStore.openBox(
'put-null',
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/test/websocket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SocketClient getTestClient({
bool autoReconnect = true,
Map<String, dynamic>? customHeaders,
Duration delayBetweenReconnectionAttempts = const Duration(milliseconds: 1),
String protocol = SocketSubProtocol.graphqlWs,
String protocol = GraphQLProtocol.graphqlWs,
}) =>
SocketClient(
wsUrl,
Expand Down Expand Up @@ -340,7 +340,7 @@ Future<void> main() async {
controller = StreamController(sync: true);
socketClient = getTestClient(
controller: controller,
protocol: SocketSubProtocol.graphqlTransportWs,
protocol: GraphQLProtocol.graphqlTransportWs,
wsUrl: wsUrl,
);
}));
Expand Down

0 comments on commit 3fa1aea

Please sign in to comment.