Skip to content

Commit

Permalink
Merge branch 'release-candidate' into merge-rc-to-main-0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Sheppard committed Feb 16, 2022
2 parents af07793 + 95cd352 commit 650120a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ void main() {
var secondRes = await Amplify.API.query(request: secondReq!).response;
var secondData = secondRes.data;
expect(secondData?.items.length, limit);
expect(secondData?.items[0].id, isNot(firstData?.items[0].id));
final firstId = firstData?.items[0]?.id;
final secondId = secondData?.items[0]?.id;
expect(firstId?.isNotEmpty, isTrue);
expect(secondId?.isNotEmpty, isTrue);
expect(firstId, isNot(secondId));
});

testWidgets('should LIST blogs with Model helper with query predicate',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class PaginatedModelTypeImpl<T extends Model> extends PaginatedModelType<T> {
}

final items = itemsJson
.cast<Map>()
.cast<Map?>()
.map(
// ignore: implicit_dynamic_method
(e) => modelType.fromJson(e.cast()),
(e) => e != null ? modelType.fromJson(e.cast()) : null,
)
.toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'package:amplify_api/src/graphql/paginated_model_type_impl.dart';
import 'package:amplify_core/amplify_core.dart';

class PaginatedResultImpl<T extends Model> extends PaginatedResult<T> {
const PaginatedResultImpl(List<T> items, int? limit, String? nextToken,
const PaginatedResultImpl(List<T?> items, int? limit, String? nextToken,
Map<String, dynamic>? filter)
: super(items, limit, nextToken, filter);

Expand Down
3 changes: 2 additions & 1 deletion packages/amplify_api/lib/src/graphql/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ Map<String, dynamic> transformAppSyncJsonToModelJson(
// check for list at top-level and tranform each entry
if (isPaginated && _input[items] is List) {
final transformedItems = (_input[items] as List)
.map((dynamic e) => transformAppSyncJsonToModelJson(e, modelSchema))
.map((dynamic e) =>
e != null ? transformAppSyncJsonToModelJson(e, modelSchema) : null)
.toList();
_input.update(items, (dynamic value) => transformedItems);
return _input;
Expand Down
63 changes: 62 additions & 1 deletion packages/amplify_api/test/graphql_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,41 @@ void main() {
request: req, data: data, errors: errors);

expect(response.data, isA<PaginatedResult<Blog>>());
expect(response.data?.items, isA<List<Blog>>());
expect(response.data?.items, isA<List<Blog?>>());
expect(response.data?.items.length, 2);
});

test(
'ModelQueries.list() should decode gracefully when there is a null in the items list',
() async {
GraphQLRequest<PaginatedResult<Blog>> req =
ModelQueries.list<Blog>(Blog.classType, limit: 2);

List<GraphQLResponseError> errors = [];
String data = '''{
"listBlogs": {
"items": [
{
"id": "test-id-1",
"name": "Test Blog 1",
"createdAt": "2021-07-29T23:09:58.441Z"
},
null
],
"nextToken": "super-secret-next-token"
}
}''';

GraphQLResponse<PaginatedResult<Blog>> response =
GraphQLResponseDecoder.instance.decode<PaginatedResult<Blog>>(
request: req, data: data, errors: errors);

expect(response.data, isA<PaginatedResult<Blog>>());
expect(response.data?.items, isA<List<Blog?>>());
expect(response.data?.items.length, 2);
expect(response.data?.items[1], isNull);
});

test(
'GraphQLResponse<PaginatedResult<Blog>> can get the request for next page of data',
() async {
Expand Down Expand Up @@ -689,6 +720,36 @@ void main() {
expect(output, expectedOutput);
});

test('should translate list with a null entry to expected format', () {
final input = <String, dynamic>{
'items': [
{
'id': 'xyz456',
'title': 'Lorem Ipsum',
'rating': 0,
'blog': {'id': 'abc123', 'title': 'blog about life'}
},
null
]
};
final expectedOutput = <String, dynamic>{
'items': [
{
'id': 'xyz456',
'title': 'Lorem Ipsum',
'rating': 0,
'blog': {
'serializedData': {'id': 'abc123', 'title': 'blog about life'}
}
},
null
]
};
final output = transformAppSyncJsonToModelJson(input, Post.schema,
isPaginated: true);
expect(output, expectedOutput);
});

test('should result in no-op if wrong schema provided', () {
final input = <String, dynamic>{
'id': 'xyz456',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:amplify_core/amplify_core.dart';
import '../../types.dart';

abstract class PaginatedResult<T extends Model> extends Model {
final List<T> items;
final List<T?> items;
final int? limit;
final String? nextToken;
final Map<String, dynamic>? filter;
Expand Down
8 changes: 4 additions & 4 deletions packages/amplify_authenticator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ environment:
flutter: ">=1.17.0"

dependencies:
amplify_auth_cognito: ^0.3.0
amplify_auth_plugin_interface: ^0.3.0
amplify_core: ^0.3.0
amplify_flutter: ^0.3.0
amplify_auth_cognito: ">=0.3.0 <0.5.0"
amplify_auth_plugin_interface: ">=0.3.0 <0.5.0"
amplify_core: ">=0.3.0 <0.5.0"
amplify_flutter: ">=0.3.0 <0.5.0"
collection: ^1.15.0
flutter:
sdk: flutter
Expand Down

0 comments on commit 650120a

Please sign in to comment.