Giter VIP home page Giter VIP logo

hasura_connect's Introduction

Hasura Package

This package is a fork of hasura_connect package

Connect your Flutter/Dart apps to Hasura simply.

What can he do

This package 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:

Usage

A simple usage example:

//import
import 'package:hasura/hasura.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.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.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(...);

Custom Database by Delegate

You can add others database to work with cache using localStorageDelegate param in HasuraConnect construct.

This library have two delegates:

  • LocalStorageSharedPreferences (default)
  • LocalStorageHive (Using Hive Database for Desktops)

Implements LocalStorage interface and use any Database:

HasuraConnect hasuraConnect = HasuraConnect(url,
            localStorageDelegate: () => LocalStorageHive(),);

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.

Feature Progress
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.

hasura_connect's People

Contributors

ben-xx avatar bwolfs2 avatar davidsdearaujo avatar jacobaraujo7 avatar jorgelrj avatar mchlalex avatar mohiuddinm avatar natarajnattu avatar rodolfosilva avatar rullyalves avatar wigny avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.