-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: QueryKey Factory를 통한 QueryKey 구조화 (#139)
* feat: implements createQueryOptions createQueryOptions 구현체 createQueryOptions 테스트 * refactor: lotus feature query 리팩터링 * refactor: lotusQueryOptions query 파일로 이동 * refactor: history feature query 리팩터링 * refactor: user feature query 리팩터링 * refactor: refactor LotusPageNation, LotusCardList 컴포넌트 도메인의 상관없이 같은 반환값을 가지는 컴포넌트를 통해 UI를 동일하게 사용하고 Query Options을 주입받아 사용하도록 변경
- Loading branch information
Showing
31 changed files
with
221 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
apps/frontend/src/shared/createQueryOptions/createQueryOptions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { createQueryOptions } from './createQueryOptions'; | ||
import { Fn } from './type'; | ||
|
||
const API = { | ||
list: () => Promise.resolve([]), | ||
get: ({ id }: { id: number }) => Promise.resolve({ id }), | ||
search: ({ keyword }: { keyword: string }) => Promise.resolve({ keyword }), | ||
hello: (name: string) => Promise.resolve({ name }) | ||
}; | ||
|
||
const getQueryOptions = (scope: string, api: Record<string, Fn>) => { | ||
const query = createQueryOptions(scope, api); | ||
|
||
return { scope, query }; | ||
}; | ||
|
||
describe('createQueryOptions', () => { | ||
it('query.all()은 스코프의 쿼리키를 반환한다.', () => { | ||
// Given | ||
const { scope, query } = getQueryOptions('post', API); | ||
|
||
// When | ||
const result = query.all(); | ||
|
||
// Then | ||
expect(result).toEqual({ queryKey: [{ scope }] }); | ||
}); | ||
|
||
it('query.type()은 타입의 쿼리키를 반환한다.', () => { | ||
// Given | ||
const { scope, query } = getQueryOptions('post', API); | ||
const type = 'list'; | ||
|
||
// When | ||
const result = query.type(type); | ||
|
||
// Then | ||
expect(result.queryKey).toEqual([{ scope, type }]); | ||
}); | ||
|
||
it.each([ | ||
['list', { id: 1 }, [{ scope: 'post', type: 'list', id: 1 }]], | ||
['get', { id: 1 }, [{ scope: 'post', type: 'get', id: 1 }]], | ||
['search', { keyword: 'hello' }, [{ scope: 'post', type: 'search', keyword: 'hello' }]] | ||
])('query.%s()은 %p를 인자로 받아 각각 쿼리키를 반환한다.', (type, data, expectedKey) => { | ||
// Given | ||
const { query } = getQueryOptions('post', API); | ||
|
||
// When | ||
const result = query[type](data); | ||
const { queryKey } = result; | ||
|
||
// Then | ||
expect(queryKey).toEqual(expectedKey); | ||
}); | ||
|
||
it("API 파라미터가 object 리터럴이 아닌 경우, 'data' 키로 감싼다.", () => { | ||
// Given | ||
const { query } = getQueryOptions('post', API); | ||
|
||
// When | ||
const result = query.hello('world'); | ||
|
||
// Then | ||
expect(result.queryKey).toEqual([{ scope: 'post', type: 'hello', data: 'world' }]); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
apps/frontend/src/shared/createQueryOptions/createQueryOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CreateQueryOptions, Fn } from './type'; | ||
|
||
const isLiteralObject = (data: unknown): data is Record<string, unknown> => data !== null && typeof data === 'object'; | ||
|
||
export function createQueryOptions<Actions extends Record<string, Fn>, Scope extends string>( | ||
scope: Scope, | ||
actions: Actions | ||
) { | ||
const options: CreateQueryOptions<Actions, Scope> = Object.fromEntries( | ||
Object.entries(actions).map(([type, action]) => { | ||
const handler = (data: unknown) => ({ | ||
queryKey: [{ scope, type, ...(isLiteralObject(data) ? data : data !== undefined ? { data } : {}) }], | ||
queryFn: async () => action(data) | ||
}); | ||
|
||
return [type, handler]; | ||
}) | ||
) as CreateQueryOptions<Actions, Scope>; | ||
|
||
return { | ||
all: () => ({ queryKey: [{ scope }] }), | ||
type: (typeKey: keyof Actions) => ({ queryKey: [{ scope, type: typeKey }] }), | ||
...options | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './createQueryOptions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type CreateQueryOptions<Actions extends Record<string, (arg: any) => unknown>, Scope extends string> = { | ||
[K in keyof Actions]: (data: Parameters<Actions[K]>[0] extends undefined ? void : Parameters<Actions[K]>[0]) => { | ||
queryKey: [ | ||
{ scope: Scope; type: K } & (Parameters<Actions[K]>[0] extends undefined | ||
? Record<string, never> | ||
: Parameters<Actions[K]>[0]) | ||
]; | ||
queryFn: () => ReturnType<Actions[K]>; | ||
}; | ||
}; | ||
|
||
export type Fn = (data: any) => unknown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.