Skip to content

Cannot set currently-valued fields to null in update mutation #129

@zopf

Description

@zopf

Overview of the Issue

I cannot easily update a field on an object that currently has a value to be null.

It seems this is because the graphql-js library is stripping null variables that get passed to it, since the GraphQL spec currently does not have a concept of a null field value. Please see graphql/graphql-js#133 and graphql/graphql-spec#83 for further information and hand-wringing.

Reproduce the Error

For example, if I have a model named User, and User has a field firstName, if I run an addUser mutation with firstName set to "Alex", but then want to set firstName on that same object to be null, I cannot.

I can try to send the following update mutation:

mutation updateMyUser($input: updateUserInput!) {
  updateUser(input: $input) {
    changedUser {
      firstName
    }
    clientMutationId
  }
}

with variables:

{
  "input": {
    "clientMutationId": "justTesting",
    "firstName": null,
    "id": "VXNlcjo1NzMzNDA4MDMwM2EzMTk4M2ExZjNmNGI="
  }
}

... but when I do, I will still receive:

{
  "data": {
    "updateUser": {
      "changedUser": {
        "firstName": "Alex"
      },
      "clientMutationId": "justTesting"
    }
  }
}

Suggest a Fix

As suggested in graphql/graphql-js#133 (comment), it seems like adding a deletions field is a viable possibility to work around the fact that the GraphQL folks seem very hesitant to implement a native null value. I'm working on that myself right now in the https://github.com/wellth-app/graffiti-mongoose fork (which has by now diverged quite a bit).

I'm also tossing around the idea of creating my own pre-graffiti middleware that notes all null values in the variables tree and adds their path to a top-level deletions array on the variables tree, so that our existing clients don't have to implement the new deletions field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions