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

Shouldn't react-compiler generate query$data type with the " $data" prop too? #4903

Open
yuliswe opened this issue Feb 2, 2025 · 3 comments

Comments

@yuliswe
Copy link

yuliswe commented Feb 2, 2025

I'm confused why the generated types from query is missing the " $data" property. Like this:

export type NewProductReviewScreenInitialQuery$data = {
  readonly product: {
    readonly " $fragmentSpreads": FragmentRefs<"NewProductReviewScreen_ProductFragment">;
  } | null | undefined;
  readonly review: {
    readonly " $fragmentSpreads": FragmentRefs<"NewProductReviewScreen_ReviewFragment">;
  } | null | undefined;
};

The above was generated from a usePreloadedQuery like this

export const newProductReviewScreenInitialQuery = graphql`
  query NewProductReviewScreenInitialQuery(
    $reviewId: UUID!
    $productId: UUID!
  ) {
    product(id: $productId) {
      ...NewProductReviewScreen_ProductFragment
    }
    review(id: $reviewId) {
      ...NewProductReviewScreen_ReviewFragment
    }
  }
`;


  const fragRef = usePreloadedQuery(
    newProductReviewScreenInitialQuery,
    queryRef
  );

So the type for fragRef.product would be

{
    readonly " $fragmentSpreads": FragmentRefs<"NewProductReviewScreen_ProductFragment">;
  }

I think this is wrong.

The @types/react-relay/relay-hooks/useFragment.d.ts contains

export function useFragment<TKey extends KeyType>(
    fragmentInput: GraphQLTaggedNode,
    fragmentRef: TKey,
): KeyTypeData<TKey>;

and @types/react-relay/relay-hooks.helpers.td.ts contains

export type KeyType<TData = unknown> = Readonly<{
    " $data"?: TData | undefined;
    " $fragmentSpreads": FragmentType;
}>;

export type KeyTypeData<TKey extends KeyType<TData>, TData = unknown> = Required<TKey>[" $data"];

This is implying to me that the useFragment requires the fragRef.product to be generated with the corresponding " $data" property. Otherwise, I always have to explicitly provide the ...$key type in order to use useFragment:

  const productProfileData = useFragment<NewProductReviewScreen_ProductFragment$key>(
    graphql`
      fragment NewProductReviewScreen_ProductFragment on Product {
        ofVendor {
          ofUser {
            firstName
          }
        }
      }
    `,
    fragRef.product
  );

Without the <NewProductReviewScreen_ProductFragment$key> explicitly provided, the return value is unknown.

Versions:

@types/[email protected]
@[email protected]
@[email protected]

@captbaritone
Copy link
Contributor

Are you observing that a type param is currently required for useFragment in TypeScript? If so, I believe that is expected, and a current limitation. If you have a concrete proposal for a way we could generate types which would avoid that, I'd be open to hearing it.

One approach is currently being explored in #4884

@yuliswe
Copy link
Author

yuliswe commented Feb 5, 2025

Thanks for the explanation. I thought my configuration was wrong.

@yuliswe yuliswe closed this as completed Feb 5, 2025
@yuliswe
Copy link
Author

yuliswe commented Feb 5, 2025

Hi @captbaritone, currently is there any technical difficulty for simply generating the type as

export type NewProductReviewScreenInitialQuery$data = {
  readonly product: NewProductReviewScreen_ProductFragment$key | null | undefined;
};

instead of the current behavior of generating this:

export type NewProductReviewScreenInitialQuery$data = {
  readonly product: {
    readonly " $fragmentSpreads": FragmentRefs<"NewProductReviewScreen_ProductFragment">;
  } | null | undefined;
};

?

The former seems that it will fit right into the KeyTypeData<TKey> type.

@yuliswe yuliswe reopened this Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants