FG
๐Ÿ”Œ APIs & SDKs

GraphQL should allow mutating optional fields to 'null'.

Freshabout 20 hours ago
Mar 14, 20260 views
Confidence Score90%
90%

Problem

Unless I'm overlooking something, I'm not seeing any way to pass `null` into a mutation for an 'optional' arg. It seems that the keys with `null` values get stripped out inside GraphQL-JS itself before making it to my `resolve` in the `obj` parameter.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Enable Null Mutation for Optional GraphQL Fields

Medium Risk

In GraphQL-JS, when a mutation is executed, keys with null values are stripped out from the input object before reaching the resolver. This behavior is due to the way GraphQL handles input types, where only defined fields are included in the mutation payload. As a result, optional fields that are set to null are omitted, preventing the mutation from correctly updating those fields to null in the database.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update GraphQL Schema to Accept Null Values

    Modify the GraphQL schema to explicitly allow null values for optional fields by using the appropriate type definitions. Ensure that the fields are defined as nullable in the schema.

    graphql
    type UpdateUserInput {
      name: String
      email: String
      age: Int
      address: String
    }
  2. 2

    Modify Resolver to Handle Null Values

    Update the resolver function to check for null values and handle them appropriately. Ensure that the logic in the resolver allows for updating fields to null when the input argument is explicitly set to null.

    javascript
    const updateUser = async (_, { input }) => {
      const { name, email, age, address } = input;
      // Logic to update user in the database
      return await UserModel.update({
        name: name !== undefined ? name : null,
        email: email !== undefined ? email : null,
        age: age !== undefined ? age : null,
        address: address !== undefined ? address : null
      });
    };
  3. 3

    Test Mutation with Null Values

    Create test cases to validate the mutation by passing null values for optional fields. Ensure that the database reflects these changes correctly after the mutation is executed.

    graphql
    mutation {
      updateUser(input: { name: null, email: "test@example.com", age: null, address: "123 Main St" }) {
        id
        name
        email
        age
        address
      }
    }
  4. 4

    Document Changes in API Reference

    Update the API documentation to reflect the changes made to the mutation, including examples of how to pass null values for optional fields. This will help consumers of the API understand the new behavior.

Validation

To confirm the fix worked, execute the mutation with null values for optional fields and verify that the corresponding fields in the database are updated to null. Additionally, check the API documentation to ensure it reflects the new behavior.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

graphqlapischema