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/services/graphql/articles.ts | 42 ++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 25 deletions(-) (limited to 'src/services/graphql/articles.ts') diff --git a/src/services/graphql/articles.ts b/src/services/graphql/articles.ts index 27406ac..1a7b2e0 100644 --- a/src/services/graphql/articles.ts +++ b/src/services/graphql/articles.ts @@ -1,4 +1,6 @@ -import { Slug, type Article, type ArticleCard } from '@ts/types/app'; +import { type Article, type ArticleCard, type Slug } from '@ts/types/app'; +import { GraphQLEdgesInput, GraphQLPageInfo } from '@ts/types/graphql/generics'; +import { EdgesResponse, EndCursorResponse } from '@ts/types/graphql/queries'; import { type RawArticle, type RawArticlePreview, @@ -7,18 +9,11 @@ import { import { getAuthorFromRawData } from '@utils/helpers/author'; import { getImageFromRawData } from '@utils/helpers/images'; import { getPageLinkFromRawData } from '@utils/helpers/pages'; -import { - EdgesResponse, - EdgesVars, - EndCursor, - fetchAPI, - getAPIUrl, - PageInfo, -} from './api'; +import { fetchAPI } from './api'; import { articleBySlugQuery, articlesCardQuery, - articlesEndCursor, + articlesEndCursorQuery, articlesQuery, articlesSlugQuery, totalArticlesQuery, @@ -31,7 +26,6 @@ import { */ export const getTotalArticles = async (search?: string): Promise => { const response = await fetchAPI({ - api: getAPIUrl(), query: totalArticlesQuery, variables: { search }, }); @@ -41,7 +35,7 @@ export const getTotalArticles = async (search?: string): Promise => { export type GetArticlesReturn = { articles: Article[]; - pageInfo: PageInfo; + pageInfo: GraphQLPageInfo; }; /** @@ -97,14 +91,13 @@ export const getArticleFromRawData = (data: RawArticle): Article => { /** * Retrieve the given number of articles from API. * - * @param {EdgesVars} props - An object of GraphQL variables. + * @param {GraphQLEdgesInput} props - An object of GraphQL variables. * @returns {Promise>} The articles data. */ export const getArticles = async ( - props: EdgesVars + props: GraphQLEdgesInput ): Promise> => { const response = await fetchAPI({ - api: getAPIUrl(), query: articlesQuery, variables: { ...props }, }); @@ -133,15 +126,14 @@ const getArticleCardFromRawData = (data: RawArticlePreview): ArticleCard => { /** * Retrieve the given number of article cards from API. * - * @param {EdgesVars} obj - An object. + * @param {GraphQLEdgesInput} obj - An object. * @param {number} obj.first - The number of articles. * @returns {Promise} - The article cards data. */ export const getArticlesCard = async ({ first, -}: EdgesVars): Promise => { +}: GraphQLEdgesInput): Promise => { const response = await fetchAPI({ - api: getAPIUrl(), query: articlesCardQuery, variables: { first }, }); @@ -157,7 +149,6 @@ export const getArticlesCard = async ({ */ export const getArticleBySlug = async (slug: string): Promise
=> { const response = await fetchAPI({ - api: getAPIUrl(), query: articleBySlugQuery, variables: { slug }, }); @@ -173,7 +164,6 @@ export const getArticleBySlug = async (slug: string): Promise
=> { export const getAllArticlesSlugs = async (): Promise => { const totalArticles = await getTotalArticles(); const response = await fetchAPI({ - api: getAPIUrl(), query: articlesSlugQuery, variables: { first: totalArticles }, }); @@ -184,15 +174,17 @@ export const getAllArticlesSlugs = async (): Promise => { /** * Retrieve the last cursor. * - * @param {EdgesVars} props - An object of GraphQL variables. + * @param {GraphQLEdgesInput} props - An object of GraphQL variables. * @returns {Promise} - The end cursor. */ export const getArticlesEndCursor = async ( - props: EdgesVars + props: GraphQLEdgesInput ): Promise => { - const response = await fetchAPI({ - api: getAPIUrl(), - query: articlesEndCursor, + const response = await fetchAPI< + EndCursorResponse, + typeof articlesEndCursorQuery + >({ + query: articlesEndCursorQuery, variables: { ...props }, }); -- cgit v1.2.3