diff --git a/lib/app/constant.dart b/lib/app/constant.dart index a5f128e..de62f16 100644 --- a/lib/app/constant.dart +++ b/lib/app/constant.dart @@ -4,6 +4,9 @@ class Constant { // static double designHeight = 1024; // static double designWidth = 1440; + // base url graphql + static String baseURLGraphQl = "http://devone.aplikasi.web.id:3300/query"; + static double designHeightPhone = 844; static double designWidthPhone = 390; diff --git a/lib/provider/dio_provider.dart b/lib/provider/dio_provider.dart new file mode 100644 index 0000000..f03191a --- /dev/null +++ b/lib/provider/dio_provider.dart @@ -0,0 +1,4 @@ +import 'package:dio/dio.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +final dioProvider = Provider((ref) => Dio()); diff --git a/lib/provider/graphql_provider.dart b/lib/provider/graphql_provider.dart new file mode 100644 index 0000000..351a245 --- /dev/null +++ b/lib/provider/graphql_provider.dart @@ -0,0 +1,22 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:graphql/client.dart'; + +import '../app/constant.dart'; + +final graphqlProvider = Provider.family( + (_, token) { + return GraphQLClient( + link: (token != "") + ? HttpLink( + Constant.baseURLGraphQl, + defaultHeaders: { + 'Authorization': 'Bearer $token', + }, + ) + : HttpLink( + Constant.baseURLGraphQl, + ), + cache: GraphQLCache(), + ); + }, +); diff --git a/lib/repository/base_repository.dart b/lib/repository/base_repository.dart index 5e6a5da..652893b 100644 --- a/lib/repository/base_repository.dart +++ b/lib/repository/base_repository.dart @@ -2,11 +2,49 @@ import 'dart:convert'; import 'dart:io'; import 'package:dio/dio.dart'; +import 'package:graphql/client.dart'; abstract class BaseRepository { final Dio dio; - BaseRepository({required this.dio}); + final GraphQLClient graphql; + BaseRepository({required this.dio, required this.graphql}); + // POST PAKE GRAPHQL + Future> postGraphQlMutation( + String query, Map inpVariables) async { + try { + final options = + MutationOptions(document: gql(query), variables: inpVariables); + final QueryResult result = await graphql.mutate(options); + + if (result.hasException) { + throw BaseRepositoryException(message: result.exception.toString()); + } + + return result.data!; + } catch (e) { + throw BaseRepositoryException(message: e.toString()); + } + } + + Future> postGraphQlQuery( + String query, Map inpVariables) async { + try { + final options = + QueryOptions(document: gql(query), variables: inpVariables); + final QueryResult result = await graphql.query(options); + + if (result.hasException) { + throw BaseRepositoryException(message: result.exception.toString()); + } + + return result.data!; + } catch (e) { + throw BaseRepositoryException(message: e.toString()); + } + } + + // NATIVE POST PAKE DIO Future> post({ required Map param, required String service, @@ -97,4 +135,4 @@ class BaseRepositoryException implements Exception { BaseRepositoryException({ required this.message, }); -} \ No newline at end of file +} diff --git a/pubspec.lock b/pubspec.lock index 33ae292..a97942e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -328,6 +328,78 @@ packages: url: "https://pub.dev" source: hosted version: "0.12.0+4" + gql: + dependency: transitive + description: + name: gql + sha256: "0bdd22c3a9464970ae590559e4f0568769b219dda9e94cb10c4cf999a3e263f7" + url: "https://pub.dev" + source: hosted + version: "1.0.1-alpha+1705114622973" + gql_dedupe_link: + dependency: transitive + description: + name: gql_dedupe_link + sha256: e5359dd0c7a38f95e2b12f6ab305989a4e30028e4032825c8e9f610150999c69 + url: "https://pub.dev" + source: hosted + version: "2.0.4-alpha+1705114623057" + gql_error_link: + dependency: transitive + description: + name: gql_error_link + sha256: "93901458f3c050e33386dedb0ca7173e08cebd7078e4e0deca4bf23ab7a71f63" + url: "https://pub.dev" + source: hosted + version: "1.0.0+1" + gql_exec: + dependency: transitive + description: + name: gql_exec + sha256: "394944626fae900f1d34343ecf2d62e44eb984826189c8979d305f0ae5846e38" + url: "https://pub.dev" + source: hosted + version: "1.1.1-alpha+1699813812660" + gql_http_link: + dependency: transitive + description: + name: gql_http_link + sha256: "1f922eed1b7078fdbfd602187663026f9f659fe9a9499e2207b5d5e01617f658" + url: "https://pub.dev" + source: hosted + version: "1.0.1+1" + gql_link: + dependency: transitive + description: + name: gql_link + sha256: "63941513a688d856546f0c3218e7ad94d47fc6e04662dcdb06de92a4cde2d7db" + url: "https://pub.dev" + source: hosted + version: "1.0.1-alpha+1705114622987" + gql_transform_link: + dependency: transitive + description: + name: gql_transform_link + sha256: "0645fdd874ca1be695fd327271fdfb24c0cd6fa40774a64b946062f321a59709" + url: "https://pub.dev" + source: hosted + version: "1.0.0" + graphql: + dependency: "direct main" + description: + name: graphql + sha256: d066e53446166c12537458386b507f7426f2b8801ebafc184576aab3cbc64d56 + url: "https://pub.dev" + source: hosted + version: "5.2.0-beta.7" + hive: + dependency: transitive + description: + name: hive + sha256: "8dcf6db979d7933da8217edcec84e9df1bdb4e4edc7fc77dbd5aa74356d6d941" + url: "https://pub.dev" + source: hosted + version: "2.2.3" hooks_riverpod: dependency: "direct main" description: @@ -432,6 +504,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.0" + normalize: + dependency: transitive + description: + name: normalize + sha256: "8a60e37de5b608eeaf9b839273370c71ebba445e9f73b08eee7725e0d92dbc43" + url: "https://pub.dev" + source: hosted + version: "0.8.2+1" package_config: dependency: transitive description: @@ -560,6 +640,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.3" + rxdart: + dependency: transitive + description: + name: rxdart + sha256: "0c7c0cedd93788d996e33041ffecda924cc54389199cde4e6a34b440f50044cb" + url: "https://pub.dev" + source: hosted + version: "0.27.7" shared_preferences: dependency: "direct main" description: @@ -725,6 +813,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.0" + web_socket_channel: + dependency: transitive + description: + name: web_socket_channel + sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b + url: "https://pub.dev" + source: hosted + version: "2.4.0" win32: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 094fd95..45c7f77 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -49,6 +49,7 @@ dependencies: crypton: ^2.2.1 path_provider: ^2.0.2 intl: ^0.17.0 + graphql: ^5.1.3 dev_dependencies: flutter_test: