Skip to content

fix(graphql): avoid invalid HNSW filters when updates are disabled - #9837

Open
tonisole wants to merge 2 commits into
dgraph-io:mainfrom
tonisole:fix/graphql-vector-filters
Open

tonisole wants to merge 2 commits into
dgraph-io:mainfrom
tonisole:fix/graphql-vector-filters

Conversation

@tonisole

@tonisole tonisole commented Sep 22, 2026 •

Copy link
Copy Markdown

Description

Fixes #9838

Fix GraphQL schema generation for indexed embeddings on types with
@generate(mutation: { update: false }).

Why we encountered this

We encountered this while adding semantic search to an existing GraphQL API.
Some types deliberately disable generated mutations because application-owned
custom mutations enforce domain validation and business rules. Those types still
need generated read and similarity queries.

Adding an indexed embedding made the schema invalid. Enabling generated updates
would change the intended write API, so it is not an appropriate workaround.
Vector search should not depend on whether an unrelated update operation is
generated.

Minimal reproduction

Submit this schema to /admin/schema/validate:

type VectorSchemaProbe
  @generate(mutation: { add: false, update: false, delete: false }) {
  id: ID!
  name: String
  embedding: [Float!] @embedding @search(by: ["hnsw(metric: cosine)"])
}

On unmodified v25.4.1, validation fails with:

Undefined type HNSWSearchFilter.

The same failure is reproducible using the schema generator on current main
at ddc4841. Changing only update to true makes validation succeed.

Root cause

addFilterType treats the HNSW index as an ordinary field filter and emits a
reference to HNSWSearchFilter, although that input type does not exist.

When generated updates are enabled, cleanupInput happens to remove the invalid
reference while traversing Update...Input and its filter input. With updates
disabled, that traversal does not happen and the invalid reference survives.

Fix

Skip @embedding fields when constructing ordinary field filters. HNSW indexes
are consumed by similarity queries, not ordinary field filters.

This preserves:

  • The embedding field and cosine DQL index.
  • Ordinary indexed-field filters and the has filter.
  • Generated get, ByEmbedding, and ById queries.
  • The requested add/update/delete generation settings.

The production change is four lines. A regression test covers all eight
add/update/delete combinations and checks the preserved schema surfaces.

Validation

  • Before the fix, the regression fails on all four combinations with
    update: false and passes on the other four.
  • After the fix, the full graphql/schema package passes:
    go test -race -count=1 ./graphql/schema.
  • go vet ./graphql/schema, gofmt, and git diff --check pass.
  • The Dgraph binary compiles with the jemalloc build tag.
  • A locally patched v25.4.1 server accepts all eight combinations and a complete
    application schema through /admin/schema/validate.
  • Application integration tests against the patched server pass for six
    1024-dimensional cosine-vector predicates, including insertion, replacement,
    deletion, and concurrent-source-change handling. Application-specific fixtures
    are not included in this contribution.

This fixes an existing feature; no new public API or documentation change is
proposed.

Checklist

  • The PR title follows Conventional Commits syntax.
  • Code compiles correctly.
  • Regression tests are included and pass.
  • Formatting, diff checks, and targeted Go vet pass.
  • Full Trunk linting passes locally. The Trunk CLI is not installed in the
    current validation environment.

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Summary by CodeRabbit

  • New Features
    • GraphQL schemas can now support HNSW embedding searches without requiring a standard filter type.
    • Embedding fields are excluded from ordinary filter generation, while generated schemas can still provide embedding-similarity queries.
    • Types configured without update mutations continue to expose their available queries and add/delete operations without an update operation.

Skip embedding fields in ordinary filter generation so vector schemas remain valid when generated updates are disabled.

Cover all eight mutation-generation combinations and preserve cosine indexes, read queries, and ordinary filters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tonisole
tonisole requested a review from a team as a code owner September 22, 2026 10:25
@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 7c720202-e366-4b19-b6c0-2845a995b846

📥 Commits

Reviewing files that changed from the base of the PR and between f3e08da and a7ae10e.

📒 Files selected for processing (4)
  • graphql/schema/gqlschema.go
  • graphql/schema/rules.go
  • graphql/schema/testdata/schemagen/input/embedding-directive-with-generate-restrictions.graphql
  • graphql/schema/testdata/schemagen/output/embedding-directive-with-generate-restrictions.graphql
💤 Files with no reviewable changes (1)
  • graphql/schema/gqlschema.go

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


📝 Walkthrough

Walkthrough

The schema generator now excludes @embedding fields from ordinary GraphQL filters. A regression test validates vector schema generation across all mutation flag combinations.

Changes

Vector filter generation

Layer / File(s) Summary
Embedding filter exclusion and regression coverage
graphql/schema/gqlschema.go, graphql/schema/vector_generate_regression_test.go
addFilterType skips @embedding fields. The regression test checks schema parsing, vector filters and queries, mutation flags, undefined filter absence, and the cosine HNSW predicate.

Priority: ⬇️ Low

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

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: matthewmcneely

Merge Risk: ⚪ Minimal · up to a7ae1

No actionable issue is established for this change; it is mergeable after normal checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. (2 skipped: 2… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing invalid HNSW filters when generated updates are disabled.
Linked Issues check ✅ Passed The changes satisfy issue [#9838]. addFilterType skips fields with @embedding, and builtInFilters no longer maps hnsw to the undefined HNSWSearchFilter. searchValidation validates the sear…
Out of Scope Changes check ✅ Passed The reviewed changes stay within issue [#9838]. The source changes remove the invalid filter reference and preserve HNSW validation. The input and output schemagen fixtures provide regression coverage…
Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. (2 skipped: 2 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.


for _, fld := range defn.Fields {
// Vector indexes support similarity queries, not ordinary field filters.
if hasEmbeddingDirective(fld) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this guard in place, the "hnsw": "HNSWSearchFilter" entry in builtInFilters (line 543) is unreachable, and it still names a type that has never existed. Worth deleting it in this PR so a future refactor doesn't rewire it and bring the bug back.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partly agreed — thanks. The mapping was not quite unreachable: searchValidation in rules.go also used builtInFilters as the registry of supported @search arguments, so deleting only that entry made every hnsw(...) field fail with "not among supported search types". a7ae10e removes the dead HNSWSearchFilter mapping and validates search arguments against supportedSearches instead (both maps had identical keys apart from hnsw), so HNSW remains a valid search type without an ordinary filter type.

)

// TestVectorGenerateRestrictions keeps vector query generation independent of mutation generation.
func TestVectorGenerateRestrictions(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schema generation coverage in this package is golden-file based: TestSchemaString walks testdata/schemagen/input/ and diffs against testdata/schemagen/output/. A fixture like embedding-directive-with-generate-restrictions.graphql with @generate(mutation: {update: false}) would pin the whole generated schema (not just a handful of substrings) and is where the next person will look for this.

The eight-combination loop is fine as a belt-and-braces check, but as written it breaks package convention in a couple of ways: every other test file here uses testify require rather than t.Fatal, and there's no precedent for a standalone *_regression_test.go file. I'd suggest adding the golden fixture, and either folding this loop into schemagen_test.go with require or dropping it since the fixture covers the failing case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in a7ae10e: added the embedding-directive-with-generate-restrictions.graphql input/output fixtures with @generate(mutation: {update: false}), covered by TestSchemaString, which also re-parses the generated schema. I removed the standalone regression test and confirmed that the fixture fails when the addFilterType guard is removed. go test ./graphql/schema/ passes.

- Remove the dead HNSWSearchFilter mapping and validate HNSW through supportedSearches.
- Replace the standalone regression with a schemagen golden fixture for update:false.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

fix(graphql): Undefined type HNSWSearchFilter when mutation updates are disabled on vector-indexed types

2 participants