Skip to content

Incorrectly reused type from unrelated operation when using multiple inline fragments #10461

@saranrapjs

Description

@saranrapjs

Which packages are impacted by your issue?

@graphql-codegen/typescript-operations

Describe the bug

I've run into a bug where the typescript-operations plugin will sometimes reuse a type generated from operation A in an unrelated operation B. Depending on the GraphQL schema used for generation, those types may be structurally equivalent (weird, but arguably not a bug) or may reflect distinct types (e.g. can produce type errors for consuming code which counts on the correct types being available between operations).

The clearest situation where this has occurred for me is with a schema like the following, which has a union of types with identical fields:

type MemberA {
  exampleField: String
}
type MemberB {
  exampleField: String
}

# both members of the union have the same field name
union Members = MemberA | MemberB

type Parent {
    parentField: Members
}
type Query {
  parent: Parent
}

...and where multiple operations target these types via inline fragments which are included more than once at a particular type location (whether via non-inline fragments, or in the query, or elsewhere):

# operation A:
        query UnionTestA {
          someParent {
            parentField {
              ... on MemberA {
                field
              }
            }
            parentField {
              ... on MemberA {
                field
              }
            }
          }
        }
# operation B:
        query UnionTestB {
          someParent {
            parentField {
              ... on MemberA {
                field
              }
            }
            parentField {
              ... on MemberB {
                field
              }
            }
          }
        }

...with a schema and query like this, typescript-operations will generate reproduce previously generated types from operation A as part of the types for operation B, which are then incorrect because in operation A the query doesn't select the same fields for all members of the union.

Your Example Website or App

master...saranrapjs:graphql-code-generator:fix-fragment-cache-collision

Steps to Reproduce the Bug or Issue

I've added a failing test to the typescript-operations tests on my fork of the repo that illustrates the bug.

(this fork/branch also has a proposed fix, which narrows where the selectionSet cache can be reused — but the contributing guidelines require filing an issue first, which is what I'm doing here!)

Expected behavior

In this situation, I would expect typescript-operations to not reuse types between operations where they are not structurally equivalent.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • NodeJS: v24.5.0
  • graphql version:
  • @graphql-codegen/* version(s): HEAD

Codegen Config File

This happens when using the extractAllFieldsToTypes: true configuration for the typescript-operations plugin.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions