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

[FE] feat: useSuspendedQuery, useSuspendedInfiniteQuery 추가 #443

Merged
merged 17 commits into from
Aug 16, 2023

Conversation

Leejin-Yang
Copy link
Collaborator

Issue

✨ 구현한 기능

  • useSuspendedQuery, useSuspendedInfiniteQuery 추가
  • 현재 쿼리들 전부 전환
  • 컴포넌트 내 if문, 옵셔널 체이닝 삭제

📢 논의하고 싶은 내용

x

🎸 기타

  • 리액트 쿼리를 사용할 때 데이터 타입이 Data | undefined로 잡혀 컴포넌트 안에서 타입 가드를 해야했습니다.

  • 두 훅을 만들어 데이터 타입이 Data로만 잡힐 수 있게 했습니다.

  • 자세한 내용은 코멘트에 담을게요

  • main 브랜치에서 나와서 작업한거 같아 다시 요청 보냅니다...........

⏰ 일정

  • 추정 시간 : 4시간
  • 걸린 시간 : 6시간

Comment on lines +4 to +7
export type UseSuspendedQueryResult<TData> = Omit<
UseQueryResult<TData, never>,
'error' | 'isLoading' | 'isError' | 'isFetching' | 'status' | 'data'
> & { data: TData; status: 'idle' | 'success' };
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suspense나 errorboundary로 위의 로딩이나 에러 상태를 잡기 때문에 반환값에서 다음 값들은 없어도 되어서 제거했습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

원래 data 와 status가 없는 타입인가요..?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존에 존재하는 data와 status의 타입은 TData | undefined, idle | success | loading | error 여러 유니온 타입인데요.
로딩과 에러는 Suspense와 ErrorBoundary가 잡고 있기 때문에 해당 상태들은 없어지고 새로 타입을 지정해줘서 추가해줬어요!

@github-actions
Copy link

github-actions bot commented Aug 14, 2023

Unit Test Results

2 tests   2 ✔️  5s ⏱️
1 suites  0 💤
1 files    0

Results for commit 5ed519e.

♻️ This comment has been updated with latest results.

