Skip to content
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

relay-compiler generates invalid Typescript for @raw_response_type + combination of conditional and non-conditional fragments #4914

Open
vhfmag opened this issue Feb 19, 2025 · 1 comment · May be fixed by #4915

Comments

@vhfmag
Copy link

vhfmag commented Feb 19, 2025

Given the code below:

export const query = graphql`
    query AppQuery($showEmail: Boolean!) @raw_response_type {
        ...AppFragment
        ...AppConditionalFragment
    }
`;

export const fragment = graphql`
    fragment AppFragment on Query {
        account {
            name
        }
    }
`;

export const conditionalFragment = graphql`
    fragment AppConditionalFragment on Query {
        account @include(if: $showEmail) {
            email
        }
    }
`;

Relay compiler generates the following types (notice the account field is listed twice):

export type AppQuery$rawResponse = {
  readonly account: {
    readonly id: string;
    readonly name: string;
  } | null | undefined;
  readonly account: {
    readonly email: string;
  } | null | undefined;
};

Which typescript interprets by using the first declaration of a given field and ignoring all others, leading to errors like:

Image

This very reproduction can be found at https://github.com/vhfmag/relay-ts-typegen-issue-repro

Instead, relay compiler should generate something like:

export type AppQuery$rawResponse = {
  readonly account: {
    readonly id: string;
    readonly name: string;
    readonly email?: string | null | undefined;
  } | null | undefined;
};
@vhfmag
Copy link
Author

vhfmag commented Feb 20, 2025

Flow has the same issue, only in the opposite direction (it uses the last declaration instead of the first). E.g. (playground link)

export type AppQuery$rawResponse = {|
  +me: ?{|
    +firstName: ?string,
    +id: string,
  |},
  +me?: ?{|
    +lastName: ?string,
  |},
|};

function fn(val: AppQuery$rawResponse) {
  val.me.id; // Cannot get `val.me.id` because property `id` is missing in object type [1]. [prop-missing]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants