hasura_connect 0.1.2 hasura_connect: ^0.1.2 copied to clipboard
Connect your Flutter/Dart apps to Hasura simply. All the power of GraphQL and Postgres in Flutter
Hasura Connect Package #
Connect your Flutter/Dart apps to Hasura simply.
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.
var r = await hasuraConnect.query(docQuery);
print(r);
//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);
});
Mutation + Subscriptions #
You can to use mutation directly from the subscription snapshot. This will allow you to update your local list even before it has been notified by Hasura.
Snapshot snapshot = hasuraConnect.subscription(docSubscription);
...
snapshot.mutation(docMutation, onNotify: (data) {
return data..insert(a, {"name": "next offline item" });
}
Mapped Subscription #
Use the Map operator to convert json data to a Dart object;
Snapshot<PostModel> snapshot = hasuraConnect.subscription(docSubscription)
.map((data) => PostModel.fromJson(data) );
snapshot.stream.listen((PostModel data) {
print(data);
}).onError((err) {
print(err);
});
Mapped Subscription with List of results #
Use the Map operator to convert list of results in json data to a List of Dart object;
Snapshot<List<PostModel>> snapshot = hasuraConnect.subscription(docSubscription)
.map((data) =>
data.map((post) => PostModel.fromJson(post)).toList());
snapshot.stream.listen(List<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: () async {
//sharedPreferences or other storage logic
return "Bearer YOUR-JWT-TOKEN";
});
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 Intercept | 🔜 |
Features and bugs #
Please send feature requests and bugs at the [issue tracker][tracker]. [tracker]: http://example.com/issues/replaceme
Created from templates made available by Stagehand under a BSD-style license.