Skip to content
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

Unable to set a field to null #191

Open
seanconnollydev opened this issue Feb 11, 2021 · 4 comments
Open

Unable to set a field to null #191

seanconnollydev opened this issue Feb 11, 2021 · 4 comments

Comments

@seanconnollydev
Copy link

Context

I am unable to set a field to null by executing a mutation that includes a null input field value. I believe I should be able to do this, but the generated serializer removes null input fields from request variables.
Consider the following input schema:

input CommunityInput {
  name: String!
  websiteUrl: String # <-- Nullable
}

And associated mutation:

updateCommunity(data: CommunityInput!, id: String!): Community!

I am using ferry to create the following request:

final request = GUpdateCommunitySettingsReq(
      (b) => b
        ..vars.id = "communityId"
        ..vars.data.name = "Community Name"
        ..vars.data.websiteUrl = null, // <--- This is null
    );

Problem

This results in a GraphQL request over the wire that strips the websiteUrl entirely. The variables look like this:

{
  "data": {
    "name": "Community Name"
  },
  "id": "a3c5d70e-3417-11eb-ae0f-7bd5cfb"
}

With a raw GraphQL request, the below mutation would set the websiteUrl field to null, which is the behavior I expect when making an equivalent call:

mutation {
  updateCommunity(
    data: {
      name: "Community Name",
      websiteUrl: null
    },
    id: "a3c5d70e-3417-11eb-ae0f-7bd5cfb85ea6"
  ) {
    id
    name
    websiteUrl
  }
}

I traced the issue to the generated serializer for the input type, which looks like this:

class _$GCommunityInputSerializer
    implements StructuredSerializer<GCommunityInput> {
  @override
  final Iterable<Type> types = const [GCommunityInput, _$GCommunityInput];
  @override
  final String wireName = 'GCommunityInput';

  @override
  Iterable<Object> serialize(Serializers serializers, GCommunityInput object,
      {FullType specifiedType = FullType.unspecified}) {
    final result = <Object>[
      'name',
      serializers.serialize(object.name, specifiedType: const FullType(String)),
    ];
    if (object.websiteUrl != null) { // <-- This null check removes the field entirely from the request.
      result
        ..add('websiteUrl')
        ..add(serializers.serialize(object.websiteUrl,
            specifiedType: const FullType(String)));
    }
    return result;
  }
@micimize
Copy link
Contributor

looks like built_value will re-add this capability google/built_value.dart#604

@seanconnollydev
Copy link
Author

Thanks! Sorry for opening this in probably the wrong repo, it's taken me a bit to figure out what's where here 😄

@micimize
Copy link
Contributor

You're good – I think it's fair to leave this open until built_value makes it possible so we can track the feature

@micimize micimize reopened this Apr 13, 2021
@fabiancrx
Copy link

It seems like google/built_value.dart#604 is already merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants