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 622 forks source link

GraphQLDataProxy usage example fails with PartialDataException(path: __typename) #1411

Open Dimous opened 9 months ago

Dimous commented 9 months ago

Hello! So I grabbed an example from -- https://pub.dev/documentation/graphql/latest/graphql/GraphQLDataProxy-class.html

import 'package:flutter_test/flutter_test.dart';
import 'package:graphql_flutter/graphql_flutter.dart';

void main() {
  final client = GraphQLClient(
    link: AuthLink(
      getToken: () => "Bearer ***",
    ).concat(
      HttpLink(
        "https://rickandmortyapi.com/graphql",
      ),
    ),
    cache: GraphQLCache(),
  );

  /// entity identifiers for normalization
  final idFields = {'__typename': 'MyType', 'id': 1};

  /// The direct cache API uses `gql_link` Requests directly
  /// These can also be obtained via `options.asRequest` from any `Options` object,
  /// or via `Operation(document: gql(...)).asRequest()`
  final queryRequest = Request(
    operation: Operation(
      document: gql(
        r'''{
        someField {
          __typename,
          id,
          myField
        }
      }''',
      ),
    ),
  );

  final queryData = {
    'someField': {
      ...idFields,
      'myField': 'originalValue',
    },
  };

  /// `broadcast: true` (the default) would rebroadcast cache updates to all safe instances of `ObservableQuery`
  /// **NOTE**: only `GraphQLClient` can immediately call for a query rebroadcast. if you request a rebroadcast directly
  /// from the cache, it still has to wait for the client to check in on it
  client.writeQuery(queryRequest, data: queryData, broadcast: false);

  /// `optimistic: true` (the default) integrates optimistic data
  /// written to the cache into your read.
  expect(client.readQuery(queryRequest, optimistic: false), equals(queryData));

  /// While fragments are never executed themselves, we provide a `gql_link`-like API for consistency.
  /// These can also be obtained via `Fragment(document: gql(...)).asRequest()`.
  final fragmentRequest = FragmentRequest(
      fragment: Fragment(
        document: gql(
          r'''
          fragment mySmallSubset on MyType {
            myField,
            someNewField
          }
        ''',
        ),
      ),
      idFields: idFields);

  /// We've specified `idFields` and are only editing a subset of the data
  final fragmentData = {
    'myField': 'updatedValue',
    'someNewField': [
      {'newData': false}
    ],
  };

  /// We didn't disable `broadcast`, so all instances of `ObservableQuery` will be notified of any changes
  client.writeFragment(fragmentRequest, data: fragmentData);

  /// __typename is automatically included in all reads
  expect(
    client.readFragment(fragmentRequest),
    equals({
      '__typename': 'MyType',
      ...fragmentData,
    }),
  );

  final updatedQueryData = {
    'someField': {
      ...idFields,
      'myField': 'updatedValue',
    },
  };

  /// `myField` is updated, but we don't have `someNewField`, as expected.
  expect(client.readQuery(queryRequest), equals(updatedQueryData));
}

Ran it:

C:\Users\***\Documents\flutter\bin\flutter.bat --no-color test --machine --start-paused test\test.dart
Testing started at 22:45 ...

package:normalize/src/normalize_node.dart 86:13               normalizeNode.<fn>
dart:collection                                               ListBase.fold
package:normalize/src/normalize_node.dart 61:19               normalizeNode
package:normalize/src/normalize_operation.dart 75:5           normalizeOperation
package:graphql/src/cache/_normalizing_data_proxy.dart 128:7  NormalizingDataProxy.writeQuery
package:graphql/src/graphql_client.dart 257:11                GraphQLClient.writeQuery
test\test.dart 57:10                                          main

Failed to load "C:/Users/***/Desktop/flutter/graphql/test/test.dart": PartialDataException(path: __typename)

I use git dependencies:

dependencies:
  flutter:
    sdk: flutter

  graphql_flutter:
    git:
      url: https://github.com/zino-hofmann/graphql-flutter.git
      path: packages/graphql_flutter

dependency_overrides:
  #normalize: ^0.9.1
  graphql:
    git:
      url: https://github.com/zino-hofmann/graphql-flutter.git
      path: packages/graphql

dev_dependencies:
  flutter_test:
    sdk: flutter

! graphql 5.2.0-beta.7 from git https://github.com/zino-hofmann/graphql-flutter.git at 41cd27 in packages/graphql (overridden)

I just wanted to use local state and it doesnt work (I also tested with Hive cache backend, no difference).

Dimous commented 9 months ago

The same result with current pub dependencies.