Skip to content

Merge sub-selections when the same response name appears across query branches - #101

Merged
iandeboisblanc merged 3 commits into
mainfrom
fix-merge-overlapping-field-selections
Jun 9, 2026
Merged

iandeboisblanc merged 3 commits into
mainfrom
fix-merge-overlapping-field-selections

Conversation

@iandeboisblanc

@iandeboisblanc iandeboisblanc commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

(For context, I ran into this issue while I was doing some new queries for assignments (which heavily use interfaces). Not a blocker for anything, just a quirk i found)

  • Bug fix: when the same field is selected at both the base level and inside an inline fragment (or via two fragment spreads), graphql-flow now merges their sub-selections into a single property — mirroring the GraphQL spec's CollectFields algorithm and what servers actually return. Previously the second occurrence silently clobbered the first, dropping fields from the generated TypeScript.
  • Major version bump: generated .ts output can change for queries that mix inline fragments with base-level selections on an interface. The field-lookup base now follows the concrete impl rather than the interface, so JSDoc descriptions are richer and split-type sub-paths use the field alias (matching how objectPropertiesToFlow has always worked for object types).

Example

Before:

hero {
  friends { id }
  ... on Human {
    friends { name }
  }
}

The Human.friends type was { name } only — id was silently dropped.

After: Human.friends is { id; name } as the server returns.

Implementation

  • objectPropertiesToFlow flattens matching inline fragments and known fragment spreads into the current selection list, then merges Field nodes by response name before generating TS property signatures.
  • unionOrInterfaceToFlow now routes interface-with-fragments queries through objectPropertiesToFlow per concrete impl (with a collectInterfaceSelectionsForPossible helper that picks up matching fragment contents), so the same merge fires across base + fragment branches. Unions and the fragment-free case retain the original per-selection flow — confirmed by a new regression test.

Validation against khan/frontend

Ran the local build against khan/frontend@main (2,199 resolved queries). 33 files differed (~1.5%):

  • Most changes: richer JSDoc descriptions on interface fields (concrete-impl descriptions now flow through). Purely additive.
  • 3 sub-type identifier renames in getNotificationsForUser.ts where queries alias a field (classroomInfo: classroom { ... }). The split-type sub-path now uses the alias, matching object-type behavior. Zero external references to the renamed names — invisible to consumers.
  • No type signatures lost, no fields disappeared, no nullability flips, no narrowed unions.

Test plan

  • All 50 existing tests pass.
  • 4 new regression tests added (object-type merge via inline fragment; object-type merge via fragment spread; interface-with-fragments merge; union-path no-change guard).
  • pnpm typecheck clean.
  • pnpm build clean.
  • Validated against khan/frontend@main — no regressions in 2,199 queries.

🤖 Generated with Claude Code

… branches

When a query selects the same field both at the base level and inside an inline
fragment (or via a fragment spread), graphql-flow now merges their sub-selections
into a single property — matching the GraphQL spec's CollectFields behavior and
what servers actually return. Previously the second occurrence silently clobbered
the first in the generated TypeScript, dropping fields from the type.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented May 12, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1d4078c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@khanacademy/graphql-flow Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@iandeboisblanc
iandeboisblanc requested a review from jeresig May 12, 2026 17:12
@jeresig
jeresig requested review from a team, jaredly and kevinb-khan May 21, 2026 13:01
@jeresig

jeresig commented May 21, 2026

Copy link
Copy Markdown
Member

Deferring to @jaredly & @kevinb-khan on this one!

@jeresig
jeresig removed request for a team and jeresig May 21, 2026 13:03

@kevinb-khan kevinb-khan left a comment

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.

Thanks for spotting this issue and working on a fix for it. The behaviour from the test cases looks good. I'll spend some time tomorrow looking at the implementation.

id: string;
} | null | undefined> | null | undefined;
id: string;
/** The robot's primary function*/primaryFunction: string;

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.

I was wondering where primaryFunction went, but it's hiding here after the comment on the same line. 😅

Comment on lines +700 to +702
friend: {
__typename: "Animal";
} | {

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.

where did __typename: "Animal" come from? Is it just one of the variants for friend?

@jaredly jaredly left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looks right! Thanks for making this better!

`
query SomeQuery {
hero(episode: JEDI) {
id

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can you select __typename here to make the output type clearer?

@iandeboisblanc
iandeboisblanc merged commit b72c93a into main Jun 9, 2026
1 check passed
@iandeboisblanc
iandeboisblanc deleted the fix-merge-overlapping-field-selections branch June 9, 2026 15:06
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.

4 participants