fresh_graphql 0.6.0 copy "fresh_graphql: ^0.6.0" to clipboard
fresh_graphql: ^0.6.0 copied to clipboard

An graphql link for token refresh. Built on top of package:graphql and manages authentication tokens transparently.

example/main.dart

// 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);
}
32
likes
140
pub points
88%
popularity

Publisher

verified publisherfelangel.dev

An graphql link for token refresh. Built on top of package:graphql and manages authentication tokens transparently.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

fresh, gql_exec, gql_link, http

More

Packages that depend on fresh_graphql