Skip to content

Commit 42990f5

Browse files
authored
feat(api): add copyWith to GraphQLRequest (#4365)
1 parent 3859b16 commit 42990f5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,39 @@ class GraphQLRequest<T> with AWSSerializable<Map<String, Object?>> {
7474
'authorizationMode': authorizationMode!.name,
7575
if (decodePath != null) 'decodePath': decodePath,
7676
};
77+
78+
/// Creates a copy of this request with the given fields replaced with the new values.
79+
/// If no new value is given, the old value is used.
80+
///
81+
/// ```dart
82+
/// final original = ModelQueries.list(
83+
/// Blog.classType,
84+
/// );
85+
/// final modified = original.copyWith(
86+
/// document: yourCustomQuery,
87+
/// );
88+
/// ```
89+
///
90+
/// Useful in advanced scenarios where you want to modify the request before sending it.
91+
///
92+
/// See https://docs.amplify.aws/lib/graphqlapi/advanced-workflows/q/platform/flutter/.
93+
GraphQLRequest<T> copyWith({
94+
String? document,
95+
String? apiName,
96+
Map<String, String>? headers,
97+
APIAuthorizationType? authorizationMode,
98+
Map<String, dynamic>? variables,
99+
String? decodePath,
100+
ModelType? modelType,
101+
}) {
102+
return GraphQLRequest<T>(
103+
document: document ?? this.document,
104+
apiName: apiName ?? this.apiName,
105+
headers: headers ?? this.headers,
106+
authorizationMode: authorizationMode ?? this.authorizationMode,
107+
variables: variables ?? this.variables,
108+
decodePath: decodePath ?? this.decodePath,
109+
modelType: modelType ?? this.modelType,
110+
);
111+
}
77112
}

packages/api/amplify_api/example/integration_test/graphql/iam_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,37 @@ void main({bool useExistingTestUser = false}) {
188188
expect(postFromResponse?.title, title);
189189
});
190190

191+
testWidgets('should copyWith request', (WidgetTester tester) async {
192+
final title = 'Lorem Ipsum Test Post: ${uuid()}';
193+
const rating = 0;
194+
final createdPost = await addPostAndBlog(title, rating);
195+
final blogId = createdPost.blog?.id;
196+
197+
// Original request with mock id
198+
final req = ModelQueries.list(
199+
Post.classType,
200+
where: Post.BLOG.eq(uuid()),
201+
limit: _limit,
202+
);
203+
204+
// Copy request with actual blog id
205+
final copiedRequest = req.copyWith(
206+
variables: {
207+
...req.variables,
208+
'filter': {
209+
'blogID': {'eq': blogId},
210+
},
211+
},
212+
);
213+
final res = await Amplify.API.query(request: copiedRequest).response;
214+
final postFromResponse = res.data?.items[0];
215+
216+
expect(res, hasNoGraphQLErrors);
217+
expect(postFromResponse?.blog?.id, isNotNull);
218+
expect(postFromResponse?.blog?.id, blogId);
219+
expect(postFromResponse?.title, title);
220+
});
221+
191222
testWidgets('should decode a custom list request',
192223
(WidgetTester tester) async {
193224
final name = 'Lorem Ipsum Test Blog: ${uuid()}';

0 commit comments

Comments
 (0)