Skip to content

Commit 80947a0

Browse files
repo: formatting code
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 99d3b81 commit 80947a0

File tree

6 files changed

+94
-42
lines changed

6 files changed

+94
-42
lines changed

packages/graphql_flutter/example/graphql_chat/lib/api/query.dart

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,35 @@ class Queries {
55
getChats {
66
__typename
77
id
8-
message
8+
description
99
name
1010
}
1111
}
1212
""";
1313
}
1414

15-
static String createChatMutation({required String name, required String message}) {
15+
static String createChatMutation(
16+
{required String name, required String description}) {
1617
return """
1718
mutation {
18-
createChat(name: $name, message: $message) {
19+
createChat(name: $name, description: $description) {
1920
id
2021
name
21-
message
22+
description
2223
}
2324
}
2425
""";
2526
}
26-
}
27+
28+
static String subscribeToNewChat() {
29+
return """
30+
subscription {
31+
chatCreated {
32+
id
33+
name
34+
description
35+
}
36+
}
37+
""";
38+
}
39+
}

packages/graphql_flutter/example/graphql_chat/lib/main.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ Future<void> main() async {
1414
},
1515
'http://127.0.0.1:9000/graphql',
1616
);
17+
var wsLink = WebSocketLink(
18+
'ws://127.0.0.1:9000/graphql',
19+
config: const SocketClientConfig(
20+
inactivityTimeout: Duration(seconds: 40),
21+
),
22+
subProtocol: SocketSubProtocol.graphqlTransportWs,
23+
);
24+
final Link link = httpLink.split(
25+
(request) => request.isSubscription,
26+
wsLink,
27+
httpLink,
28+
);
1729
ValueNotifier<GraphQLClient> client = ValueNotifier(
1830
GraphQLClient(
1931
link: httpLink,
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
21
class Chat {
32
final int id;
43
final String name;
5-
final String message;
4+
final String description;
65

7-
const Chat({required this.id, required this.name, required this.message});
6+
const Chat({required this.id, required this.name, required this.description});
87

98
factory Chat.fromJSON(Map<String, dynamic> json) {
10-
return Chat(id: json["id"], name: json["name"], message: json["message"]);
9+
return Chat(
10+
id: json["id"], name: json["name"], description: json["description"]);
1111
}
12-
}
12+
}
Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
<<<<<<< HEAD
2-
=======
3-
import 'dart:convert';
4-
5-
>>>>>>> feat(graphql_flutter): add graphql flutter example
6-
import 'package:flutter/cupertino.dart';
1+
import 'package:flutter/material.dart';
72
import 'package:graphql_chat/api/query.dart';
83
import 'package:graphql_chat/model/chat.dart';
94
import 'package:graphql_flutter/graphql_flutter.dart';
105
import 'package:logger/logger.dart';
116

127
class HomeViewBody extends StatelessWidget {
138
final Logger _logger = Logger();
9+
List<Chat> _chats = [];
10+
1411
HomeViewBody({Key? key}) : super(key: key);
1512

1613
@override
@@ -26,30 +23,61 @@ class HomeViewBody extends StatelessWidget {
2623
Widget _buildScrollView({required BuildContext context}) {
2724
return Query(
2825
options: QueryOptions<List<Chat>>(
26+
fetchPolicy: FetchPolicy.networkOnly,
27+
parserFn: (Map<String, dynamic> json) {
28+
var rawList = List.of(json["getChats"]);
29+
return rawList
30+
.map((jsonChat) => Chat.fromJSON(jsonChat))
31+
.toList();
32+
},
33+
document: gql(Queries.getGetQuery())),
34+
builder: (QueryResult result,
35+
{VoidCallback? refetch, FetchMore? fetchMore}) {
36+
if (result.hasException) {
37+
_logger.e(result.exception);
38+
throw Exception(result.exception);
39+
}
40+
if (result.isLoading) {
41+
_logger.i("Still loading");
42+
return const Text("Loading chats");
43+
}
44+
_logger.d(result.data ?? "Data is undefined");
45+
_chats = result.parsedData as List<Chat>;
46+
return ListView(
47+
children:
48+
_chats.map((chatData) => Text(chatData.description)).toList(),
49+
);
50+
});
51+
}
52+
53+
Widget _buildUpdateScrollView({required BuildContext context}) {
54+
return Subscription(
55+
options: SubscriptionOptions(
2956
parserFn: (Map<String, dynamic> json) {
30-
var rawList = List.of(json["getChats"]);
31-
return rawList.map((jsonChat) => Chat.fromJSON(jsonChat)).toList();
57+
return Chat.fromJSON(json["chatCreated"]);
3258
},
33-
document: gql(Queries.getGetQuery())
34-
), builder: (QueryResult result, { VoidCallback? refetch, FetchMore? fetchMore }) {
35-
if (result.hasException) {
36-
_logger.e(result.exception);
37-
throw Exception(result.exception);
38-
}
39-
if (result.isLoading) {
40-
_logger.i("Still loading");
41-
return const Text("Loading chats");
42-
}
43-
_logger.d(result.data ?? "Data is undefined");
44-
var chats = result.parsedData as List<Chat>;
45-
<<<<<<< HEAD
46-
return ListView(
47-
children: chats.map((chatData) => Text(chatData.message)).toList(),
48-
);
49-
=======
50-
return Text("Somethings is returned : ${jsonEncode(chats)}");
51-
>>>>>>> feat(graphql_flutter): add graphql flutter example
52-
});
59+
document: gql(Queries.subscribeToNewChat()),
60+
),
61+
builder: (result) {
62+
if (result.hasException) {
63+
_logger.e(result.exception);
64+
return Text(result.exception.toString());
65+
}
66+
67+
if (result.isLoading) {
68+
return const Center(
69+
child: CircularProgressIndicator(),
70+
);
71+
}
72+
// ResultAccumulator is a provided helper widget for collating subscription results.
73+
_logger.d(result.data ?? "Data is undefined");
74+
var chat = result.parsedData as Chat;
75+
return ResultAccumulator.appendUniqueEntries(
76+
latest: _chats,
77+
builder: (context, {results}) =>
78+
ListView(children: [Text(chat.name)]),
79+
);
80+
});
5381
}
5482

5583
/// Build the scroll view with all the information
@@ -60,5 +88,4 @@ class HomeViewBody extends StatelessWidget {
6088
],
6189
);
6290
}
63-
64-
}
91+
}

packages/graphql_flutter/example/graphql_chat/lib/view/home_view.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ class _HomeViewState extends State<HomeView> {
8181
},
8282
);
8383
}
84-
}
84+
}

packages/graphql_flutter/example/graphql_chat/test/widget_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
import 'package:flutter_test/flutter_test.dart';
99

1010
void main() {
11-
testWidgets('Counter increments smoke test', (WidgetTester tester) async { });
11+
testWidgets('Counter increments smoke test', (WidgetTester tester) async {});
1212
}

0 commit comments

Comments
 (0)