Skip to content

Commit d3795d7

Browse files
author
Travis Sheppard
authored
fix(api): do not include null values in ModelMutations.create (#2504)
1 parent 393ea2c commit d3795d7

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,10 @@ class GraphQLRequestFactory {
281281
///
282282
/// When the model has a parent via a belongsTo, the id from the parent is added
283283
/// as a field similar to "blogID" where the value is `post.blog.id`.
284-
Map<String, dynamic> buildInputVariableForMutations(Model model) {
284+
Map<String, dynamic> buildInputVariableForMutations(
285+
Model model, {
286+
required GraphQLRequestOperation operation,
287+
}) {
285288
final schema =
286289
getModelSchemaByModelName(model.getInstanceType().modelName(), null);
287290
final modelJson = model.toJson();
@@ -303,10 +306,17 @@ class GraphQLRequestFactory {
303306
}
304307
}
305308

306-
// Remove any relational fields or readonly.
309+
// Remove some fields from input.
307310
final fieldsToRemove = schema.fields!.entries
308311
.where(
309-
(entry) => entry.value.association != null || entry.value.isReadOnly,
312+
(entry) =>
313+
// relational fields
314+
entry.value.association != null ||
315+
// read-only
316+
entry.value.isReadOnly ||
317+
// null values on create operations
318+
(operation == GraphQLRequestOperation.create &&
319+
modelJson[entry.value.name] == null),
310320
)
311321
.map((entry) => entry.key)
312322
.toSet();

packages/api/amplify_api/lib/src/graphql/factories/model_mutations_factory.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ class ModelMutationsFactory extends ModelMutationsInterface {
2222
APIAuthorizationType? authorizationMode,
2323
Map<String, String>? headers,
2424
}) {
25-
final input =
26-
GraphQLRequestFactory.instance.buildInputVariableForMutations(model);
25+
final input = GraphQLRequestFactory.instance.buildInputVariableForMutations(
26+
model,
27+
operation: GraphQLRequestOperation.create,
28+
);
2729
// Does not use buildVariablesForMutationRequest because creations don't have conditions.
2830
final variables = {'input': input};
2931

@@ -96,8 +98,10 @@ class ModelMutationsFactory extends ModelMutationsInterface {
9698
}) {
9799
final condition = GraphQLRequestFactory.instance
98100
.queryPredicateToGraphQLFilter(where, model.getInstanceType());
99-
final input =
100-
GraphQLRequestFactory.instance.buildInputVariableForMutations(model);
101+
final input = GraphQLRequestFactory.instance.buildInputVariableForMutations(
102+
model,
103+
operation: GraphQLRequestOperation.update,
104+
);
101105

102106
final variables = GraphQLRequestFactory.instance
103107
.buildVariablesForMutationRequest(input: input, condition: condition);

packages/api/amplify_api/test/graphql_helpers_test.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,6 @@ void main() {
378378
'id': id,
379379
'name': name,
380380
'createdAt': time,
381-
'file': null,
382-
'files': null
383381
}
384382
};
385383
const expectedDoc =
@@ -393,6 +391,18 @@ void main() {
393391
expect(req.decodePath, 'createBlog');
394392
});
395393

394+
test(
395+
'ModelMutations.create() should not include null fields in input variable',
396+
() {
397+
const name = 'Test Blog';
398+
399+
final Blog blog = Blog(name: name);
400+
final GraphQLRequest<Blog> req = ModelMutations.create<Blog>(blog);
401+
final inputVariable = req.variables['input'] as Map<String, dynamic>;
402+
403+
expect(inputVariable.containsKey('file'), isFalse);
404+
});
405+
396406
test(
397407
'ModelMutations.create() should build a valid request for a model with a parent',
398408
() {
@@ -413,8 +423,6 @@ void main() {
413423
'id': postId,
414424
'title': title,
415425
'rating': rating,
416-
'created': null,
417-
'likeCount': null,
418426
'blogID': blogId
419427
}
420428
};

0 commit comments

Comments
 (0)