Flutter graphql cache update works only once

1.3k views Asked by At
static Future<QueryResult> updateOrderStatus({required dynamic input, required String status, required String orderId, required String buyerId}) {
    final client = GraphqlClient.getGraphQlClient();
    print(input);

    final MutationOptions options = MutationOptions(
      document: gql(OrderMutaions.updateOrderStatus),
      variables: {'input': input},
      update: (cache, result) {
        print(orderId);
        print(buyerId);
        final req = QueryOptions(
          document: gql(OrderQueries.getOrderByOrderId),
          variables: {
            'input': {"orderId": orderId, "buyerUid": buyerId},
          },
        ).asRequest;

        final response = cache.readQuery(req);

        print('cache response');
        print(response);

        response?["getOrderByOrderId"]["status"] = status;

        print('cache response new');
        print(response);

        if (response != null) {
          cache.writeQuery(
            req,
            broadcast: true,
            data: response,
          );
        }

        return cache;
      },
    );

    return client.mutate(options);
  }

I am using this package to use graphql in flutter as I don't want to use query/mutation widgets. I am using watchQuery for getOrderByOrderId and listening to changes. When updateOrderStatus query is called I update cache so watchQuery gets the changes. However when the first time status changes it works fine. cache is updated and watchquery gets the changes. But when next time status is again changed cache.readQuery(req) returns null value for given query. Hence this works only once. I don't understad why second time is gets null. Where is the mistake?

0

There are 0 answers