Repository navigation
Merge sub-selections when the same response name appears across query branches #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
| }; | ||
| `); | ||
| }); | ||
|
|
@@ -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 | ||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering where |
||
| } | { | ||
| __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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where did |
||
| __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!) { | ||
|
|
||
There was a problem hiding this comment.
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?