Showing
5 changed files
with
114 additions
and
4 deletions
| 1 | import '../get_instance/src/lifecycle.dart'; | 1 | import '../get_instance/src/lifecycle.dart'; |
| 2 | import 'http/src/certificates/certificates.dart'; | 2 | import 'http/src/certificates/certificates.dart'; |
| 3 | +import 'http/src/exceptions/exceptions.dart'; | ||
| 3 | import 'http/src/http.dart'; | 4 | import 'http/src/http.dart'; |
| 4 | import 'http/src/response/response.dart'; | 5 | import 'http/src/response/response.dart'; |
| 5 | import 'sockets/sockets.dart'; | 6 | import 'sockets/sockets.dart'; |
| @@ -206,14 +207,105 @@ class GetConnect extends GetConnectInterface { | @@ -206,14 +207,105 @@ class GetConnect extends GetConnectInterface { | ||
| 206 | } | 207 | } |
| 207 | 208 | ||
| 208 | @override | 209 | @override |
| 209 | - GetSocket socket(String url, {Duration ping = const Duration(seconds: 5)}) { | 210 | + GetSocket socket( |
| 211 | + String url, { | ||
| 212 | + Duration ping = const Duration(seconds: 5), | ||
| 213 | + }) { | ||
| 210 | _checkIfDisposed(isHttp: false); | 214 | _checkIfDisposed(isHttp: false); |
| 211 | - final _url = baseUrl == null ? url : baseUrl + url; | ||
| 212 | - final _socket = GetSocket(_url, ping: ping); | 215 | + |
| 216 | + final _socket = GetSocket(_concatUrl(url), ping: ping); | ||
| 213 | sockets.add(_socket); | 217 | sockets.add(_socket); |
| 214 | return _socket; | 218 | return _socket; |
| 215 | } | 219 | } |
| 216 | 220 | ||
| 221 | + String _concatUrl(String url) { | ||
| 222 | + if (url == null) return baseUrl; | ||
| 223 | + return baseUrl == null ? url : baseUrl + url; | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + /// query allow made GraphQL raw querys | ||
| 227 | + /// final connect = GetConnect(); | ||
| 228 | + /// connect.baseUrl = 'https://countries.trevorblades.com/'; | ||
| 229 | + /// final response = await connect.query( | ||
| 230 | + /// r""" | ||
| 231 | + /// { | ||
| 232 | + /// country(code: "BR") { | ||
| 233 | + /// name | ||
| 234 | + /// native | ||
| 235 | + /// currency | ||
| 236 | + /// languages { | ||
| 237 | + /// code | ||
| 238 | + /// name | ||
| 239 | + /// } | ||
| 240 | + /// } | ||
| 241 | + ///} | ||
| 242 | + ///""", | ||
| 243 | + ///); | ||
| 244 | + ///print(response.body); | ||
| 245 | + Future<GraphQLResponse<T>> query<T>( | ||
| 246 | + String query, { | ||
| 247 | + String url, | ||
| 248 | + Map<String, dynamic> variables, | ||
| 249 | + Map<String, String> headers, | ||
| 250 | + }) async { | ||
| 251 | + try { | ||
| 252 | + final res = | ||
| 253 | + await post(_concatUrl(url), {'query': query, 'variables': variables}); | ||
| 254 | + | ||
| 255 | + final listError = res.body['errors']; | ||
| 256 | + if ((listError is List) && listError.isNotEmpty) { | ||
| 257 | + // return GraphQLResponse<T>(body: res.body['data'] as T); | ||
| 258 | + return GraphQLResponse<T>( | ||
| 259 | + graphQLErrors: listError | ||
| 260 | + .map((e) => GraphQLError( | ||
| 261 | + code: e['extensions']['code']?.toString(), | ||
| 262 | + message: e['message']?.toString(), | ||
| 263 | + )) | ||
| 264 | + .toList()); | ||
| 265 | + } | ||
| 266 | + return GraphQLResponse<T>(body: res.body['data'] as T); | ||
| 267 | + } on Exception catch (_) { | ||
| 268 | + return GraphQLResponse<T>(graphQLErrors: [ | ||
| 269 | + GraphQLError( | ||
| 270 | + code: null, | ||
| 271 | + message: _.toString(), | ||
| 272 | + ) | ||
| 273 | + ]); | ||
| 274 | + } | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + Future<GraphQLResponse<T>> mutation<T>( | ||
| 278 | + String mutation, { | ||
| 279 | + String url, | ||
| 280 | + Map<String, dynamic> variables, | ||
| 281 | + Map<String, String> headers, | ||
| 282 | + }) async { | ||
| 283 | + try { | ||
| 284 | + final res = await post( | ||
| 285 | + _concatUrl(url), {'query': mutation, 'variables': variables}); | ||
| 286 | + | ||
| 287 | + final listError = res.body['errors']; | ||
| 288 | + if ((listError is List) && listError.isNotEmpty) { | ||
| 289 | + // return GraphQLResponse<T>(body: res.body['data'] as T); | ||
| 290 | + return GraphQLResponse<T>( | ||
| 291 | + graphQLErrors: listError | ||
| 292 | + .map((e) => GraphQLError( | ||
| 293 | + code: e['extensions']['code']?.toString(), | ||
| 294 | + message: e['message']?.toString(), | ||
| 295 | + )) | ||
| 296 | + .toList()); | ||
| 297 | + } | ||
| 298 | + return GraphQLResponse<T>(body: res.body['data'] as T); | ||
| 299 | + } on Exception catch (_) { | ||
| 300 | + return GraphQLResponse<T>(graphQLErrors: [ | ||
| 301 | + GraphQLError( | ||
| 302 | + code: null, | ||
| 303 | + message: _.toString(), | ||
| 304 | + ) | ||
| 305 | + ]); | ||
| 306 | + } | ||
| 307 | + } | ||
| 308 | + | ||
| 217 | bool _isDisposed = false; | 309 | bool _isDisposed = false; |
| 218 | 310 | ||
| 219 | bool get isDisposed => _isDisposed; | 311 | bool get isDisposed => _isDisposed; |
| @@ -9,6 +9,15 @@ class GetHttpException implements Exception { | @@ -9,6 +9,15 @@ class GetHttpException implements Exception { | ||
| 9 | String toString() => message; | 9 | String toString() => message; |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | +class GraphQLError { | ||
| 13 | + GraphQLError({this.code, this.message}); | ||
| 14 | + final String message; | ||
| 15 | + final String code; | ||
| 16 | + | ||
| 17 | + @override | ||
| 18 | + String toString() => 'GETCONNECT ERROR:\n\tcode:$code\n\tmessage:$message'; | ||
| 19 | +} | ||
| 20 | + | ||
| 12 | class UnauthorizedException implements Exception { | 21 | class UnauthorizedException implements Exception { |
| 13 | @override | 22 | @override |
| 14 | String toString() { | 23 | String toString() { |
| 1 | import 'dart:collection'; | 1 | import 'dart:collection'; |
| 2 | import 'dart:convert'; | 2 | import 'dart:convert'; |
| 3 | +import '../exceptions/exceptions.dart'; | ||
| 3 | import '../request/request.dart'; | 4 | import '../request/request.dart'; |
| 4 | import '../status/http_status.dart'; | 5 | import '../status/http_status.dart'; |
| 5 | 6 | ||
| 7 | +class GraphQLResponse<T> extends Response<T> { | ||
| 8 | + final List<GraphQLError> graphQLErrors; | ||
| 9 | + GraphQLResponse({T body, this.graphQLErrors}) : super(body: body); | ||
| 10 | +} | ||
| 11 | + | ||
| 6 | class Response<T> { | 12 | class Response<T> { |
| 7 | const Response({ | 13 | const Response({ |
| 8 | this.request, | 14 | this.request, |
| 1 | name: get | 1 | name: get |
| 2 | description: Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with GetX. | 2 | description: Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with GetX. |
| 3 | -version: 3.23.0 | 3 | +version: 3.23.1 |
| 4 | homepage: https://github.com/jonataslaw/getx | 4 | homepage: https://github.com/jonataslaw/getx |
| 5 | 5 | ||
| 6 | environment: | 6 | environment: |
-
Please register or login to post a comment