Skip to content

fix: resolve aliases by type condition - #273

Merged
co-sic merged 2 commits into
mainfrom
pk/fix-type-conditioned-aliases
Nov 14, 2026
Merged

co-sic merged 2 commits into
mainfrom
pk/fix-type-conditioned-aliases

Conversation

@co-sic

@co-sic co-sic commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #272, from its review.

Within one selection, fragments on different types can use the same alias for different fields:

media {
  __typename
  ... on Book { date: published }
  ... on Blog { date: publishedDate }
}

The alias map was keyed by response path only, so the last alias won, and a Book result threw Field publishedDate not found. Selecting __typename didn't help, because the alias lookup never saw the concrete type.

Each alias entry now records the type conditions (... on Type, fragment X on Type) that apply at each level of its path. When walking the response, the transformer matches them against the concrete types it already resolves from __typename. Conditions on interfaces and unions match their member types. Entries without conditions match everything.

Without __typename on an interface result, the concrete type is unknown. There, any condition that overlaps with the interface matches, so works { ... on Book { d: date } } still resolves as on main. When aliases from several operations are merged (no operation name), the last matching alias wins, as before.

New tests in test/transform.test.ts cover unions, interfaces, named fragments, aliases nested below a type condition, and conditions on an interface. Two more cover interface results without __typename and alias precedence across operations, so the cases that already worked keep working.

🤖 Generated with Claude Code

Inline fragments and fragment spreads with different type conditions can use the same alias for different fields. The alias map was keyed by path only, so the last alias won and objects of the other type threw. Record the type conditions of each alias per path level and match them against the concrete types resolved from __typename.
For an interface result without __typename the concrete type is unknown, so type conditions that overlap with the interface now match, as aliases did before. With aliases merged across operations the last matching alias wins again, as before.
@co-sic
co-sic merged commit fcdcd29 into main Nov 14, 2026
6 checks passed
@co-sic
co-sic deleted the pk/fix-type-conditioned-aliases branch November 14, 2026 11:03
gastrobot pushed a commit that referenced this pull request Nov 14, 2026
## [2.3.1](v2.3.0...v2.3.1) (2026-11-14)

### Bug Fixes

* resolve aliases by type condition ([#273](#273)) ([fcdcd29](fcdcd29))
@gastrobot

Copy link
Copy Markdown

🎉 This PR is included in version 2.3.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants