zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

cache.readQuery returns null #1092

Closed aquadesk closed 2 years ago

aquadesk commented 2 years ago

Hello guys,

I am using graphql_flutter: ^5.0.2-beta.6 this version.

I tried beta.7 but I get build errors so I could not try beta.7.

  1. I have 2 mutations. delete and create for my product review model.
  2. When I run delete, it read from cache and update after execution of the mutation. It runs well and cache being read and rewrites successfully.
  3. when I run create, it tries to get cache but it gets null.
  4. They use exact same code so I think this might be a bug.

Expected behavior

The last line is to return correct cache

  Future<QueryResult<MutationCreateProductReview>> createProductReview({
    required String productName,
    String? desc,
    int? rating,
    List<String?>? images,
  }) async {
    var result = await client.mutateCreateProductReview(
      OptionsMutationCreateProductReview(
        variables: VariablesMutationCreateProductReview(
          productName: productName,
          desc: desc,
          rating: rating,
          images: images,
        ),
        update: (cache, result) {
          final queryResult = cache.readQuery(currentQueryOptions.asRequest); // << this line to return correct cache

I run this in iOS simulator

Other useful/optional fields

This fails. meaning queryResult becomes null

  Future<QueryResult<MutationCreateProductReview>> createProductReview({
    required String productName,
    String? desc,
    int? rating,
    List<String?>? images,
  }) async {
    var result = await client.mutateCreateProductReview(
      OptionsMutationCreateProductReview(
        variables: VariablesMutationCreateProductReview(
          productName: productName,
          desc: desc,
          rating: rating,
          images: images,
        ),
        update: (cache, result) {
          final queryResult = cache.readQuery(currentQueryOptions.asRequest);

While, this works well. queryResult this has cache.

  Future<QueryResult<MutationDeleteProductReview>> deleteProductReview({
    required String id,
  }) async {
    var result = await client.mutateDeleteProductReview(
      OptionsMutationDeleteProductReview(
        variables: VariablesMutationDeleteProductReview(
          id: id,
        ),
        update: (cache, result) {
          final queryResult = cache.readQuery(currentQueryOptions.asRequest);
aquadesk commented 2 years ago

Sorry I digged in and found that my code has a bug. Sorry!