Created
September 9, 2021 15:10
-
-
Save benhaxe/a25daa5a99a6bf2beefbf1118d09b4a2 to your computer and use it in GitHub Desktop.
GraphQL tips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:developer'; | |
import 'dart:io'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:gp_flutter_core/config/service/gql_config.dart'; | |
import 'package:graphql_flutter/graphql_flutter.dart'; | |
/// The service responsible for networking requests | |
class GpGQLClient { | |
GpGQLClient(this._gqlConfig); | |
final GQLConfig _gqlConfig; | |
///[getGraphQLClient] | |
ValueNotifier<GraphQLClient> defaultGraphQLClient() { | |
final HttpLink httpLink = HttpLink(_gqlConfig.baseUrl); | |
return ValueNotifier( | |
GraphQLClient( | |
link: httpLink, | |
cache: GraphQLCache(), | |
), | |
); | |
} | |
Future<ValueNotifier<GraphQLClient>> _getGraphQLClient() async { | |
return ValueNotifier( | |
GraphQLClient( | |
link: await _getAuthLink().then((value) { | |
return value.concat( | |
HttpLink( | |
_gqlConfig.baseUrl, | |
), | |
); | |
}), | |
cache: GraphQLCache(), | |
), | |
); | |
} | |
Future<AuthLink> _getAuthLink() async { | |
String? _getAuthToken; | |
if (_gqlConfig.bearerToken != null) { | |
final token = await _gqlConfig.bearerToken!(); | |
if (token.isNotEmpty) { | |
_getAuthToken = 'Bearer $token'; | |
} else { | |
_getAuthToken = ''; | |
} | |
} | |
return AuthLink( | |
headerKey: 'Authorization', | |
getToken: () async => _getAuthToken!, | |
); | |
} | |
/// Expose the [graphlQlClient] | |
Future<ValueNotifier<GraphQLClient>> get getGraphQlClient => | |
_getGraphQLClient(); | |
/// | |
Future<QueryResult> gpMutate({ | |
required String mutationDocument, | |
Map<String, dynamic>? data, | |
}) async { | |
QueryResult queryResult; | |
try { | |
queryResult = await _getGraphQLClient().then((value) { | |
return value.value.mutate( | |
MutationOptions( | |
document: gql(mutationDocument), | |
variables: data!, | |
), | |
); | |
}); | |
// ignore: unused_catch_stack | |
} on OperationException catch (error, stacktrace) { | |
log('Operation Error is: $error'); | |
rethrow; | |
// ignore: unused_catch_stack | |
} on SocketException catch (error, stacktrace) { | |
throw SocketException(error.toString()); | |
} catch (e) { | |
log('Error is: $e'); | |
rethrow; | |
} | |
if (queryResult.hasException) { | |
throw OperationException( | |
linkException: queryResult.exception!.linkException, | |
graphqlErrors: queryResult.exception!.graphqlErrors, | |
); | |
} else { | |
return queryResult; | |
} | |
} | |
///. | |
Future<QueryResult> gpQuery({ | |
required String queryDocument, | |
Map<String, dynamic>? data, | |
}) async { | |
QueryResult queryResult; | |
try { | |
queryResult = await _getGraphQLClient().then((value) { | |
return value.value.query( | |
QueryOptions( | |
document: gql(queryDocument), | |
variables: data!, | |
), | |
); | |
}); | |
// ignore: unused_catch_stack | |
} on OperationException catch (error, stacktrace) { | |
rethrow; | |
// ignore: unused_catch_stack | |
} on SocketException catch (error, stacktrace) { | |
throw SocketException(error.toString()); | |
} catch (e) { | |
rethrow; | |
} | |
if (queryResult.hasException) { | |
throw OperationException( | |
linkException: queryResult.exception!.linkException, | |
graphqlErrors: queryResult.exception!.graphqlErrors, | |
); | |
} else { | |
return queryResult; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment