diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-24 20:00:08 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-27 14:47:51 +0100 |
| commit | f111685c5886f3e77edfd3621c98d8ac1b9bcce4 (patch) | |
| tree | 62a541fe3afeb64bf745443706fbfb02e96c5230 /src/services/graphql/api.ts | |
| parent | bee515641cb144be9a855ff2cac258d2fedab21d (diff) | |
refactor(services, types): reorganize GraphQL fetchers and data types
The Typescript mapped types was useful for autocompletion in fetchers
but their are harder to maintain. I think it's better to keep each
query close to its fetcher to have a better understanding of the
fetched data. So I:
* colocate queries with their own fetcher
* colocate mutations with their own mutator
* remove Typescript mapped types for queries and mutations
* move data convertors inside graphql services
* rename most of data types and fetchers
Diffstat (limited to 'src/services/graphql/api.ts')
| -rw-r--r-- | src/services/graphql/api.ts | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/src/services/graphql/api.ts b/src/services/graphql/api.ts deleted file mode 100644 index 003f92d..0000000 --- a/src/services/graphql/api.ts +++ /dev/null @@ -1,83 +0,0 @@ -import type { - Mutations, - MutationsInputMap, - MutationsResponseMap, - Queries, - QueriesInputMap, - QueriesResponseMap, -} from '../../types'; -import { CONFIG } from '../../utils/config'; - -/** - * Retrieve the API url from settings. - * - * @returns {string} The API url. - */ -export const getAPIUrl = (): string => { - const { url } = CONFIG.api; - - if (!url) { - throw new Error('API url is not defined.'); - } - - return url; -}; - -export type ResponseMap<T, K extends Mutations | Queries> = K extends Mutations - ? MutationsResponseMap<T> - : QueriesResponseMap<T>; - -export type InputMap<T extends Mutations | Queries> = T extends Mutations - ? MutationsInputMap - : QueriesInputMap; - -type FetchAPIVariables<T> = T extends Queries - ? QueriesInputMap[T] - : T extends Mutations - ? MutationsInputMap[T] - : never; - -type FetchAPIProps<Q extends Queries | Mutations, V = FetchAPIVariables<Q>> = { - query: Q; - variables?: V; -}; - -type FetchAPIResponse<T, K extends Queries | Mutations> = K extends Queries - ? QueriesResponseMap<T>[K] - : K extends Mutations - ? MutationsResponseMap<T>[K] - : never; - -export const fetchAPI = async <T, K extends Queries | Mutations>({ - query, - variables, -}: FetchAPIProps<K>): Promise<FetchAPIResponse<T, K>> => { - const response = await fetch(getAPIUrl(), { - method: 'POST', - headers: { - 'content-type': 'application/json;charset=UTF-8', - }, - body: JSON.stringify({ - query, - variables, - }), - }); - - type JSONResponse = { - data?: FetchAPIResponse<T, K>; - errors?: { message: string }[]; - }; - - const { data, errors }: JSONResponse = await response.json(); - - if (response.ok) { - if (!data) return Promise.reject(new Error(`No data found"`)); - - return data; - } - console.error('Failed to fetch API'); - const error = new Error( - errors?.map((e) => e.message).join('\n') ?? 'unknown' - ); - return Promise.reject(error); -}; |
