fresh_graphql 0.6.1 fresh_graphql: ^0.6.1 copied to clipboard
An graphql link for token refresh. Built on top of package:graphql and manages authentication tokens transparently.
// ignore_for_file: avoid_print
import 'dart:io';
import 'dart:math';
import 'package:fresh_graphql/fresh_graphql.dart';
import 'package:graphql/client.dart';
const getJobsQuery = '''
query GetJobs() {
jobs {
title
}
}
''';
void main() async {
final freshLink = FreshLink.oAuth2(
tokenStorage: InMemoryTokenStorage(),
refreshToken: (token, client) async {
// Perform refresh and return new token
print('refreshing token!');
await Future<void>.delayed(const Duration(seconds: 1));
if (Random().nextInt(1) == 0) {
throw RevokeTokenException();
}
return const OAuth2Token(accessToken: 't0ps3cret_r3fresh3d!');
},
shouldRefresh: (_) => Random().nextInt(2) == 0,
)..authenticationStatus.listen(print);
await freshLink.setToken(const OAuth2Token(accessToken: 't0ps3cret!'));
final graphQLClient = GraphQLClient(
cache: GraphQLCache(),
link: Link.from([freshLink, HttpLink('https://api.graphql.jobs')]),
);
final result = await graphQLClient.query<dynamic>(
QueryOptions<dynamic>(document: gql(getJobsQuery)),
);
print(result.data);
exit(0);
}