step 6 : add package graphql, configure base repo, constant, graph provider.dart

This commit is contained in:
sindhu
2024-01-24 08:19:12 +07:00
parent 3cfc545d89
commit aa22053180
6 changed files with 166 additions and 2 deletions

View File

@@ -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;

View File

@@ -0,0 +1,4 @@
import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final dioProvider = Provider<Dio>((ref) => Dio());

View File

@@ -0,0 +1,22 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:graphql/client.dart';
import '../app/constant.dart';
final graphqlProvider = Provider.family<GraphQLClient, String>(
(_, token) {
return GraphQLClient(
link: (token != "")
? HttpLink(
Constant.baseURLGraphQl,
defaultHeaders: {
'Authorization': 'Bearer $token',
},
)
: HttpLink(
Constant.baseURLGraphQl,
),
cache: GraphQLCache(),
);
},
);

View File

@@ -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<Map<String, dynamic>> postGraphQlMutation(
String query, Map<String, dynamic> 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<Map<String, dynamic>> postGraphQlQuery(
String query, Map<String, dynamic> 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<Map<String, dynamic>> post({
required Map<String, dynamic> param,
required String service,
@@ -97,4 +135,4 @@ class BaseRepositoryException implements Exception {
BaseRepositoryException({
required this.message,
});
}
}