Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/merge-overlapping-field-selections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"@khanacademy/graphql-flow": major
---

Merge sub-selections when the same response name is selected through multiple branches (base level + inline fragment, two fragment spreads, etc.) so the generated TypeScript matches what the server actually returns.

**Bug fix.** Previously, a query like

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

generated a `Human.friends` type containing only `name` — the `id` selection from the base level was silently dropped. graphql-flow now mirrors the GraphQL spec's CollectFields behavior and merges the two `friends` selections, producing `{ id; name }`.

**Behavior change (why this is a major bump):** For queries that mix inline fragments with base-level field selections on an interface, the field-lookup base is now the concrete impl rather than the interface itself. In practice this means:

- JSDoc description comments on fields now come from the concrete impl (e.g. `Human.name`'s description shows up where previously only the bare interface field appeared).
- If any of your interfaces declare covariant field overrides (a concrete impl narrowing the interface's field type), generated types for those fields will now reflect the concrete impl's narrower type when accessed through a fragment refinement on that impl. Runtime data was already this shape; the type is just catching up.

If you depend on the exact previous output (e.g. snapshot tests of generated `.ts` files), expect diffs in interface-with-fragment queries. Regenerate types and review.
164 changes: 162 additions & 2 deletions src/__test__/graphql-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe("graphql-flow generation", () => {
} | null | undefined> | null | undefined;
hands: number | null | undefined;
id: string;
name: string | null | undefined;
/** The person's name*/name: string | null | undefined;
} | null | undefined> | null | undefined;
hands: number | null | undefined;
homePlanet: string | null | undefined;
Expand Down Expand Up @@ -402,7 +402,7 @@ describe("graphql-flow generation", () => {
} | {
__typename: "Human";
homePlanet: string | null | undefined;
name: string | null | undefined;
/** The person's name*/name: string | null | undefined;
};
`);
});
Expand Down Expand Up @@ -555,6 +555,166 @@ describe("graphql-flow generation", () => {
`);
});

it("should merge sub-selections when the same field appears at the base level and in a matching inline fragment", () => {
const result = rawQueryToFlowTypes(
`
query SomeQuery {
human(id: "x") {
friends { id }
... on Human {
friends { name }
}
}
}`,
{readOnlyArray: false},
);
expect(result).toMatchInlineSnapshot(`
// SomeQueryType.js
export type SomeQueryType = {
variables: {},
response: {
human: /** A human character*/{
friends: Array<{
id: string;
name: string | null | undefined;
} | null | undefined> | null | undefined;
} | null | undefined;
}
};
`);
});

it("should merge sub-selections when the same field is contributed by a fragment spread and the base level", () => {
const result = rawQueryToFlowTypes(
`
fragment HumanFriendsIds on Human {
friends { id }
}
query SomeQuery {
human(id: "x") {
...HumanFriendsIds
friends { name }
}
}`,
{readOnlyArray: false},
);
expect(result).toMatchInlineSnapshot(`
// HumanFriendsIds.js
export type HumanFriendsIds = {
friends: Array<{
id: string;
} | null | undefined> | null | undefined;
};

// SomeQueryType.js
export type SomeQueryType = {
variables: {},
response: {
human: /** A human character*/{
friends: Array<{
id: string;
name: string | null | undefined;
} | null | undefined> | null | undefined;
} | null | undefined;
}
};
`);
});

it("should merge sub-selections on an interface when the same field appears at the base level and inside an inline fragment on a concrete impl", () => {
const result = rawQueryToFlowTypes(
`
query SomeQuery {
hero(episode: JEDI) {
__typename
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?

friends { id }
... on Human {
friends { name }
homePlanet
}
... on Droid {
primaryFunction
}
}
}`,
{readOnlyArray: false},
);
expect(result).toMatchInlineSnapshot(`
// SomeQueryType.js
export type SomeQueryType = {
variables: {},
response: {
hero: {
__typename: "Droid";
friends: Array<{
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. 😅

} | {
__typename: "Human";
friends: Array<{
id: string;
name: string | null | undefined;
} | null | undefined> | null | undefined;
homePlanet: string | null | undefined;
id: string;
} | null | undefined;
}
};
`);
});

it("should leave unions on the existing per-selection flow (no accidental field merging across variants)", () => {
// Unions don't allow base-level Field selections (other than
// __typename), so the response-name merge in objectPropertiesToFlow
// shouldn't activate here. Each inline fragment's selections must
// stay scoped to its own variant.
const result = rawQueryToFlowTypes(
`
fragment HumanName on Human {
name
}
query SomeQuery {
friend(id: "x") {
__typename
...HumanName
... on Human {
hands
}
... on Droid {
primaryFunction
}
}
}`,
{readOnlyArray: false},
);
expect(result).toMatchInlineSnapshot(`
// HumanName.js
export type HumanName = {
/** The person's name*/name: string | null | undefined;
};

// SomeQueryType.js
export type SomeQueryType = {
variables: {},
response: {
friend: {
__typename: "Animal";
} | {
Comment on lines +703 to +705

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?

__typename: "Droid";
/** The robot's primary function*/primaryFunction: string;
} | {
__typename: "Human";
hands: number | null | undefined;
name: string | null | undefined;
} | null | undefined;
}
};
`);
});

it("should handle a complex input variable", () => {
const result = rawQueryToFlowTypes(
`mutation addCharacter($character: CharacterInput!) {
Expand Down
Loading
Loading