Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Flutterando/hasura_connect
Connect your Flutter/Dart apps to Hasura simply.
https://github.com/Flutterando/hasura_connect
Last synced: 27 days ago
JSON representation
Connect your Flutter/Dart apps to Hasura simply.
- Host: GitHub
- URL: https://github.com/Flutterando/hasura_connect
- Owner: Flutterando
- License: mit
- Created: 2019-08-07T20:34:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T17:19:44.000Z (6 months ago)
- Last Synced: 2024-06-25T15:44:07.080Z (6 months ago)
- Language: Dart
- Homepage: https://pub.dev/packages/hasura_connect
- Size: 1.94 MB
- Stars: 204
- Watchers: 17
- Forks: 62
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-hasura - Hasura Connect - A client library to talk to Hasura from Flutter/Dart apps (Tools and Extensions)
README
Hasura Connect - Connect your Flutter/Dart apps to Hasura simply.
The hasura_connect is designed to facilitate Hasura's integration with Flutter applications.
Report Bug
ยท
Request Feature
[![License](https://img.shields.io/github/license/flutterando/hasura_connect?style=plastic)](https://github.com/Flutterando/hasura_connect/blob/master/LICENSE)
[![Pub Points](https://img.shields.io/pub/points/hasura_connect?label=pub%20points&style=plastic)](https://pub.dev/packages/hasura_connect/score)
[![Contributors](https://img.shields.io/github/contributors/flutterando/hasura_connect?style=plastic)](https://github.com/Flutterando/hasura_connect/graphs/contributors)
[![Forks](https://img.shields.io/github/forks/flutterando/hasura_connect?color=yellowgreen&logo=github&style=plastic)](https://github.com/Flutterando/hasura_connect/graphs/contributors)[![Pub Publisher](https://img.shields.io/pub/publisher/hasura_connect?style=plastic)](https://pub.dev/publishers/flutterando.com.br/packages)
[![Flutterando Youtube](https://img.shields.io/youtube/channel/subscribers/UCplT2lzN6MHlVHHLt6so39A?color=blue&label=Flutterando&logo=YouTube&logoColor=red&style=plastic)](https://www.youtube.com/flutterando)
Table of Contents
## About The Project
The hasura_connect is designed to facilitate Hasura's integration with Flutter applications, leveraging the full power of Graphql.
## Sponsors
## Getting Started
To install Hasura Connect in your project you can follow the instructions below:
a) Add in your pubspec.yaml:
```sh
dependencies:
hasura_connect:
```
b) or use slidy:
```sh
slidy install hasura_connect
```## How To Use
A simple usage example:
```dart
//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:
```dart
//document
String docQuery = """
query {
authors {
id
name
}
}
""";
```Now just add the document to the "query" method of the HasuraConnect instance.
```dart
//get query
var r = await hasuraConnect.query(docQuery);//OR USE MUTATION
var r = await hasuraConnect.mutation(docQuery);
```
## SubscriptionsSubscriptions will notify you each time you have a change to the searched items. Use the "hasuraConnect.subscription" method to receive a stream.
```dart
Snapshot snapshot = await hasuraConnect.subscription(docSubscription);
snapshot.listen((data) {
print(data);
}).onError((err) {
print(err);
});
```## Using variables
Variables maintain the integrity of Querys, see an example:
```dart
String docSubscription = """
subscription algumaCoisa($limit:Int!){
users(limit: $limit, order_by: {user_id: desc}) {
id
name
}
}
""";Snapshot snapshot = await hasuraConnect.subscription(docSubscription, variables: {"limit": 10});
//change values of variables for PAGINATIONS
snapshot.changeVariable({"limit": 20});
```## INTERCEPTORS
This is a good strategy to control the flow of requests. With that we can create interceptors for logs or cache for example. The community has already provided some interceptors for caching. Interceptors are highly customizable.
* hive_cache_interceptor
* shared_preferences_cache_interceptor
* hasura_cache_interceptorView Hasura's official Authorization documentation.
```dart
class TokenInterceptor extends Interceptor {
final FirebaseAuth auth;
TokenInterceptor(this.auth);@override
Future onConnected(HasuraConnect connect) {}@override
Future onDisconnected() {}@override
Future onError(HasuraError request) async {
return request;
}@override
Future onRequest(Request request) async {
var user = await auth.currentUser();
var token = await user.getIdToken(refresh: true);
if (token != null) {
try {
request.headers["Authorization"] = "Bearer ${token.token}";
return request;
} catch (e) {
return null;
}
} else {
Modular.to.pushReplacementNamed("/login");
}
}@override
Future onResponse(Response data) async {
return data;
}@override
Future onSubscription(Request request, Snapshot snapshot) {}@override
Future onTryAgain(HasuraConnect connect) {}
}
```
Or:```dart
class TokenInterceptor extends InterceptorBase {
final FirebaseAuth auth;
TokenInterceptor(this.auth);@override
Future onRequest(Request request) async {
var user = await auth.currentUser();
var token = await user.getIdToken(refresh: true);
if (token != null) {
try {
request.headers["Authorization"] = "Bearer ${token.token}";
return request;
} catch (e) {
return null;
}
} else {
Modular.to.pushReplacementNamed("/login");
}
}
}
```
## INTERCEPTORNow you can intercept all requests, erros, subscritions.... all states of your hasura_connect connection.
* onConnected
* onDisconnected
* onError
* onRequest
* onResponse
* onSubscription
* onTryAgain## CACHE OFFLINE
Now you will need to create a Interceptor or use a Cache Interceptor Package made to help you like: InMemory, Hive or SharedPreference
```dart
//In Memory
import 'package:hasura_cache_interceptor/hasura_hive_cache_interceptor.dart';final storage = MemoryStorageService();
final cacheInterceptor = CacheInterceptor(storage);
final hasura = HasuraConnect(
"",
interceptors: [cacheInterceptor],
httpClient: httpClient,
)
``````dart
//Hive
import 'package:hasura_connect/hasura_connect.dart';
import 'package:hasura_hive_cache_interceptor/hasura_hive_cache_interceptor.dart';final cacheInterceptor = HiveCacheInterceptor(" (optional)");
final hasura = HasuraConnect(
"",
interceptors: [cacheInterceptor],
httpClient: httpClient,
)
``````dart
//Shared Preference
import 'package:hasura_connect/hasura_connect.dart';
import 'package:shared_preferences_cache_interceptor/shared_preferences_cache_interceptor.dart';final cacheInterceptor = SharedPreferencesCacheInterceptor();
final hasura = HasuraConnect(
"",
interceptors: [cacheInterceptor],
httpClient: httpClient,
)
```## 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;
_For more examples, please refer to the ๐ง [Documentation](https://example.com) - Currently being updated ๐ง .
## Common Errors
- Query data returned not decoded to utf-8.
Fix:
```dart
import 'dart:convert';extension Utf8convert on String {
String _utf8convert() {
List bytes = this.toString().codeUnits;
return utf8.decode(bytes);
}
String get utf8convert => _utf8convert();
}
```## Features
- โ Queries
- โ Mutations
- โ Subscriptions
- โ Change Variable in Subscriptions
- โ Auto-Reconnect
- โ Dynamic JWT Token
- โ bloc_pattern Integration
- โ Provider Integration
- โ Variables
- โ Cache Subscription
- โ Cache Mutation
- โ Cache QueryRight now this package has concluded all his intended features. If you have any suggestions or find something to report, see below how to contribute to it.
## Contributing
๐ง [Contributing Guidelines](https://github.com/angular/angular/blob/main/CONTRIBUTING.md) - Currently being updated ๐ง
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the appropriate tag.
Don't forget to give the project a star! Thanks again!1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull RequestRemember to include a tag, and to follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) and [Semantic Versioning](https://semver.org/) when uploading your commit and/or creating the issue.
## License
Distributed under the MIT License. See `LICENSE.txt` for more information.
## Contact
Flutterando Community
- [Discord](https://discord.gg/qNBDHNARja)
- [Telegram](https://t.me/flutterando)
- [Website](https://www.flutterando.com.br/)
- [Youtube Channel](https://www.youtube.com.br/flutterando)
- [Other useful links](https://linktr.ee/flutterando)## Contributors
## Maintaned by
---
Built and maintained by Flutterando.