From f111685c5886f3e77edfd3621c98d8ac1b9bcce4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 24 Nov 2023 20:00:08 +0100 Subject: 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 --- src/services/graphql/articles.ts | 201 --------------------------------------- 1 file changed, 201 deletions(-) delete mode 100644 src/services/graphql/articles.ts (limited to 'src/services/graphql/articles.ts') diff --git a/src/services/graphql/articles.ts b/src/services/graphql/articles.ts deleted file mode 100644 index 82bde41..0000000 --- a/src/services/graphql/articles.ts +++ /dev/null @@ -1,201 +0,0 @@ -import type { - Article, - ArticleCard, - EdgesResponse, - EndCursorResponse, - GraphQLEdgesInput, - GraphQLPageInfo, - RawArticle, - RawArticlePreview, - Slug, - TotalItems, -} from '../../types'; -import { - getAuthorFromRawData, - getImageFromRawData, - getPageLinkFromRawData, - updateContentTree, -} from '../../utils/helpers'; -import { fetchAPI } from './api'; -import { - articleBySlugQuery, - articlesCardQuery, - articlesEndCursorQuery, - articlesQuery, - articlesSlugQuery, - totalArticlesQuery, -} from './articles.query'; - -/** - * Retrieve the total number of articles. - * - * @returns {Promise} - The articles total number. - */ -export const getTotalArticles = async (search?: string): Promise => { - const response = await fetchAPI({ - query: totalArticlesQuery, - variables: { search }, - }); - - return response.posts.pageInfo.total; -}; - -export type GetArticlesReturn = { - articles: Article[]; - pageInfo: GraphQLPageInfo; -}; - -/** - * Convert raw data to an Article object. - * - * @param {RawArticle} data - The page raw data. - * @returns {Article} The page data. - */ -export const getArticleFromRawData = async ( - data: RawArticle -): Promise
=> { - const { - acfPosts, - author, - commentCount, - contentParts, - databaseId, - date, - featuredImage, - info, - modified, - slug, - title, - seo, - } = data; - - return { - content: await updateContentTree(contentParts.afterMore), - id: databaseId, - intro: contentParts.beforeMore, - meta: { - author: author && getAuthorFromRawData(author.node, 'page'), - commentsCount: commentCount ?? 0, - cover: featuredImage?.node - ? getImageFromRawData(featuredImage.node) - : undefined, - dates: { publication: date, update: modified }, - seo: { - description: seo?.metaDesc ?? '', - title: seo?.title ?? '', - }, - thematics: acfPosts.postsInThematic?.map((thematic) => - getPageLinkFromRawData(thematic, 'thematic') - ), - topics: acfPosts.postsInTopic?.map((topic) => - getPageLinkFromRawData(topic, 'topic') - ), - wordsCount: info.wordsCount, - }, - slug, - title, - }; -}; - -/** - * Retrieve the given number of articles from API. - * - * @param {GraphQLEdgesInput} props - An object of GraphQL variables. - * @returns {Promise>} The articles data. - */ -export const getArticles = async ( - props: GraphQLEdgesInput -): Promise> => { - const response = await fetchAPI({ - query: articlesQuery, - variables: { ...props }, - }); - - return response.posts; -}; - -/** - * Convert a raw article preview to an article card. - * - * @param {RawArticlePreview} data - A raw article preview. - * @returns {ArticleCard} An article card. - */ -const getArticleCardFromRawData = (data: RawArticlePreview): ArticleCard => { - const { databaseId, date, featuredImage, slug, title } = data; - - return { - cover: featuredImage ? getImageFromRawData(featuredImage.node) : undefined, - dates: { publication: date }, - id: databaseId, - slug, - title, - }; -}; - -/** - * Retrieve the given number of article cards from API. - * - * @param {GraphQLEdgesInput} obj - An object. - * @param {number} obj.first - The number of articles. - * @returns {Promise} - The article cards data. - */ -export const getArticlesCard = async ({ - first, -}: GraphQLEdgesInput): Promise => { - const response = await fetchAPI({ - query: articlesCardQuery, - variables: { first }, - }); - - return response.posts.nodes.map((node) => getArticleCardFromRawData(node)); -}; - -/** - * Retrieve an Article object by slug. - * - * @param {string} slug - The article slug. - * @returns {Promise
} The requested article. - */ -export const getArticleBySlug = async (slug: string): Promise
=> { - const response = await fetchAPI({ - query: articleBySlugQuery, - variables: { slug }, - }); - - return getArticleFromRawData(response.post); -}; - -/** - * Retrieve all the articles slugs. - * - * @returns {Promise} - An array of articles slugs. - */ -export const getAllArticlesSlugs = async (): Promise => { - const totalArticles = await getTotalArticles(); - const response = await fetchAPI({ - query: articlesSlugQuery, - variables: { first: totalArticles }, - }); - - return response.posts.edges.map((edge) => edge.node.slug); -}; - -/** - * Retrieve the last cursor. - * - * @param {GraphQLEdgesInput} props - An object of GraphQL variables. - * @returns {Promise} - The end cursor. - */ -export const getArticlesEndCursor = async ( - props: GraphQLEdgesInput -): Promise => { - const response = await fetchAPI< - EndCursorResponse, - typeof articlesEndCursorQuery - >({ - query: articlesEndCursorQuery, - variables: { ...props }, - }); - - return response.posts.pageInfo.endCursor; -}; -- cgit v1.2.3