Skip to content

Commit d7e3042

Browse files
author
Travis Sheppard
committed
chore(api): make GraphQLResponse errors non-nullable
1 parent 883f312 commit d7e3042

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

packages/amplify_core/lib/src/types/api/graphql/graphql_response.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class GraphQLResponse<T> {
2222
/// This will be `null` if there are any GraphQL errors during execution.
2323
final T? data;
2424

25-
/// A list of errors from execution. If no errors, it will be `null`.
26-
final List<GraphQLResponseError>? errors;
25+
/// A list of errors from execution. If no errors, it will be an empty list.
26+
final List<GraphQLResponseError> errors;
2727

2828
const GraphQLResponse({
2929
this.data,
30-
this.errors,
30+
required this.errors,
3131
});
3232

3333
static GraphQLResponse<String?> raw({
@@ -36,10 +36,10 @@ class GraphQLResponse<T> {
3636
}) {
3737
return GraphQLResponse(
3838
data: data,
39-
errors: errors,
39+
errors: errors ?? const [],
4040
);
4141
}
4242

4343
/// Whether errors are present in the response.
44-
bool get hasErrors => errors != null && errors!.isNotEmpty;
44+
bool get hasErrors => errors.isNotEmpty;
4545
}

packages/api/amplify_api/lib/src/graphql/helpers/graphql_response_decoder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ class GraphQLResponseDecoder {
127127
}
128128
}
129129

130-
List<GraphQLResponseError>? _deserializeGraphQLResponseErrors(
130+
List<GraphQLResponseError> _deserializeGraphQLResponseErrors(
131131
Map<String, dynamic>? response,
132132
) {
133133
final errors = response?['errors'] as List?;
134134
if (errors == null || errors.isEmpty) {
135-
return null;
135+
return [];
136136
}
137137
return errors
138138
.cast<Map<String, dynamic>>()

packages/api/amplify_api/test/graphql_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void main() {
198198
final expected = json.encode(_expectedQuerySuccessResponseBody['data']);
199199

200200
expect(res.data, equals(expected));
201-
expect(res.errors, equals(null));
201+
expect(res.errors, isEmpty);
202202
});
203203

204204
test('Query returns proper response.data with dynamic type', () async {
@@ -222,7 +222,7 @@ void main() {
222222
final expected = json.encode(_expectedQuerySuccessResponseBody['data']);
223223

224224
expect(res.data, equals(expected));
225-
expect(res.errors, equals(null));
225+
expect(res.errors, isEmpty);
226226
});
227227

228228
test('Mutate returns proper response.data', () async {
@@ -245,7 +245,7 @@ void main() {
245245
final expected = json.encode(_expectedMutateSuccessResponseBody['data']);
246246

247247
expect(res.data, equals(expected));
248-
expect(res.errors, equals(null));
248+
expect(res.errors, isEmpty);
249249
});
250250
});
251251
group('Model Helpers', () {
@@ -270,7 +270,7 @@ void main() {
270270
// response asserts
271271
expect(res.data, isA<Blog>());
272272
expect(res.data?.id, _modelQueryId);
273-
expect(res.errors, equals(null));
273+
expect(res.errors, isEmpty);
274274
});
275275
});
276276

@@ -387,7 +387,7 @@ void main() {
387387
);
388388

389389
expect(res.data, equals(null));
390-
expect(res.errors?.single, equals(errorExpected));
390+
expect(res.errors.single, equals(errorExpected));
391391
});
392392

393393
test('request with custom auth mode and auth error', () async {
@@ -414,7 +414,7 @@ void main() {
414414
);
415415

416416
expect(res.data, equals(null));
417-
expect(res.errors?.single, equals(errorExpected));
417+
expect(res.errors.single, equals(errorExpected));
418418
});
419419

420420
test('canceled query request should never resolve', () async {

0 commit comments

Comments
 (0)