Я делаю веб-приложение с React, Relay (экспериментальным) и Typescript, и у меня возникают проблемы с повторным использованием компонентов с похожими данными/реквизитами в разных фрагментах.
Допустим, у меня есть следующий запрос Relay и основной компонент приложения:
const query = graphql`
query AppQuery {
currentUser {
...HomePage_currentUser
...ProfilePage_currentUser
}
}
`;
export default function App(): JSX.Element {
const appData = useLazyLoadQuery<AppQuery>(query, {});
return (
<>
<HomePage currentUser={appData.currentUser}>
<ProfilePage currentUser={appData.currentUser}>
</>
);
}
И следующие компоненты страницы:
interface HomePageProps {
currentUser: HomePage_currentUser$key | null;
}
const currentUserFragment = graphql`
fragment HomePage_currentUser on User {
id
profileImageUrl
items {
id
name
}
}
`;
export default function HomePage(props: HomePageProps): JSX.Element {
const currentUser = useFragment(currentUserFragment, props.currentUser);
return (
<>
{/* ... */}
<ProfileIcon user={currentUser} >
{/* ... */}
</>
)
}
interface ProfilePageProps {
currentUser: ProfilePage_currentUser$key | null;
}
const currentUserFragment = graphql`
fragment ProfilePage_currentUser on User {
id
profileImageUrl
lastLoggedInTimestamp
}
`;
export default function ProfilePage(props: ProfilePageProps): JSX.Element {
const currentUser = useFragment(currentUserFragment, props.currentUser);
return (
<>
{/* ... */}
<ProfileIcon user={currentUser} >
{/* ... */}
</>
)
}
И следующий компонент ProfileIcon
interface ProfileIconProps {
currentUser: ???
}
export default function ProfileIcon(props: ProfileIconProps): JSX.Element {
return (
<div>
<img src={props.currentUser.profileImageUrl} />
</div>
)
}
Мой главный вопрос касается типа реквизита currentUser
в компоненте ProfileIcon
. Кажется, что нет чистого способа повторно использовать компонент для типа ProfilePage_currentUser
и типа HomePage_currentUser
, несмотря на то, что запрашиваемые данные невероятно похожи, и они явно совместимы для этого компонента.
Есть ли какой-либо рекомендуемый способ справиться с этим, кроме чего-то подобного?
interface ProfileIconProps {
currentUser: Omit<ProfilePage_currentUser, ' $refType'>
}