Skip to content

Commit a662c7c

Browse files
author
Gabor
committed
Merge branch 'main' of github.com:zino-hofmann/graphql-flutter
2 parents 21a1953 + baf555e commit a662c7c

File tree

27 files changed

+584
-91
lines changed

27 files changed

+584
-91
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
Describe the purpose of the pull request.
1+
There are some simple steps to get your PR merged, that are the following:
2+
3+
- Describe your PR and why a maintainer should merge it;
4+
- Put the same description inside the commit body otherwise if we change github hosting you application will be based on a instable source code;
5+
- Write the commit header in the way that it is following these simple rules [1]
6+
- Make sure that your PR will pass the CI
7+
- Wait for an review :smile: that will not happen if one of the previous step is missing.
8+
9+
[1](https://github.com/zino-hofmann/graphql-flutter/blob/main/docs/dev/MAINTAINERS.md#commit-style)
10+
211
<!--
312
### Breaking changes
413
@@ -14,4 +23,4 @@ Describe the purpose of the pull request.
1423
1524
- Added ... .
1625
- Updated ... .
17-
-->
26+
-->

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ packages/graphql_flutter/example/android/app/.settings/
3131
packages/graphql/example/android
3232
packages/graphql/example/ios
3333

34-
3534
#
3635
# Flutter
3736
#

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ CC_CHANGELOG=dart pub global run changelog_cmd
55
default: analyze check
66

77
dep:
8-
dart pub global activate melos;
9-
# dart pub global activate changelog_cmd;
8+
dart pub global activate melos 2.9.0;
9+
dart pub global activate changelog_cmd;
1010
$(CC) bootstrap
1111

1212
check: ci_check_client ci_check_flutter

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ Around `graphql_flutter` are builds awesome tools like:
5050

5151
1. [graphql_flutter_bloc](https://github.com/artflutter/graphql_flutter_bloc)
5252
2. [graphql_codegen](https://github.com/heftapp/graphql_codegen)
53+
3. [graphql-cache-inspector](https://pub.dev/packages/graphql_cache_inspector)
5354

5455
## Features
56+
5557
&nbsp; Queries, Mutations, and Subscriptions
5658
&nbsp; [Query polling and rebroadcasting](./packages/graphql/README.md#clientwatchquery-and-observablequery)
5759
&nbsp; [In memory and persistent caching](./packages/graphql/README.md#persistence)

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

examples/starwars/pubspec.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ description: An example graphql_flutter application utilizing graphql_starwars_t
44
publish_to: none
55

66
environment:
7-
sdk: ">=2.10.0 <3.0.0"
7+
sdk: ">=2.13.0 <4.0.0"
88

99
dependencies:
1010
flutter:
1111
sdk: flutter
1212
graphql_flutter:
1313
path: ../../packages/graphql_flutter
1414
graphql: ^5.1.2-beta.1
15-
universal_platform:
16-
^0.1.3
15+
universal_platform: ^1.0.0+1
1716
# https://github.com/flutter/flutter/issues/36126#issuecomment-596215587
1817

1918
flutter:
20-
uses-material-design: true
19+
uses-material-design: true

examples/starwars/server/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: starwars_server
33
publish_to: none
44

55
environment:
6-
sdk: ">=2.10.0 <3.0.0"
6+
sdk: ">=2.12.0 <4.0.0"
77

88
dependencies:
99
graphql_starwars_test_server: ^0.1.0

packages/graphql/CHANGELOG.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
# v5.2.0-beta.6
2+
3+
## Fixed
4+
- Send SubscriptionComplete message when using graphqlTra… ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/6e73d62ba2b8a58a35b3b18e372003462a73e192)). @ 30-08-2023
5+
6+
## Added
7+
- Send custom payload in PingMessage ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/19d5c86b98e889b333996da43126f9404a9a4556)). @Rochak69 31-08-2023
8+
- added WebSocket token refresh and autoReconnect toggling ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/e1c6d5404be2ff54f916bceab6bb52a04bae5d01)). @vytautas-pranskunas- 24-07-2023
9+
10+
11+
# v5.2.0-beta.5
12+
13+
## Fixed
14+
- bumps http to v1 ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/396c3b3f6986b6d3174e548982a93188b49ee5bc)). @moisessantos 06-07-2023
15+
16+
17+
# v5.2.0-beta.4
18+
19+
## Added
20+
- Cache parsed data ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/aa81251f71f7a5f566eae4a9575eb6547050c2d9)). @budde377 03-06-2023
21+
22+
23+
# v5.2.0-beta.3
24+
25+
## Added
26+
- bump sdk version upper bound to <4.0.0 ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/8bb9ba355e53dccf5e291b1f05171459bf8867ed)). @ndelanou 17-05-2023
27+
28+
29+
# v5.2.0-beta.2
30+
31+
## Added
32+
- bump gql version to 1.0.0 ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/6c991d8e8dc952f9cf0774746ce3d59f48f1ea32)). @ndelanou 03-05-2023
33+
- print the connection status with ws ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/a9aefc41909eb6911c4c4de1d0477bb620d1098d)). @vytautas-pranskunas- 15-03-2023
34+
35+
36+
# v5.2.0-beta.1
37+
38+
## Fixed
39+
- onComplete callback should send null ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/aceff150bf8c676207f16aa07f9622dfc7e56e9d)). @budde377 12-03-2023
40+
- upgrade the ws packege to v2.3.0 ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/e736f5bf1b1226c5607b2c07e2f3e83b6cb18945)). @vincenzopalazzo 15-01-2023
41+
- remove unecessary deprecation code ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/6deede934fb85c735b14c28b737319581bda6f16)). @vincenzopalazzo 15-01-2023
42+
- support @include and @skip with new normalize major ver ([commit](https://github.com/zino-hofmann/graphql-flutter/commit/c31b82ab7b516fd1d1e1ed735fa17a605caea441)). @vincenzopalazzo 15-01-2023
43+
44+
145
# v5.1.3
246

347
## Fixed

packages/graphql/README.md

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,87 @@ subscription = client.subscribe(
327327
subscription.listen(reactToAddedReview)
328328
```
329329

330+
#### Adding headers (including auth) to WebSocket
331+
332+
In order to add auth header or any other header to websocket connection use `initialPayload` property
333+
334+
```dart
335+
initialPayload: () {
336+
var headers = <String, String>{};
337+
headers.putIfAbsent(HttpHeaders.authorizationHeader, () => token);
338+
339+
return headers;
340+
},
341+
```
342+
343+
#### Refreshing headers (including auth)
344+
345+
In order to refresh auth header you need to setup `onConnectionLost` function
346+
347+
```dart
348+
onConnectionLost: (int? code, String? reason) async {
349+
if (code == 4001) {
350+
await authTokenService.issueToken(refresh: true);
351+
return Duration.zero;
352+
}
353+
354+
return null;
355+
}
356+
```
357+
358+
Where `code` and `reason` are values returned from the server on connection close. There is no such code like 401 in WebSockets so you can use your custom and server code could look similar:
359+
360+
```typescript
361+
subscriptions: {
362+
'graphql-ws': {
363+
onConnect: async (context: any) => {
364+
const { connectionParams } = context;
365+
366+
if (!connectionParams) {
367+
throw new Error('Connection params are missing');
368+
}
369+
370+
const authToken = connectionParams.authorization;
371+
372+
if (authToken) {
373+
const isValid await authService.isTokenValid(authToken);
374+
375+
if (!isValid) {
376+
context.extra.socket.close(4001, 'Unauthorized');
377+
}
378+
379+
return;
380+
}
381+
},
382+
},
383+
},
384+
```
385+
386+
`onConnectionLost` function returns `Duration` which is basically `delayBetweenReconnectionAttempts` for current reconnect attempt. If duration is `null` then default `delayBetweenReconnectionAttempts` will be used. Otherwise returned value. For example upon expired auth token there is not much sense to wait after token is refreshed.
387+
388+
#### Handling connection manually
389+
390+
`toggleConnection` stream was introduced to allow connect or disconnect manually.
391+
392+
```dart
393+
var toggleConnection = PublishSubject<ToggleConnectionState>;
394+
395+
SocketClientConfig(
396+
toggleConnection: toggleConnection,
397+
),
398+
```
399+
400+
later from your code call
401+
402+
```dart
403+
toggleConnection.add(ToggleConnectionState.disconnect);
404+
//OR
405+
toggleConnection.add(ToggleConnectionState.connect);
406+
```
407+
408+
When `disconnect` event is called `autoReconnect` stops. When `connect` is called `autoReconnect` resumes.
409+
this is useful when for some reason you want to stop reconnection. For example when user logouts from the system and reconnection would cause auth error from server causing infinite loop.
410+
330411
#### Customizing WebSocket Connections
331412

332413
`WebSocketLink` now has an experimental `connect` parameter that can be
@@ -427,15 +508,15 @@ class _Connection {
427508
428509
```
429510

430-
2- if you need to update your socket just cancel your subscription and resubscribe again using usual way
511+
2- if you need to update your socket just cancel your subscription and resubscribe again using usual way
431512
and if the token changed it will be reconnect with the new token otherwise it will use the same client
432513

433514

434515

435516
### `client.watchQuery` and `ObservableQuery`
436517

437518
[`client.watchQuery`](https://pub.dev/documentation/graphql/latest/graphql/GraphQLClient/watchQuery.html)
438-
can be used to execute both queries and mutations, then reactively listen to changes to the underlying data in the cache.
519+
can be used to execute both queries and mutations, then reactively listen to changes to the underlying data in the cache.
439520

440521
```dart
441522
final observableQuery = client.watchQuery(
@@ -506,7 +587,7 @@ To disable cache normalization entirely, you could pass `(data) => null`.
506587
If you only cared about `nodeId`, you could pass `(data) => data['nodeId']`.
507588

508589
Here's a more detailed example where the system involved contains versioned entities you don't want to clobber:
509-
```dart
590+
```dart
510591
String customDataIdFromObject(Map<String, Object> data) {
511592
final typeName = data['__typename'];
512593
final entityId = data['entityId'];
@@ -589,17 +670,17 @@ query {
589670

590671
```
591672

592-
if you're not providing the possible type map and introspecting the typename, the cache can't be updated.
673+
if you're not providing the possible type map and introspecting the typename, the cache can't be updated.
593674

594675
## Direct Cache Access API
595676

596677
The [`GraphQLCache`](https://pub.dev/documentation/graphql/latest/graphql/GraphQLCache-class.html)
597678
leverages [`normalize`] to give us a fairly apollo-ish [direct cache access] API, which is also available on `GraphQLClient`.
598679
This means we can do [local state management] in a similar fashion as well.
599680

600-
The cache access methods are available on any cache proxy, which includes the `GraphQLCache` the `OptimisticProxy` passed to `update` in the `graphql_flutter` `Mutation` widget, and the `client` itself.
681+
The cache access methods are available on any cache proxy, which includes the `GraphQLCache` the `OptimisticProxy` passed to `update` in the `graphql_flutter` `Mutation` widget, and the `client` itself.
601682
> **NB** counter-intuitively, you likely never want to use use direct cache access methods directly on the `cache`,
602-
> as they will not be rebroadcast automatically.
683+
> as they will not be rebroadcast automatically.
603684
> **Prefer `client.writeQuery` and `client.writeFragment` to those on the `client.cache` for automatic rebroadcasting**
604685
605686
In addition to this overview, a complete and well-commented rundown of can be found in the
@@ -641,10 +722,10 @@ final data = client.readQuery(queryRequest);
641722
client.writeQuery(queryRequest, data);
642723
```
643724

644-
The cache access methods are available on any cache proxy, which includes the `GraphQLCache` the `OptimisticProxy` passed to `update` in the `graphql_flutter` `Mutation` widget, and the `client` itself.
645-
> **NB** counter-intuitively, you likely never want to use use direct cache access methods on the cache
725+
The cache access methods are available on any cache proxy, which includes the `GraphQLCache` the `OptimisticProxy` passed to `update` in the `graphql_flutter` `Mutation` widget, and the `client` itself.
726+
> **NB** counter-intuitively, you likely never want to use use direct cache access methods on the cache
646727
cache.readQuery(queryRequest);
647-
client.readQuery(queryRequest); //
728+
client.readQuery(queryRequest); //
648729

649730
### `FragmentRequest`, `readFragment`, and `writeFragment`
650731
`FragmentRequest` has almost the same api as `Request`, but is provided directly from `graphql` for consistency.
@@ -710,7 +791,7 @@ client.query(QueryOptions(
710791
errorPolicy: ErrorPolicy.ignore,
711792
// ignore cache data.
712793
cacheRereadPolicy: CacheRereadPolicy.ignore,
713-
// ...
794+
// ...
714795
));
715796
```
716797
Defaults can also be overridden via `defaultPolices` on the client itself:
@@ -724,11 +805,11 @@ GraphQLClient(
724805
CacheRereadPolicy.mergeOptimistic,
725806
),
726807
),
727-
// ...
808+
// ...
728809
)
729810
```
730811

731-
**[`FetchPolicy`](https://pub.dev/documentation/graphql/latest/graphql/FetchPolicy-class.html):** determines where the client may return a result from, and whether that result will be saved to the cache.
812+
**[`FetchPolicy`](https://pub.dev/documentation/graphql/latest/graphql/FetchPolicy-class.html):** determines where the client may return a result from, and whether that result will be saved to the cache.
732813
Possible options:
733814

734815
- cacheFirst: return result from cache. Only fetch from network if cached result is not available.
@@ -737,7 +818,7 @@ Possible options:
737818
- noCache: return result from network, fail if network call doesn't succeed, don't save to cache.
738819
- networkOnly: return result from network, fail if network call doesn't succeed, save to cache.
739820

740-
**[`ErrorPolicy`](https://pub.dev/documentation/graphql/latest/graphql/ErrorPolicy-class.html):** determines the level of events for errors in the execution result.
821+
**[`ErrorPolicy`](https://pub.dev/documentation/graphql/latest/graphql/ErrorPolicy-class.html):** determines the level of events for errors in the execution result.
741822
Possible options:
742823

743824
- none (default): Any GraphQL Errors are treated the same as network errors and any data is ignored from the response.
@@ -869,7 +950,7 @@ API key, IAM, and Federated provider authorization could be accomplished through
869950

870951
This package does not support code-generation out of the box, but [graphql_codegen](https://pub.dev/packages/graphql_codegen) does!
871952

872-
This package extensions on the client which takes away the struggle of serialization and gives you confidence through type-safety.
953+
This package extensions on the client which takes away the struggle of serialization and gives you confidence through type-safety.
873954
It is also more performant than parsing GraphQL queries at runtime.
874955

875956
For example, by creating the `.graphql` file
@@ -966,3 +1047,4 @@ HttpLink httpLink = HttpLink('https://api.url/graphql', defaultHeaders: {
9661047
[local state management]: https://www.apollographql.com/docs/tutorial/local-state/#update-local-data
9671048
[`typepolicies`]: https://www.apollographql.com/docs/react/caching/cache-configuration/#the-typepolicy-type
9681049
[direct cache access]: https://www.apollographql.com/docs/react/caching/cache-interaction/
1050+

packages/graphql/changelog.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"package_name": "graphql",
3-
"version": "v5.1.3",
3+
"version": "v5.2.0-beta.6",
44
"api": {
55
"name": "github",
66
"repository": "zino-hofmann/graphql-flutter",
7-
"branch": "macros/ws-release"
7+
"branch": "main"
88
},
99
"generation_method": {
1010
"name": "semver-v2",

packages/graphql/example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void readRepositories() async {
7878
result.data!['viewer']['repositories']['nodes'] as List<dynamic>;
7979

8080
repositories.forEach(
81-
(dynamic f) => {stdout.writeln('Id: ${f['id']} Name: ${f['name']}')},
81+
(dynamic f) => stdout.writeln('Id: ${f['id']} Name: ${f['name']}'),
8282
);
8383

8484
exit(0);

packages/graphql/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ version: 1.0.0+1
55
publish_to: none
66

77
environment:
8-
sdk: '>=2.12.0 <3.0.0'
8+
sdk: '>=2.12.0 <4.0.0'
99

1010
dependencies:
1111
args:

packages/graphql/lib/src/core/query_options.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:gql/ast.dart';
99
import 'package:graphql/client.dart';
1010
import 'package:meta/meta.dart';
1111

12-
typedef OnQueryComplete = FutureOr<void> Function(Map<String, dynamic> data);
12+
typedef OnQueryComplete = FutureOr<void> Function(Map<String, dynamic>? data);
1313

1414
typedef OnQueryError = FutureOr<void> Function(OperationException? error);
1515

@@ -366,7 +366,7 @@ class QueryCallbackHandler<TParsed> {
366366
if (options.onComplete != null) {
367367
return (QueryResult? result) {
368368
if (!result!.isLoading && !result.isOptimistic) {
369-
return options.onComplete!(result.data ?? {});
369+
return options.onComplete!(result.data);
370370
}
371371
};
372372
}

0 commit comments

Comments
 (0)