-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I send request with null field? #473
Comments
This is currently not supported. It was a limitation of built_value when this library was written originally. However, built_value supports this now, so we could add an option to ferry_generator to add This would, however, lead to all fields being included and being sent as null even if they were never set, so it's also not a really good solution. A complete fix would probably require wrapping all nullable values in something like this. At the moment, I can only offer you a workaround: Write your own class that implements the G...Var() type and implement your own toJson() where you decide what's included or omitted. |
@knaeckeKami Can you provide sample code or how to implements the |
You could for example add an additional bool field |
I was faced with this issue and ended up with custom serializer: class PassportSerializer extends JsonSerializer<GupdatePassportVars>
implements StructuredSerializer<GupdatePassportVars> {
@override
GupdatePassportVars fromJson(Map<String, dynamic> json) {
throw UnimplementedError();
}
@override
Map<String, dynamic> toJson(GupdatePassportVars object) {
final passport = object.passport;
final json = object.passport.toJson();
if (passport.series == null) {
json['series'] = null;
}
if (passport.number == null) {
json['number'] = null;
}
return {
"passportId": object.passportId?.value,
"passport": json
};
}
}
Hope it will be fixed soon. Thx for your job! |
Thanks for sharing a workaround! I have a draft solution here: gql-dart/gql#381 I am not sure when I can make it production-ready (especially tests, make this an opt-in feature to avoid breaking changes, and code cleanup). |
Extending on what @zombie6888 has posted. Here is a full example: import 'package:built_value/serializer.dart';
// ignore: implementation_imports
import 'package:gql_code_builder/src/serializers/json_serializer.dart';
import '../../utils/date_utils.dart';
// IMPORT GQueryVars
class QueryVarsSerializer extends JsonSerializer<GQueryVars>
implements StructuredSerializer<GQueryVars> {
@override
GQueryVars fromJson(Map<String, dynamic> json) {
final builder = GQueryVarsBuilder()
..id = json['id']
..nullableField = dateTimeFromJsonValue(json['nullableField']);
return builder.build();
}
@override
Map<String, dynamic> toJson(GQueryVars object) =>
{'id': object.id, 'nullableField': object.nullableField};
// Required override method needed to serialize a null value
// Needs to return a list of all of the key value pairs
@override
Iterable<Object?> serialize(
Serializers serializers,
GQueryVars object, {
FullType specifiedType = FullType.unspecified,
}) =>
['id', object.id, 'nullableField', object.nullableField];
} Then in build.yaml: targets:
$default:
builders:
ferry_generator|serializer_builder:
enabled: true
options:
schema: app|lib/graphql/schema.graphql
custom_serializers:
- import: "IMPORT LOCATION"
name: QueryVarsSerializer |
Fixed by #549 |
I want to update a field with null.
So I did something like below.
field_name
is nullable field in graphql schema.when I submit the request, it doesn't send the
field_name
field.How can I send field with null value?
The text was updated successfully, but these errors were encountered: