bloc icon indicating copy to clipboard operation
bloc copied to clipboard

Extend Jobs example with Graphql Subscriptions

Open RobertoOrtis opened this issue 4 years ago • 2 comments

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 avatar Sep 01 '20 01:09 RobertoOrtis

@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?

JigneshWorld avatar Feb 18 '21 16:02 JigneshWorld

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

kateile avatar Mar 21 '21 04:03 kateile

Closing this since the jobs example was removed due to the discontinued API.

felangel avatar Mar 10 '24 21:03 felangel