From a0d00743cbbdb77b27c1a3d5711407ffed5befac Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sat, 20 Aug 2022 21:39:51 +0200 Subject: refactor(types): move and rename GraphQL types The api file in services was not really readable. So I move the types and I also rewrite a little the fetch function. I also rename most of the type to avoid conflict with preexisting types (like Node) and to keep consistency. --- src/utils/hooks/use-article.tsx | 6 ++---- src/utils/hooks/use-comments.tsx | 6 ++---- src/utils/hooks/use-pagination.tsx | 7 ++++--- 3 files changed, 8 insertions(+), 11 deletions(-) (limited to 'src/utils/hooks') diff --git a/src/utils/hooks/use-article.tsx b/src/utils/hooks/use-article.tsx index 6281a54..e658407 100644 --- a/src/utils/hooks/use-article.tsx +++ b/src/utils/hooks/use-article.tsx @@ -1,4 +1,4 @@ -import { fetchAPI, getAPIUrl } from '@services/graphql/api'; +import { fetchAPI } from '@services/graphql/api'; import { getArticleFromRawData } from '@services/graphql/articles'; import { articleBySlugQuery } from '@services/graphql/articles.query'; import { Article } from '@ts/types/app'; @@ -22,9 +22,7 @@ const useArticle = ({ fallback, }: UseArticleConfig): Article | undefined => { const { data } = useSWR( - slug - ? { api: getAPIUrl(), query: articleBySlugQuery, variables: { slug } } - : null, + slug ? { query: articleBySlugQuery, variables: { slug } } : null, fetchAPI ); diff --git a/src/utils/hooks/use-comments.tsx b/src/utils/hooks/use-comments.tsx index 9076888..cb0848b 100644 --- a/src/utils/hooks/use-comments.tsx +++ b/src/utils/hooks/use-comments.tsx @@ -1,4 +1,4 @@ -import { fetchAPI, getAPIUrl } from '@services/graphql/api'; +import { fetchAPI } from '@services/graphql/api'; import { buildCommentsTree, getCommentFromRawData, @@ -24,9 +24,7 @@ const useComments = ({ fallback, }: UseCommentsConfig): Comment[] | undefined => { const { data } = useSWR( - contentId - ? { api: getAPIUrl(), query: commentsQuery, variables: { contentId } } - : null, + contentId ? { query: commentsQuery, variables: { contentId } } : null, fetchAPI ); diff --git a/src/utils/hooks/use-pagination.tsx b/src/utils/hooks/use-pagination.tsx index a80a539..f17b6ff 100644 --- a/src/utils/hooks/use-pagination.tsx +++ b/src/utils/hooks/use-pagination.tsx @@ -1,4 +1,5 @@ -import { type EdgesResponse, type EdgesVars } from '@services/graphql/api'; +import { GraphQLEdgesInput } from '@ts/types/graphql/generics'; +import { EdgesResponse, Search } from '@ts/types/graphql/queries'; import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite'; export type UsePaginationProps = { @@ -9,7 +10,7 @@ export type UsePaginationProps = { /** * A function to fetch more data. */ - fetcher: (props: EdgesVars) => Promise>; + fetcher: (props: GraphQLEdgesInput & Search) => Promise>; /** * The number of results per page. */ @@ -74,7 +75,7 @@ const usePagination = ({ const getKey: SWRInfiniteKeyLoader = ( pageIndex: number, previousData: EdgesResponse - ): EdgesVars | null => { + ): (GraphQLEdgesInput & Search) | null => { // Reached the end. if (previousData && !previousData.edges.length) return null; -- cgit v1.2.3