hasura_connect 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 92

Hasura Connect Package #

Connect your Flutter/Dart apps to Hasura simply.

What can he do #

The hasura_connect is designed to facilitate Hasura's integration with Flutter applications, leveraging the full power of Graphql.

  • Use Query, Mutation and Subscriptions the easy way.
  • Offline cache for Subscription and Mutation made from a Snapshot.
  • Easy integration with leading dart providers (Provider, bloc_pattern).
  • Pass your JWT Token easily if you are informed when it is invalid.

Install #

Add dependency in your pubspec.yaml file:

dependencies:
  hasura_connect:

or use Slidy:

slidy install hasura_connect

Usage #

A simple usage example:


//import
import 'package:hasura_connect/hasura_connect.dart';

String url = 'http://localhost:8080/v1/graphql';
HasuraConnect hasuraConnect = HasuraConnect(url);

You can encapsulate this instance into a BLoC class or directly into a Provider.

Create a document with Query:

//document
String docQuery = """
  query {
    authors {
        id
        email
        name
      }
  }
""";

Now just add the document to the "query" method of the HasuraConnect instance.

//get query
var r = await hasuraConnect.query(docQuery);

//get query with cache
var r = await hasuraConnect.cachedQuery(docQuery);

//OR USE MUTATION
var r = await hasuraConnect.mutation(docQuery);

Subscriptions #

Subscriptions will notify you each time you have a change to the searched items. Use the "hasuraConnect.subscription" method to receive a stream.

Snapshot snapshot = hasuraConnect.subscription(docSubscription);
  snapshot.stream.listen((data) {
    print(data);
  }).onError((err) {
    print(err);
  });

Subscription Converter #

Use the Map operator to convert json data to a Dart object;

Snapshot<PostModel> snapshot = hasuraConnect
                                  .subscription(docSubscription)
                                  .convert((data) => PostModel.fromJson(data),
                                        cachePersist: (PostModel post) => post.toJson(),
                                      );

snapshot.stream.listen((PostModel data) {
   print(data);
 }).onError((err) {
   print(err);
 });

Using variables #

Variables maintain the integrity of Querys, see an example:


String docSubscription = """
  subscription algumaCoisa($limit:Int!){
    users(limit: $limit, order_by: {user_id: desc}) {
      id
      email
      name
    }
  }
""";

Snapshot snapshot = hasuraConnect.subscription(docSubscription, variables: {"limit": 10});

//change values of variables for PAGINATIONS
snapshot.changeVariable({"limit": 20});

Authorization (JWT Token) #

View Hasura's official Authorization documentation.


String url = 'http://localhost:8080/v1/graphql';
HasuraConnect hasuraConnect = HasuraConnect(url, token: (isError) async {
  //sharedPreferences or other storage logic
  return "Bearer YOUR-JWT-TOKEN";
});

CACHE OFFLINE #

  • Offline caching works with subscriptions automatically.
  • A good strategy for mutation caching is to add the offline object to the snapshot with the add parameter with what will be the change, then perform the mutation.
  • When a mutation internet error occurs, HasuraConnect will attempt to mutate again when the device reconnects to the internet.
  • Use this information to promote your offline persistence rules.
Snapshot snapshot = connect.subscription(...);

//Add object to cache of snapshot
var list = snapshot.value;
list.add(newItem);
snapshot.add(newItem);

//exec asinc mutation
conn.mutation(...);

Dispose #

HasuraConnect provides a dispose() method for use in Provider or BlocProvider. Subscription will start only when someone is listening, and when all listeners are closed HasuraConnect automatically disconnects.

Therefore, we only connect to Hasura when we are actually using it;

Roadmap #

This is currently our roadmap, please feel free to request additions/changes.

FeatureProgress
Queries
Mutations
Subscriptions
Change Variable in Subscriptions
Auto-Reconnect
Dynamic JWT Token
bloc_pattern Integration
Provider Integration
Variables
Cache Subscription
Cache Mutation
Cache Query

Features and bugs #

Please send feature requests and bugs at the issue tracker.

Created from templates made available by Stagehand under a BSD-style license.

1.0.3 #

  • Fix not close snapshot error
  • improve cache.

1.0.2 #

  • Added Flutter Web Cache offline for PWA.
  • added request with variable cache.

1.0.0+2 #

  • Cache offline for Subscription and Query (CachedQuery).
  • When mutation fails due to no connection, HasuraConnect will retry when you have internet.
  • Refactored Snapshot.
  • Error Handling.

0.2.0 #

  • Added Subscription Cache.
  • fix error #9
  • fix Duplicate error
  • close streams subscriptions
  • Added isError in token Function

0.1.2 #

  • fix start stream with value

0.1.0 #

  • Mapped Subscriptions
  • Mutation in Snapshot

0.0.8 #

  • Flutter web Support.

0.0.7+1 #

  • Apply Health suggestions.

0.0.7 #

  • Add and Remove Headers

0.0.6 #

  • Added variables.
  • Change Variables in subscriptions (for Reactive Pagination)
  • Mutations links

0.0.3 #

  • Add variable;
  • Add mutation;

0.0.2 #

  • Query return param data;

0.0.1 #

  • Initial version, created by Stagehand

example/README.md

example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  hasura_connect: ^1.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:hasura_connect/hasura_connect.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
84
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]
92
Learn more about scoring.

We analyzed this package on Dec 6, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/src/services/local_storage_hasura.dart. (-0.50 points)

Analysis of lib/src/services/local_storage_hasura.dart reported 1 hint:

line 40 col 17: Await only futures.

Format lib/src/utils/hydrated.dart.

Run flutter format to format lib/src/utils/hydrated.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
hive ^1.1.1 1.1.1
http ^0.12.0+2 0.12.0+2
path_provider ^1.4.4 1.4.5
rxdart >=0.22.3 <1.0.0 0.22.6 0.23.0-dev.2
websocket ^0.0.5 0.0.5
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
http_parser 3.1.3
meta 1.1.7 1.1.8
path 1.6.4
pedantic 1.9.0
platform 2.2.1
pointycastle 1.0.2
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8