Comment on lines +15 to +19
export type UseSuspendedQueryResultOnIdle = UseSuspendedQueryResult<undefined> & {
status: 'idle';
isSuccess: false;
isIdle: true;
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리액트 쿼리에서 idle 상태는 쿼리가 실행되지 않거나 준비되지 않은 상태입니다. 데이터 요청을 하지 않아 데이터가 undefined입니다.

Comment on lines +21 to +26
export type UseSuspendedQueryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
> = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'suspense' | 'queryKey' | 'queryFn'>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 쿼리 옵션 인자 타입에서 suspense와 쿼리키, 쿼리함수를 삭제했습니다. suspense는 default true로 들어갈 것이고, 키와 함수는 따로 인자로 받습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

따로 인자로 받아서 타입을 삭제했다는 말이 이해가 안가요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

원래 옵션(useQuery 사용할 때 중괄호로 해서 넣는 부분)에 queryKey와 queryFn을 넣어서 사용했는데, 아래 구현부에서 보면 옵션에서 받는 것이 아닌 첫번째 인자, 두번째 인자로 두 값을 받고 있어서 옵션에서 해당 속성은 삭제했어요

suspense는 true로 고정할 것이기에 뺐습니다!

Comment on lines +28 to +33
export type UseSuspendedQueryOptionWithoutEnabled<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
> = Omit<UseSuspendedQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'enabled'>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enabled 속성은 쿼리가 데이터를 가져올지 말지 결정하는 속성인데, false로 하면 데이터를 가져오지 않고 idle 상태가 됩니다.

Comment on lines +35 to +44
export function useSuspendedQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
>(
queryKey: TQueryKey,
queryFn: QueryFunction<TQueryFnData, TQueryKey>,
options?: UseSuspendedQueryOptionWithoutEnabled<TQueryFnData, TError, TData, TQueryKey>
): UseSuspendedQueryResultOnSuccess<TData>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인자로 받는 옵션이 없다면 성공, data 타입은 TData

Comment on lines +46 to +55
export function useSuspendedQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
>(
queryKey: TQueryKey,
queryFn: QueryFunction<TQueryFnData, TQueryKey>,
options: UseSuspendedQueryOptionWithoutEnabled<TQueryFnData, TError, TData, TQueryKey> & { enabled?: true }
): UseSuspendedQueryResultOnSuccess<TData>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

옵션이 있고 enabled가 없거나 true이면 성공, data 타입은 TData

Comment on lines +57 to +66
export function useSuspendedQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
>(
queryKey: TQueryKey,
queryFn: QueryFunction<TQueryFnData, TQueryKey>,
options: UseSuspendedQueryOptionWithoutEnabled<TQueryFnData, TError, TData, TQueryKey> & { enabled: false }
): UseSuspendedQueryResultOnIdle;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enabled 속성이 false면 idle, data 타입은 undefined

Comment on lines +79 to +95
export function useSuspendedQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
>(
queryKey: TQueryKey,
queryFn: QueryFunction<TQueryFnData, TQueryKey>,
options?: UseSuspendedQueryOptions<TQueryFnData, TError, TData, TQueryKey>
) {
return useQuery({
queryKey,
queryFn,
suspense: true,
...options,
}) as UseSuspendedQueryResult<TData>;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수 구현부에요.

첫번째 인자로 쿼리키, 두번째 인자로 쿼리 함수, 세번째 인자로 옵션을 받아요. suspense는 true로 두고 enabled에 따라 반환하는 data 타입이 다르게 됩니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위랑 아래랑 차이점이 무엇인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위랑 아래가 어떤것을 말하는건가요.??

Comment on lines +10 to +13
export type UseSuspendedInfiniteQueryResult<TData = undefined> = Omit<
UseInfiniteQueryResult<TData, never>,
'error' | 'isLoading' | 'isError' | 'isFetching' | 'status' | 'data'
>;
Copy link
Collaborator Author

@Leejin-Yang Leejin-Yang Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useSuspendedQuery와 비슷하지만 반환값이 다르다는 차이가 있습니다

Comment on lines +15 to +20
export type UseSuspendedInfiniteQueryResultOnSuccess<TData> = UseSuspendedInfiniteQueryResult<TData> & {
data: InfiniteData<TData>;
status: 'success';
isSuccess: true;
isIdle: false;
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

데이터의 타입도 InfiniteData로 suspended query와 달라요.

나머지 아래 내용은 동일!

@Leejin-Yang
Copy link
Collaborator Author

Copy link
Collaborator

@hae-on hae-on left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 쩌네요,,,
토스 가자

@@ -102,6 +102,7 @@ module.exports = {
'import/no-unresolved': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/ban-types': 'off',
'import/export': 'off',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얘는 뭐하는 친구죠??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수 오버로딩을 사용하려면 같은 이름의 함수를 여러개 선언해야하는데 린트 에러가 나서 껐습니다 ㅎㅎ...

@@ -4,18 +4,18 @@ import styled from 'styled-components';
import PreviewImage from '@/assets/characters.svg';

interface ProductOverviewItemProps {
name: string;
image: string | null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

억지로 구겨넣은 '?'가 다 사라졌군요 👍

Copy link
Collaborator

@xodms0309 xodms0309 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨습니다. 사실 UseSuspendedQuery는 이해가 잘 안가네요 나중에 설명해주시면 좋을 것 같습니다.

TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거는 무슨 뜻인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 useQuery가 받는 타입입니다!

export function useQuery<
  TQueryFnData = unknown,
  TError = unknown,
  TData = TQueryFnData,
  TQueryKey extends QueryKey = QueryKey,
>(
  options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
): UseQueryResult<TData, TError>

useQuery 사용할 때 제네릭 첫번째 인자로 데이터 반환 타입을 넣는 그 것입니다.

Comment on lines +4 to +7
export type UseSuspendedQueryResult<TData> = Omit<
UseQueryResult<TData, never>,
'error' | 'isLoading' | 'isError' | 'isFetching' | 'status' | 'data'
> & { data: TData; status: 'idle' | 'success' };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

원래 data 와 status가 없는 타입인가요..?

Comment on lines +21 to +26
export type UseSuspendedQueryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
> = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'suspense' | 'queryKey' | 'queryFn'>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

따로 인자로 받아서 타입을 삭제했다는 말이 이해가 안가요

Comment on lines +79 to +95
export function useSuspendedQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
>(
queryKey: TQueryKey,
queryFn: QueryFunction<TQueryFnData, TQueryKey>,
options?: UseSuspendedQueryOptions<TQueryFnData, TError, TData, TQueryKey>
) {
return useQuery({
queryKey,
queryFn,
suspense: true,
...options,
}) as UseSuspendedQueryResult<TData>;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위랑 아래랑 차이점이 무엇인가요?

@@ -36,7 +36,8 @@ export const reviewHandlers = [

return res(
ctx.status(200),
ctx.json({ page: sortedReviews.page, reviews: sortedReviews.reviews.slice(page * 5, (page + 1) * 5) })
ctx.json({ page: sortedReviews.page, reviews: sortedReviews.reviews }),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기는 왜 바뀐건가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

페이지가 오르고 슬라이스 해주니까 데이터가 몇개 없어서 빈배열이 오더라구요. 그래서 화면에 나타나지 않았습니다
그래서 무한스크롤을 할 때 리스트 여러개 오는지 확인하려고 뒷 부분 삭제했습니다

@Leejin-Yang Leejin-Yang merged commit fdebc6d into develop Aug 16, 2023
3 checks passed
@Leejin-Yang Leejin-Yang deleted the feat/issue-431-1 branch August 16, 2023 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FE] useSuspendedQuery, useSuspendedInfiniteQuery 작성
3 participants