File tree Expand file tree Collapse file tree 3 files changed +33
-11
lines changed
lib/src/graphql/factories Expand file tree Collapse file tree 3 files changed +33
-11
lines changed Original file line number Diff line number Diff line change @@ -281,7 +281,10 @@ class GraphQLRequestFactory {
281
281
///
282
282
/// When the model has a parent via a belongsTo, the id from the parent is added
283
283
/// 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
+ }) {
285
288
final schema =
286
289
getModelSchemaByModelName (model.getInstanceType ().modelName (), null );
287
290
final modelJson = model.toJson ();
@@ -303,10 +306,17 @@ class GraphQLRequestFactory {
303
306
}
304
307
}
305
308
306
- // Remove any relational fields or readonly .
309
+ // Remove some fields from input .
307
310
final fieldsToRemove = schema.fields! .entries
308
311
.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 ),
310
320
)
311
321
.map ((entry) => entry.key)
312
322
.toSet ();
Original file line number Diff line number Diff line change @@ -22,8 +22,10 @@ class ModelMutationsFactory extends ModelMutationsInterface {
22
22
APIAuthorizationType ? authorizationMode,
23
23
Map <String , String >? headers,
24
24
}) {
25
- final input =
26
- GraphQLRequestFactory .instance.buildInputVariableForMutations (model);
25
+ final input = GraphQLRequestFactory .instance.buildInputVariableForMutations (
26
+ model,
27
+ operation: GraphQLRequestOperation .create,
28
+ );
27
29
// Does not use buildVariablesForMutationRequest because creations don't have conditions.
28
30
final variables = {'input' : input};
29
31
@@ -96,8 +98,10 @@ class ModelMutationsFactory extends ModelMutationsInterface {
96
98
}) {
97
99
final condition = GraphQLRequestFactory .instance
98
100
.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
+ );
101
105
102
106
final variables = GraphQLRequestFactory .instance
103
107
.buildVariablesForMutationRequest (input: input, condition: condition);
Original file line number Diff line number Diff line change @@ -378,8 +378,6 @@ void main() {
378
378
'id' : id,
379
379
'name' : name,
380
380
'createdAt' : time,
381
- 'file' : null ,
382
- 'files' : null
383
381
}
384
382
};
385
383
const expectedDoc =
@@ -393,6 +391,18 @@ void main() {
393
391
expect (req.decodePath, 'createBlog' );
394
392
});
395
393
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
+
396
406
test (
397
407
'ModelMutations.create() should build a valid request for a model with a parent' ,
398
408
() {
@@ -413,8 +423,6 @@ void main() {
413
423
'id' : postId,
414
424
'title' : title,
415
425
'rating' : rating,
416
- 'created' : null ,
417
- 'likeCount' : null ,
418
426
'blogID' : blogId
419
427
}
420
428
};
You can’t perform that action at this time.
0 commit comments