Skip to content

Commit

Permalink
added querys and mutations from graphql to getConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
jonataslaw committed Dec 15, 2020
1 parent dafb027 commit b4b1c65
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [3.23.1]
- Fix allowSelfSigned on Flutter web

## [3.23.0]
- Add GetResponsive (@SchabanBo)
- Update tests, fix predicate for offNamedUntil (@vbuberen)
Expand Down
98 changes: 95 additions & 3 deletions lib/get_connect/connect.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '../get_instance/src/lifecycle.dart';
import 'http/src/certificates/certificates.dart';
import 'http/src/exceptions/exceptions.dart';
import 'http/src/http.dart';
import 'http/src/response/response.dart';
import 'sockets/sockets.dart';
Expand Down Expand Up @@ -206,14 +207,105 @@ class GetConnect extends GetConnectInterface {
}

@override
GetSocket socket(String url, {Duration ping = const Duration(seconds: 5)}) {
GetSocket socket(
String url, {
Duration ping = const Duration(seconds: 5),
}) {
_checkIfDisposed(isHttp: false);
final _url = baseUrl == null ? url : baseUrl + url;
final _socket = GetSocket(_url, ping: ping);

final _socket = GetSocket(_concatUrl(url), ping: ping);
sockets.add(_socket);
return _socket;
}

String _concatUrl(String url) {
if (url == null) return baseUrl;
return baseUrl == null ? url : baseUrl + url;
}

/// query allow made GraphQL raw querys
/// final connect = GetConnect();
/// connect.baseUrl = 'https://countries.trevorblades.com/';
/// final response = await connect.query(
/// r"""
/// {
/// country(code: "BR") {
/// name
/// native
/// currency
/// languages {
/// code
/// name
/// }
/// }
///}
///""",
///);
///print(response.body);
Future<GraphQLResponse<T>> query<T>(
String query, {
String url,
Map<String, dynamic> variables,
Map<String, String> headers,
}) async {
try {
final res =
await post(_concatUrl(url), {'query': query, 'variables': variables});

final listError = res.body['errors'];
if ((listError is List) && listError.isNotEmpty) {
// return GraphQLResponse<T>(body: res.body['data'] as T);
return GraphQLResponse<T>(
graphQLErrors: listError
.map((e) => GraphQLError(
code: e['extensions']['code']?.toString(),
message: e['message']?.toString(),
))
.toList());
}
return GraphQLResponse<T>(body: res.body['data'] as T);
} on Exception catch (_) {
return GraphQLResponse<T>(graphQLErrors: [
GraphQLError(
code: null,
message: _.toString(),
)
]);
}
}

Future<GraphQLResponse<T>> mutation<T>(
String mutation, {
String url,
Map<String, dynamic> variables,
Map<String, String> headers,
}) async {
try {
final res = await post(
_concatUrl(url), {'query': mutation, 'variables': variables});

final listError = res.body['errors'];
if ((listError is List) && listError.isNotEmpty) {
// return GraphQLResponse<T>(body: res.body['data'] as T);
return GraphQLResponse<T>(
graphQLErrors: listError
.map((e) => GraphQLError(
code: e['extensions']['code']?.toString(),
message: e['message']?.toString(),
))
.toList());
}
return GraphQLResponse<T>(body: res.body['data'] as T);
} on Exception catch (_) {
return GraphQLResponse<T>(graphQLErrors: [
GraphQLError(
code: null,
message: _.toString(),
)
]);
}
}

bool _isDisposed = false;

bool get isDisposed => _isDisposed;
Expand Down
9 changes: 9 additions & 0 deletions lib/get_connect/http/src/exceptions/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ class GetHttpException implements Exception {
String toString() => message;
}

class GraphQLError {
GraphQLError({this.code, this.message});
final String message;
final String code;

@override
String toString() => 'GETCONNECT ERROR:\n\tcode:$code\n\tmessage:$message';
}

class UnauthorizedException implements Exception {
@override
String toString() {
Expand Down
6 changes: 6 additions & 0 deletions lib/get_connect/http/src/response/response.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import 'dart:collection';
import 'dart:convert';
import '../exceptions/exceptions.dart';
import '../request/request.dart';
import '../status/http_status.dart';

class GraphQLResponse<T> extends Response<T> {
final List<GraphQLError> graphQLErrors;
GraphQLResponse({T body, this.graphQLErrors}) : super(body: body);
}

class Response<T> {
const Response({
this.request,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: get
description: Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with GetX.
version: 3.23.0
version: 3.23.1
homepage: https://github.com/jonataslaw/getx

environment:
Expand Down

0 comments on commit b4b1c65

Please sign in to comment.