Skip to content

fix(graphql): check required fields on new nested nodes without XID - #9892

Open
yuminn-k wants to merge 2 commits into
dgraph-io:mainfrom
yuminn-k:fix-8781-ensure-nonnulls-new-node
Open

yuminn-k wants to merge 2 commits into
dgraph-io:mainfrom
yuminn-k:fix-8781-ensure-nonnulls-new-node

Conversation

@yuminn-k

@yuminn-k yuminn-k commented Nov 16, 2026 •

Copy link
Copy Markdown

Description

Fixes #8781.

A deep set update (or add) can create a new nested node that has no XID and no ID. Such a node was written without checking !-typed fields, because EnsureNonNulls ran only on the branch that creates a new node from an XID. The ...Ref input types make every field optional, so the schema did not catch it either, and a node missing a required field was stored.

This change calls EnsureNonNulls on the new-node path too, excluding the @hasInverse field set from the parent, the same way the XID branch does. The top-level update case still returns earlier and is not affected.

Added a rewriting test in update_mutation_test.yaml: updateAuthor with set: {posts: [{text: ...}]} now fails with type Post requires a value for field title, but no value present.

Checked with go test ./graphql/resolve ./graphql/schema. Without the fix, the new test fails (Expected value not to be nil); with it, both packages pass.

Checklist

  • The PR title follows the
    Conventional Commits syntax, leading
    with fix:, feat:, chore:, ci:, etc.
  • Tests added for new functionality, or regression tests for bug fixes added as applicable

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Summary by CodeRabbit

  • Bug Fixes
    • Updates now report an error when a newly added nested post is missing its required title, rather than proceeding with the incomplete post.

@yuminn-k
yuminn-k requested a review from a team as a code owner November 16, 2026 22:34
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The mutation rewriter now checks required fields when it creates a nested reference-input node. A new update-mutation test checks that rewriting fails when a nested Post does not include its required title.

Changes: Nested update validation

Layer / File(s) Summary
Validate required fields before creating nested nodes
graphql/resolve/mutation_rewriter.go, graphql/resolve/update_mutation_test.yaml
The rewriter excludes the inverse field from required-field validation and returns an error when validation fails. The test checks a nested Post without a title.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: area/graphql, go

Suggested reviewers: matthewmcneely, shiva-istari

Merge Risk: Minimal · up to 43799

Deep set updates that create nested nodes without an ID or XID now fail when required fields are missing, instead of storing incomplete records. Valid inputs are unaffected, and a regression test covers the case. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: adding required-field validation (EnsureNonNulls) on nested nodes created without an XID or ID. The raw summary and objectives confirm this matches the mutation_rewrit…
Linked Issues check Passed Issue #8781 requires that a deep update creating a nested object fail when required field values are missing from the input. The whole-PR diff (merge base to head) confirms the change in graphql/resol…
Out of Scope Changes check Passed The whole-PR diff contains two hunks. The first is an 11-line addition in graphql/resolve/mutation_rewriter.go that adds the EnsureNonNulls check on the new-node path of rewriteObject, which implement…
Full details: Title check

Explanation

The title names the main change: adding required-field validation (EnsureNonNulls) on nested nodes created without an XID or ID. The raw summary and objectives confirm this matches the mutation_rewriter.go change and the new YAML regression test. The title follows the conventional fix(scope) form and is specific enough for a reviewer scanning history.

Full details: Linked Issues check

Explanation

Issue #8781 requires that a deep update creating a nested object fail when required field values are missing from the input. The whole-PR diff (merge base to head) confirms the change in graphql/resolve/mutation_rewriter.go, rewriteObject, on the new-node path (the branch after the XID lookup, before newObj["dgraph.type"] is set). The code computes exclude from srcField.Inverse() when srcField is non-nil, calls typ.EnsureNonNulls(obj, exclude), and on error appends the error and returns without creating the node. This addresses the issue example: a nested Member without id or XID, missing the required nickname, now fails. The whole-PR diff of graphql/resolve/update_mutation_test.yaml adds a regression case: updateAuthor with set: {posts: [{text: ...}]} expects the error 'type Post requires a value for field title, but no value present'. The top-level update path returns earlier and is unchanged, as the PR description states. The PR description reports the test fails without the fix; the test was not executed in this review. The AI-generated summary says no check runs when srcField is nil. The diff shows the check runs in that case with an empty exclude. This review uses the diff, so the summary is not relied on for this point. Automated tests in the PR cover the change.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff contains two hunks. The first is an 11-line addition in graphql/resolve/mutation_rewriter.go that adds the EnsureNonNulls check on the new-node path of rewriteObject, which implements the #8781 objective. The second is a 22-line YAML regression test in graphql/resolve/update_mutation_test.yaml that exercises that path. No unrelated refactoring, documentation, or API changes appear in the reviewed diffs. The check also applies to top-level calls where srcField is nil. It only rejects inputs that omit required fields, which matches the issue intent, so it does not appear to be an unrelated behavior change. Evidence limit: the surrounding code and other callers of rewriteObject were not inspected, so the effect on other mutation paths is not fully verified.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 78c7db12-ce37-47b8-83e3-7797fd941555
Commits

Reviewing files that changed from the base of the PR and between bd09f1b and 4379969.

Files selected for processing: 1

  • graphql/resolve/update_mutation_test.yaml
Files skipped from review as they are similar to previous changes: 1

  • graphql/resolve/update_mutation_test.yaml

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@shiva-istari
shiva-istari force-pushed the fix-8781-ensure-nonnulls-new-node branch from 14b3550 to bd09f1b Compare November 19, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Deep update should not be possible if mandatory fields are not present in the input

1 participant