Skip to content

Commit b6968e3

Browse files
author
Travis Sheppard
authored
fix(api): support enums in query predicates for model helpers (#1595)
1 parent c9daa5b commit b6968e3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/api/amplify_api/lib/src/graphql/graphql_request_factory.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ String _getGraphQLFilterExpression(QueryFieldOperatorType operatorType) {
341341
return result;
342342
}
343343

344-
/// Convert Temporal* and DateTime values to string if needed; otherwise return unchanged.
344+
/// Convert Temporal*, DateTime and enum values to string if needed.
345+
/// Otherwise return unchanged.
345346
dynamic _getSerializedValue(dynamic value) {
346347
if (value is TemporalDateTime ||
347348
value is TemporalDate ||
@@ -352,5 +353,8 @@ dynamic _getSerializedValue(dynamic value) {
352353
if (value is DateTime) {
353354
return _getSerializedValue(TemporalDateTime(value));
354355
}
356+
if (value is Enum) {
357+
return describeEnum(value);
358+
}
355359
return value;
356360
}

packages/api/amplify_api/test/query_predicate_graphql_filter_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import 'package:amplify_api/amplify_api.dart';
22
import 'package:amplify_api/src/graphql/graphql_request_factory.dart';
33
import 'package:amplify_core/amplify_core.dart';
44
import 'package:amplify_test/test_models/ModelProvider.dart';
5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter_test/flutter_test.dart';
67

8+
enum Size { SMALL, MEDIUM, LARGE }
9+
710
void main() {
811
group('queryPredicateToGraphQLFilter()', () {
912
// needed to fetch the schema from within the helper
@@ -209,5 +212,17 @@ void main() {
209212
_testQueryPredicateTranslation(queryPredicate, expectedFilter,
210213
modelType: Post.classType);
211214
});
215+
216+
test('query with enum should serialize to string', () {
217+
// Note: enums are not actually in the codegen model. Nonetheless, we
218+
// expect this predicate to translate as follows.
219+
final queryPredicate = Post.TITLE.eq(Size.MEDIUM);
220+
final expectedFilter = {
221+
'title': {'eq': describeEnum(Size.MEDIUM)}
222+
};
223+
224+
_testQueryPredicateTranslation(queryPredicate, expectedFilter,
225+
modelType: Post.classType);
226+
});
212227
});
213228
}

0 commit comments

Comments
 (0)