Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Dimous opened this issue Feb 20, 2024 · 1 comment

Comments

@Dimous
Copy link

Dimous commented Feb 20, 2024

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
Copy link
Author

Dimous commented Feb 20, 2024

The same result with current pub dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant