Skip to content

fix(dang): point GraphQL errors at the failing field - #90

Merged
vito merged 1 commit into
fix-nested-selection-argsfrom
improve-graphql-field-errors
Jun 4, 2026
Merged

vito merged 1 commit into
fix-nested-selection-argsfrom
improve-graphql-field-errors

Conversation

@vito

@vito vito commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Stacked on #89 — review/merge that first; GitHub will retarget this to main once it lands.

Problem

When a GraphQL query built from an object selection failed, the error highlighted the whole selection (the receiver's .{) instead of the field the server actually rejected:

non-declaration evaluation failed: Error: ObjectSelection.evalGraphQLSelection: executing GraphQL query: input:1:27: viewer.repositories Requesting 500 records ... exceeds the `first` limit of 100 records.

  --> main.dang:3:8
   3 | viewer.{
              ^

The caret points at viewer.{ (line 3), but the offending field is repositories several lines down.

Fix

GraphQL execution errors carry a machine-readable path (["viewer", "repositories"]). evalGraphQLSelection now:

  1. Extracts that path from the gqlerror.List (graphqlErrorFieldPath).
  2. Walks the selection tree to find the deepest matching field (locateErrorField), skipping leading segments like the receiver.
  3. Attaches that field's source location, so the error highlights the offending field. The internal ObjectSelection.evalGraphQLSelection: prefix also drops away for this case.

When the error carries no path, the previous behaviour is preserved.

Now:

  --> main.dang:5:3
   5 |   repositories(first: 500).{nodes.{name}}
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Tests

  • pkg/dang/graphql_error_test.go — unit tests for path extraction (names, indices, wrapping) and the tree walk (receiver skip, nested descent, deepest-known-field, no match).
  • tests/errors/graphql_field_error.dang + golden — end-to-end error test. Backed by a new alwaysFails field on the test server's User type so a field nested inside a selection can fail server-side. Verified the golden points at the whole selection (with the noisy prefix) before the fix and at alwaysFails after.

go test ./... passes.

When a GraphQL query built from an object selection failed, the error
highlighted the whole selection (the receiver's `.{`) rather than the field
the server actually rejected. For example, a server complaining about
`repositories` pointed at `viewer.{` several lines up.

GraphQL execution errors carry a machine-readable path identifying the
field. Extract that path, walk the selection tree to find the deepest
matching field, and attach its source location so the error highlights the
offending field. When no path is present the previous behaviour is kept.

Add a unit test for the path extraction and tree walk, plus an error test
backed by a new alwaysFails field on the test server's User type so a field
nested inside a selection can fail server-side.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

@vito
vito merged commit deac35b into fix-nested-selection-args Jun 4, 2026
6 checks passed
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

Successfully merging this pull request may close these issues.

1 participant