@@ -3,14 +3,15 @@ import 'package:flutter/material.dart';
33
44class GraphQLService {
55 static ValueNotifier <GraphQLClient >? _client;
6- static const String _baseUrl = "https://redesigned-carnival-xp6v4wpj9pw2jv-4000.app.github.dev/" ;
6+ static const String _baseUrl =
7+ "https://redesigned-carnival-xp6v4wpj9pw2jv-4000.app.github.dev/" ;
78
89 /// Get or initialize GraphQL client
910 static ValueNotifier <GraphQLClient > getClient () {
1011 if (_client == null ) {
1112 // Initialize GraphQL client
1213 final HttpLink httpLink = HttpLink (_baseUrl);
13-
14+
1415 // Create the GraphQL client
1516 _client = ValueNotifier <GraphQLClient >(
1617 GraphQLClient (
@@ -19,16 +20,17 @@ class GraphQLService {
1920 ),
2021 );
2122 }
22-
23+
2324 return _client! ;
2425 }
2526
2627 /// Sign in with username and password
27- ///
28+ ///
2829 /// Returns user data and token on success, null on failure
29- static Future <Map <String , dynamic >?> signIn (String username, String password) async {
30+ static Future <Map <String , dynamic >?> signIn (
31+ String username, String password) async {
3032 final GraphQLClient client = getClient ().value;
31-
33+
3234 const String signInMutation = '''
3335 mutation SignIn(\$ username: String!, \$ password: String!) {
3436 signIn(username: \$ username, password: \$ password) {
@@ -40,15 +42,15 @@ class GraphQLService {
4042 }
4143 }
4244 ''' ;
43-
45+
4446 final MutationOptions options = MutationOptions (
4547 document: gql (signInMutation),
4648 variables: {
4749 'username' : username,
4850 'password' : password,
4951 },
5052 );
51-
53+
5254 try {
5355 final QueryResult result = await client.mutate (options);
5456 if (result.hasException) {
@@ -63,11 +65,12 @@ class GraphQLService {
6365 }
6466
6567 /// Sign up with name, username and password
66- ///
68+ ///
6769 /// Returns user data on success, null on failure
68- static Future <Map <String , dynamic >?> signUp (String name, String username, String password) async {
70+ static Future <Map <String , dynamic >?> signUp (
71+ String name, String username, String password) async {
6972 final GraphQLClient client = getClient ().value;
70-
73+
7174 const String signUpMutation = '''
7275 mutation SignUp(\$ name: String!, \$ username: String!, \$ password: String!) {
7376 signUp(name: \$ name, username: \$ username, password: \$ password) {
@@ -77,7 +80,7 @@ class GraphQLService {
7780 }
7881 }
7982 ''' ;
80-
83+
8184 final MutationOptions options = MutationOptions (
8285 document: gql (signUpMutation),
8386 variables: {
@@ -86,7 +89,7 @@ class GraphQLService {
8689 'password' : password,
8790 },
8891 );
89-
92+
9093 try {
9194 final QueryResult result = await client.mutate (options);
9295 if (result.hasException) {
@@ -99,23 +102,23 @@ class GraphQLService {
99102 return null ;
100103 }
101104 }
102-
105+
103106 /// Sign out current user
104- ///
107+ ///
105108 /// Returns success status
106109 static Future <bool > signOut () async {
107110 final GraphQLClient client = getClient ().value;
108-
111+
109112 const String signOutMutation = '''
110113 mutation SignOut {
111114 signOut
112115 }
113116 ''' ;
114-
117+
115118 final MutationOptions options = MutationOptions (
116119 document: gql (signOutMutation),
117120 );
118-
121+
119122 try {
120123 final QueryResult result = await client.mutate (options);
121124 if (result.hasException) {
@@ -130,28 +133,26 @@ class GraphQLService {
130133 }
131134
132135 /// Create a new shelf with given name
133- ///
136+ ///
134137 /// Returns success message
135138 static Future <Map <String , dynamic >?> createShelf (String shelfName) async {
136139 final GraphQLClient client = getClient ().value;
137-
140+
138141 const String createShelfMutation = '''
139142 mutation CreateShelf(\$ request: CreateShelfRequest!) {
140143 createShelf(request: \$ request) {
141144 msg
142145 }
143146 }
144147 ''' ;
145-
148+
146149 final MutationOptions options = MutationOptions (
147150 document: gql (createShelfMutation),
148151 variables: {
149- 'request' : {
150- "shelfName" : shelfName
151- }
152+ 'request' : {"shelfName" : shelfName}
152153 },
153154 );
154-
155+
155156 try {
156157 final QueryResult result = await client.mutate (options);
157158 if (result.hasException) {
@@ -166,11 +167,12 @@ class GraphQLService {
166167 }
167168
168169 /// Update shelf contents with contain and exclude lists
169- ///
170+ ///
170171 /// Returns updated shelf info
171- static Future <Map <String , dynamic >?> updateShelf (String shelfName, List <String > containList, List <String > excludeList) async {
172+ static Future <Map <String , dynamic >?> updateShelf (String shelfName,
173+ List <String > containList, List <String > excludeList) async {
172174 final GraphQLClient client = getClient ().value;
173-
175+
174176 const String updateShelfMutation = '''
175177 mutation UpdateShelf(\$ request: UpdateShelfRequest!) {
176178 updateShelf(request: \$ request) {
@@ -179,7 +181,7 @@ class GraphQLService {
179181 }
180182 }
181183 ''' ;
182-
184+
183185 final MutationOptions options = MutationOptions (
184186 document: gql (updateShelfMutation),
185187 variables: {
@@ -190,7 +192,7 @@ class GraphQLService {
190192 }
191193 },
192194 );
193-
195+
194196 try {
195197 final QueryResult result = await client.mutate (options);
196198 if (result.hasException) {
@@ -205,11 +207,11 @@ class GraphQLService {
205207 }
206208
207209 /// Get books in a shelf by shelf name
208- ///
210+ ///
209211 /// Returns list of books
210212 static Future <List <dynamic >> getBooksInShelf (String shelfName) async {
211213 final GraphQLClient client = getClient ().value;
212-
214+
213215 const String getBooksQuery = '''
214216 query GetBooksInShelf(\$ request: GetBooksInShelfRequest!) {
215217 getBooksInShelf(request: \$ request) {
@@ -234,31 +236,29 @@ class GraphQLService {
234236 }
235237 }
236238 ''' ;
237-
239+
238240 final QueryOptions options = QueryOptions (
239241 document: gql (getBooksQuery),
240242 variables: {
241- 'request' : {
242- "shelfName" : shelfName
243- }
243+ 'request' : {"shelfName" : shelfName}
244244 },
245245 );
246-
246+
247247 try {
248248 final QueryResult result = await client.query (options);
249249 if (result.hasException) {
250250 debugPrint ('GraphQL Error: ${result .exception .toString ()}' );
251251 return [];
252252 }
253-
253+
254254 if (result.data != null && result.data! ['getBooksInShelf' ] != null ) {
255255 return result.data! ['getBooksInShelf' ] as List <dynamic >;
256256 }
257-
257+
258258 return [];
259259 } catch (e) {
260260 debugPrint ('Error in getBooksInShelf: $e ' );
261261 return [];
262262 }
263263 }
264- }
264+ }
0 commit comments