fresh_graphql 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 81

fresh_graphql 🍋 #

Pub build coverage style: effective dart License: MIT


A graphql link for built-in token refresh. Built to be used with fresh.

Overview #

fresh_graphql is a graphql link which attempts to simplify custom API authentication by integrating token refresh and caching transparently. fresh_graphql is flexible and is intended to support custom token refresh mechanisms.

Usage #

final freshLink = FreshLink(
  tokenStorage: InMemoryTokenStorage(),
  refreshToken: (token, client) {
    // Perform refresh and return new token
  },
);
final graphQLClient = GraphQLClient(
  cache: InMemoryCache(),
  link: Link.from([freshLink, HttpLink(uri: 'https://my.graphql.api')]),
);

Example #

See the example for a complete sample application using fresh_graphql which integrates with api.graphql.jobs.

0.2.0 #

  • BREAKING remove onRefreshFailure from FreshLink
  • Add authenticationStatus Stream to FreshLink

0.1.0 #

Initial Release of the library

  • Includes FreshLink graphql link with built-in token refresh.

example/main.dart

import 'dart:io';
import 'dart:math';

import 'package:fresh_graphql/fresh_graphql.dart';
import 'package:graphql/client.dart';

const getJobsQuery = r'''
  query GetJobs() {
    jobs {
      title
    }
  }
''';

void main() async {
  final freshLink = FreshLink<OAuth2Token>(
    tokenStorage: InMemoryTokenStorage(),
    refreshToken: (token, client) async {
      // Perform refresh and return new token
      print('refreshing token!');
      await Future.delayed(const Duration(seconds: 1));
      if (Random().nextInt(3) == 0) {
        throw RevokeTokenException();
      }
      return OAuth2Token(accessToken: 't0ps3cret_r3fresh3d!');
    },
    shouldRefresh: (_) => Random().nextInt(1) == 0,
  )
    ..setToken(OAuth2Token(accessToken: 't0ps3cret!'))
    ..authenticationStatus.listen(print);
  final graphQLClient = GraphQLClient(
    cache: InMemoryCache(),
    link: Link.from([freshLink, HttpLink(uri: 'https://api.graphql.jobs')]),
  );
  final result = await graphQLClient.query(
    QueryOptions(documentNode: gql(getJobsQuery)),
  );
  print(result.data);
  exit(0);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  fresh_graphql: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:fresh_graphql/fresh_graphql.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
81
Learn more about scoring.

We analyzed this package on Jul 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Analysis suggestions

Package not compatible with runtime flutter-web on Web

Because:

  • package:fresh_graphql/fresh_graphql.dart that imports:
  • package:fresh_graphql/src/fresh_link.dart that imports:
  • package:graphql/client.dart that imports:
  • package:graphql/src/link/http/link_http.dart that imports:
  • package:graphql/src/exceptions/exceptions.dart that imports:
  • package:graphql/src/exceptions/io_network_exception.dart that imports:
  • dart:io

Package not compatible with runtime js

Because:

  • package:fresh_graphql/fresh_graphql.dart that imports:
  • package:fresh_graphql/src/fresh_link.dart that imports:
  • package:graphql/client.dart that imports:
  • package:graphql/src/link/http/link_http.dart that imports:
  • package:graphql/src/exceptions/exceptions.dart that imports:
  • package:graphql/src/exceptions/io_network_exception.dart that imports:
  • dart:io

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
fresh ^0.2.1 0.2.1
graphql ^3.0.2 3.0.2 4.0.0-alpha.1
http ^0.12.1 0.12.1
meta ^1.1.8 1.2.2 1.3.0-nullsafety
pedantic ^1.9.0 1.9.2
Transitive dependencies
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
crypto 2.1.5
gql 0.12.3 0.12.4-alpha+1594749296675
http_parser 3.1.4
matcher 0.12.9
mime 0.9.6+3
path 1.7.0
quiver 2.1.3
rxdart 0.23.1 0.24.1
source_span 1.7.0
stack_trace 1.9.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
uuid_enhanced 3.0.2
websocket 0.0.5
Dev dependencies
effective_dart ^1.2.1
mockito ^4.1.1
test ^1.14.3
test_coverage ^0.4.1