facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.39k stars 1.82k forks source link

[useFragment] Using two fragments inside component result in unknown or {} type #4505

Open MateusAndrade opened 11 months ago

MateusAndrade commented 11 months ago

useFragment

Hello, while I'm using two useFragment inside my component, the data coming from the second call gets either unknown or {} with Typescript:

E.g:

    const data1 = useFragment(FirstFragment, props.queryRef);

    const person = useFragment(PersonFragment, data1.person);

I'm spreading the PersonFragment inside the FirstFragment, I checked for the types created by relay and Indeed they show the right definition for it, but for some reason, either Typescript/VS-Code isn't able to infer the type here.

mattste commented 4 months ago

Still running into this issue. Has anyone figured out a workaround for this?

tobias-tengler commented 4 months ago

Just pass the $key type as generic argument directly to useFragment.

vinibgoulart commented 4 months ago

what about define the type explicitly?

type Props = {
  queryRef: FirstFragment_query$key
}

useFragment<FirstFragment_query$key>(graphql`
  fragment FirstFragment_query on Query {
    id
  }
`, props.queryRef)