Skip to content

Commit 1d06d0c

Browse files
committed
chore(amplify_test): Remove graphql dependency (#2570)
The `graphql` dependency is not really needed and is currently causing tests to break throughout due to it implementing an interface in a Dart pkg which recently changed.
1 parent b2a123e commit 1d06d0c

File tree

2 files changed

+96
-96
lines changed

2 files changed

+96
-96
lines changed

packages/amplify_test/lib/src/integration_test_utils/auth_cognito/integration_test_auth_utils.dart

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,85 @@
1515

1616
import 'dart:async';
1717
import 'dart:convert';
18+
1819
import 'package:amplify_flutter/amplify_flutter.dart';
19-
import 'package:graphql/client.dart';
20+
import 'package:http/http.dart' as http;
2021
import 'package:stream_transform/stream_transform.dart';
2122

2223
import 'types/confirm_sign_up_response.dart';
2324

2425
/// A GraphQL document used by the [deleteUser] test utility method.
25-
const deleteDocument = '''mutation DeleteUser(\$Username: String!) {
26-
deleteUser(Username: \$Username) {
26+
const deleteDocument = r'''mutation DeleteUser($Username: String!) {
27+
deleteUser(Username: $Username) {
2728
error
2829
success
2930
}
3031
}''';
3132

33+
final _client = http.Client();
34+
35+
Future<Map<String, Object?>> _graphQL(
36+
String document, {
37+
Map<String, Object?>? variables,
38+
}) async {
39+
final config = await Amplify.asyncConfig;
40+
final api = config.api!.awsPlugin!.default$!;
41+
final response = await _client.post(
42+
Uri.parse(api.endpoint).replace(path: '/graphql'),
43+
headers: {
44+
'x-api-key': api.apiKey!,
45+
},
46+
body: jsonEncode({
47+
'query': document,
48+
if (variables != null) 'variables': variables,
49+
}),
50+
);
51+
if (response.statusCode != 200) {
52+
throw Exception('${response.statusCode}: ${response.body}');
53+
}
54+
final responseJson = jsonDecode(response.body) as Map<String, Object?>;
55+
final result = GraphQLResponse(
56+
data: responseJson['data'] as Map<String, Object?>?,
57+
errors: (responseJson['errors'] as List?)
58+
?.cast<Map<String, Object?>>()
59+
.map(GraphQLResponseError.fromJson)
60+
.toList() ??
61+
const [],
62+
);
63+
if (result.errors.isNotEmpty) {
64+
throw Exception(result.errors);
65+
}
66+
return result.data!;
67+
}
68+
3269
/// Deletes a Cognito user in backend infrastructure.
3370
///
3471
/// This method differs from the Auth.deleteUser API in that
3572
/// an access token is not required.
3673
Future<void> deleteUser(String username) async {
37-
final config = await Amplify.asyncConfig;
38-
final url = config.auth?.awsPlugin?.appSync?.default$?.apiUrl;
39-
final key = config.auth?.awsPlugin?.appSync?.default$?.apiKey;
40-
final client = GraphQLClient(
41-
cache: GraphQLCache(),
42-
link: HttpLink(
43-
url!,
44-
defaultHeaders: {
45-
'x-api-key': key!,
46-
},
47-
),
74+
final result = await _graphQL(
75+
deleteDocument,
76+
variables: {
77+
'Username': username,
78+
},
4879
);
4980

50-
final options = MutationOptions(
51-
document: gql(
52-
r'''
53-
mutation DeleteUser(
54-
$Username: String!
55-
) {
56-
deleteUser(Username: $Username) {
57-
success
58-
exception
59-
}
60-
}
61-
''',
62-
),
63-
variables: <String, dynamic>{
64-
'Username': username,
65-
});
66-
67-
final QueryResult result = await client.mutate(options);
68-
if (result.data?['deleteUser']?['exception'] != null) {
69-
throw Exception(result.data?['deleteUser']?['exception']);
81+
final exception = (result['deleteUser'] as Map?)?['exception'] as String?;
82+
if (exception != null) {
83+
throw Exception(exception);
7084
}
7185
}
7286

7387
/// Creates a Cognito user in backend infrastructure. This documention describes
7488
/// how each parameter is expected to be used in the backend .
7589
///
76-
/// Throws [GraphQLResponseErrors] if present in the response.
90+
/// Throws an [Exception] if there are errors present in the response.
7791
///
7892
/// The [username] parameter can be plain text or a phone_number or email,
7993
/// depending on the backend configuration.
80-
/// The [password] is used as the temporary password if [autoconfirm] is true.
81-
/// The [autoconfirm] flag will mark the user as confirmed and give them a permanent password.
82-
/// The [enableMFA] flag will opt-in the user to using SMS MFA.
94+
/// The [password] is used as the temporary password if [autoConfirm] is true.
95+
/// The [autoConfirm] flag will mark the user as confirmed and give them a permanent password.
96+
/// The [enableMfa] flag will opt-in the user to using SMS MFA.
8397
/// The [verifyAttributes] flag will verify the email and phone, and should be used
8498
/// if tests need to bypass the verification step.
8599
/// The [attributes] list passes additional attributes.
@@ -91,22 +105,8 @@ Future<void> adminCreateUser(
91105
bool verifyAttributes = false,
92106
List<AuthUserAttribute> attributes = const [],
93107
}) async {
94-
final config = await Amplify.asyncConfig;
95-
final url = config.auth?.awsPlugin?.appSync?.default$?.apiUrl;
96-
final key = config.auth?.awsPlugin?.appSync?.default$?.apiKey;
97-
final client = GraphQLClient(
98-
cache: GraphQLCache(),
99-
link: HttpLink(
100-
url!,
101-
defaultHeaders: {
102-
'x-api-key': key!,
103-
},
104-
),
105-
);
106-
107-
final options = MutationOptions(
108-
document: gql(
109-
r'''
108+
final result = await _graphQL(
109+
r'''
110110
mutation AdminCreateUser(
111111
$AutoConfirm: Boolean,
112112
$Email:String,
@@ -133,48 +133,48 @@ Future<void> adminCreateUser(
133133
}
134134
}
135135
''',
136-
),
137-
variables: <String, dynamic>{
138-
'AutoConfirm': autoConfirm,
139-
'Email': attributes
140-
.firstWhere(
141-
(el) => el.userAttributeKey == CognitoUserAttributeKey.email,
142-
orElse: () => const AuthUserAttribute(
143-
userAttributeKey: CognitoUserAttributeKey.email,
144-
value: '[email protected]'))
145-
.value,
146-
'EnabledMFA': enableMfa,
147-
'Given_Name': attributes
148-
.firstWhere(
149-
(el) =>
150-
el.userAttributeKey == CognitoUserAttributeKey.givenName,
151-
orElse: () => const AuthUserAttribute(
152-
userAttributeKey: CognitoUserAttributeKey.givenName,
153-
value: 'default_given_name'))
154-
.value,
155-
'Name': attributes
156-
.firstWhere(
157-
(el) => el.userAttributeKey == CognitoUserAttributeKey.name,
158-
orElse: () => const AuthUserAttribute(
159-
userAttributeKey: CognitoUserAttributeKey.name,
160-
value: 'default_name'))
161-
.value,
162-
'Password': password,
163-
'Phone_Number': attributes
164-
.firstWhere(
165-
(el) =>
166-
el.userAttributeKey == CognitoUserAttributeKey.phoneNumber,
167-
orElse: () => const AuthUserAttribute(
168-
userAttributeKey: CognitoUserAttributeKey.phoneNumber,
169-
value: '+15555555'))
170-
.value,
171-
'Username': username,
172-
'VerifyAttributes': verifyAttributes
173-
});
136+
variables: {
137+
'AutoConfirm': autoConfirm,
138+
'Email': attributes
139+
.firstWhere(
140+
(el) => el.userAttributeKey == CognitoUserAttributeKey.email,
141+
orElse: () => const AuthUserAttribute(
142+
userAttributeKey: CognitoUserAttributeKey.email,
143+
value: '[email protected]'))
144+
.value,
145+
'EnabledMFA': enableMfa,
146+
'Given_Name': attributes
147+
.firstWhere(
148+
(el) => el.userAttributeKey == CognitoUserAttributeKey.givenName,
149+
orElse: () => const AuthUserAttribute(
150+
userAttributeKey: CognitoUserAttributeKey.givenName,
151+
value: 'default_given_name'))
152+
.value,
153+
'Name': attributes
154+
.firstWhere(
155+
(el) => el.userAttributeKey == CognitoUserAttributeKey.name,
156+
orElse: () => const AuthUserAttribute(
157+
userAttributeKey: CognitoUserAttributeKey.name,
158+
value: 'default_name'))
159+
.value,
160+
'Password': password,
161+
'Phone_Number': attributes
162+
.firstWhere(
163+
(el) =>
164+
el.userAttributeKey == CognitoUserAttributeKey.phoneNumber,
165+
orElse: () => const AuthUserAttribute(
166+
userAttributeKey: CognitoUserAttributeKey.phoneNumber,
167+
value: '+15555555'))
168+
.value,
169+
'Username': username,
170+
'VerifyAttributes': verifyAttributes
171+
},
172+
);
174173

175-
final QueryResult result = await client.mutate(options);
176-
if (result.data?['adminCreateUser']?['exception'] != null) {
177-
throw Exception(result.data?['adminCreateUser']?['exception']);
174+
final exception =
175+
(result['adminCreateUser'] as Map?)?['exception'] as String?;
176+
if (exception != null) {
177+
throw Exception(exception);
178178
}
179179
}
180180

packages/amplify_test/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies:
2121
sdk: flutter
2222
flutter_test:
2323
sdk: flutter
24-
graphql: 5.1.2
24+
http: ^0.13.5
2525
stream_transform: ^2.0.0
2626

2727
dependency_overrides:

0 commit comments

Comments
 (0)