graphql-to-dart icon indicating copy to clipboard operation
graphql-to-dart copied to clipboard

toJson() should not include items which were not set

Open isaiahtaylorhh opened this issue 4 years ago • 1 comments

final testInput = CreateTestInput(fkID: '123', color: 'red');

print('${createTestInput.toJson()}');
// This prints { fkID: 123, color: red, otherAttr: null, id: null, ...}

This becomes a problem when trying to use this library with https://github.com/zino-app/graphql-flutter because in most cases you don't want to explicitly null things out.

Workaround:

final newItemResult = await client.mutate(MutationOptions(
  documentNode: gql(mutationString),
  variables: { 'input': createTestInput.toJson()
    ..removeWhere((_, dynamic val) => val == null) // remove the null items
}));

isaiahtaylorhh avatar Mar 14 '20 15:03 isaiahtaylorhh

I've been rewriting graphql-to-dart as built-graphql recently, for which nulls will be excluded by default.

micimize avatar Mar 15 '20 01:03 micimize