bloc
bloc copied to clipboard
Extend Jobs example with Graphql Subscriptions
Hello,
I managed to implement bloc together with graphql inside my app by following the Jobs example. However, I have no idea how I could have graphql subscriptions and BLoC working together. It would be great if you could extend your jobs example to include subscriptions.
I am using:
- graphql_flutter: ^4.0.0-alpha.3
- bloc: ^6.0.2
- flutter_bloc: ^6.0.2
@RobertoOrtis I have just checked into Jobs example with GraphQL and their API Can't able to find any subscription there in document/playground..
Can you please help if you know if there is any subscription on same API we can use to extend demo?
I am using subscription with artemis and graphql-flutter this way
Repo example
class JobsRepository {
final GraphQLClient client;
JobsRepository({@required this.client}) : assert(client != null);
Stream<QueryResult> subscribeToJobs() {
final _options = SubscriptionOptions(
document: NewJobsSubscription().document,
);
return client.subscribe(_options);
}
}
Cubit Example
class JobsSubscriptionCubit extends Cubit<String> {
JobsSubscriptionCubit(this._repository) : super(null);
final JobsRepository _repository;
subscribe() {
Stream<QueryResult> _subscription = _repository.subscribeToJobs();
_subscription.listen((result) {
if (result.hasException) {
print(result.exception.toString());
/// You can emit error here
return;
}
if (result.isLoading) {
print('awaiting results');
return;
}
print('New Review: ${result.data}');
/// I am using artemis here to parse data. Highly recommended tool
final data = NewJobs$Subscription.fromJson(result.data).newJobs;
emit(data);
});
}
}
And you can use BlocBuilder or BlocListener to get data. I will later try to add this functionality to jobs example
Closing this since the jobs example was removed due to the discontinued API.