Skip to content

Commit

Permalink
some round-trip troubleshooting
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed Jan 20, 2021
1 parent fbc5a2d commit 930c40d
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 213 deletions.
25 changes: 19 additions & 6 deletions packages/graphql/test/cache/cache_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ final typelessTest = TestCase(
a {
# union
list {
__typename
#__typename
value
... on Item { id }
#... on Item { id }
}
b {
id
Expand All @@ -327,10 +327,23 @@ final typelessTest = TestCase(
data: {
'a': {
'list': [
{'__typename': 'Num', 'value': 1},
{'__typename': 'Num', 'value': 2},
{'__typename': 'Num', 'value': 3},
{'__typename': 'Item', 'id': 4, 'value': 4}
{
//'__typename': 'Num',
'value': 1,
},
{
//'__typename': 'Num',
'value': 2,
},
{
//'__typename': 'Num',
'value': 3,
},
{
//'__typename': 'Item',
//'id': 4,
'value': 4,
}
],
'b': {
'id': 5,
Expand Down
123 changes: 123 additions & 0 deletions packages/graphql/test/fetch_policy_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:gql_exec/gql_exec.dart';
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';

import 'package:graphql/client.dart';
import 'package:gql/language.dart';

import './helpers.dart';

void main() {
const String readRepositories = r'''
query ReadRepositories($nRepositories: Int!) {
viewer {
repositories(last: $nRepositories) {
nodes {
__typename
id
name
viewerHasStarred
}
}
}
}
''';
readRepositoryData({withTypenames = true, withIds = true}) {
return {
'viewer': {
'repositories': {
'nodes': [
{
if (withIds) 'id': 'MDEwOlJlcG9zaXRvcnkyNDgzOTQ3NA==',
'name': 'pq',
'viewerHasStarred': false
},
{
if (withIds) 'id': 'MDEwOlJlcG9zaXRvcnkzMjkyNDQ0Mw==',
'name': 'go-evercookie',
'viewerHasStarred': false
},
{
if (withIds) 'id': 'MDEwOlJlcG9zaXRvcnkzNTA0NjgyNA==',
'name': 'watchbot',
'viewerHasStarred': false
},
]
.map((map) =>
withTypenames ? {'__typename': 'Repository', ...map} : map)
.toList(),
},
},
};
}

MockLink link;
GraphQLClient client;

group('FetchPolicy', () {
setUp(() {
link = MockLink();

client = GraphQLClient(
cache: getTestCache(),
link: link,
);
});

group('query', () {
// TODO cacheFirst code path: Return result from cache. Only fetch from network if cached result is not available.
// TODO cacheAndNetwork code path: Return result from cache first (if it exists), then return network result once it's available.
// TODO cacheOnly code path: Return result from cache if available, fail otherwise.
// TODO noCache code path: Return result from network, fail if network call doesn't succeed, don't save to cache.
// TODO networkOnly code path: Return result from network, fail if network call doesn't succeed, save to cache.
test('switch to cacheOnly returns cached data', () async {
final _options = QueryOptions(
fetchPolicy: FetchPolicy.cacheAndNetwork,
document: parseString(readRepositories),
variables: <String, dynamic>{
'nRepositories': 42,
},
);
final repoData = readRepositoryData(withTypenames: true);

when(
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable([
Response(data: repoData),
]),
);

final QueryResult r = await client.query(_options);

verify(
link.request(
Request(
operation: Operation(
document: parseString(readRepositories),
//operationName: 'ReadRepositories',
),
variables: <String, dynamic>{
'nRepositories': 42,
},
context: Context(),
),
),
);

expect(r.exception, isNull);
expect(r.data, equals(repoData));

final QueryResult cacheResult = await client.query(QueryOptions(
fetchPolicy: FetchPolicy.cacheOnly,
document: parseString(readRepositories),
variables: <String, dynamic>{
'nRepositories': 42,
},
));
expect(cacheResult.exception, isNull);
expect(cacheResult.data, equals(repoData));
});
});
});
}
73 changes: 0 additions & 73 deletions packages/graphql/test/graphql_client_test.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import 'dart:convert';

import 'package:gql_exec/gql_exec.dart';
import 'package:gql_link/gql_link.dart';
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';

import 'package:graphql/client.dart';
import 'package:gql/language.dart';
import './mutation_sequence_issue_data.dart' as order;

import './helpers.dart';

class MockLink extends Mock implements Link {}

void main() {
const String readSingle = r'''
query ReadSingle($id: ID!) {
Expand Down Expand Up @@ -447,74 +442,6 @@ void main() {
result.data['action']['starrable']['viewerHasStarred'] as bool;
expect(viewerHasStarred, true);
});

/// https://github.com/zino-app/graphql-flutter/issues/747#issuecomment-731649547
test('watchQuery mutations are isolated', () async {
//client.writeFragment(
// FragmentRequest(
// fragment: Fragment(document: gql(order.fragment)),
// idFields: {'__typename': 'Order', 'id': order.closedOrder['id']},
// variables: {'placeId': (order.closedOrder['place'] as Map)['id']},
// ),
// data: order.closedOrder,
//);
//print(client.cache.store.toMap());

int count = 0;

final mutation = gql(order.mutation);
var _options = MutationOptions(
document: mutation,
//fetchPolicy: FetchPolicy.cacheAndNetwork,
variables: {
'placeId': (order.expectedResult['place'] as Map)['id'],
'lines': [order.closedOrderLineCreate],
'name': null
},
);

when(
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable(
[
Response(
data: {
'createOrder':
count < 1 ? order.closedOrder : order.expectedResult,
'__typename': 'Mutation'
},
),
],
),
);

var observableQuery = await client.watchQuery(WatchQueryOptions(
document: _options.document,
variables: _options.variables,
fetchResults: false,
));
var multiResult = await observableQuery.fetchResults();
var result = await multiResult.networkResult;
expect(result.data['createOrder'], equals(order.closedOrder));

count += 1;

observableQuery.options.variables = {
'placeId': (order.expectedResult['place'] as Map)['id'],
'lines': [order.closedOrderLineCreate],
'name': null
};

multiResult = await observableQuery.fetchResults();
print(multiResult.eagerResult);
result = await multiResult.networkResult;

//print(jsonEncode(client.cache.store.toMap()));

expect(result.hasException, isFalse);
expect(result.data['createOrder'], equals(order.expectedResult));
});
});

group('subscription', () {
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql/test/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:async';

import 'package:graphql/client.dart';
import 'package:mockito/mockito.dart';

class MockLink extends Mock implements Link {}

const debuggingUnexpectedTestFailures = false;

Expand Down
Loading

0 comments on commit 930c40d

Please sign in to comment.