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/topics.ts | 154 ----------------------------------------- 1 file changed, 154 deletions(-) delete mode 100644 src/services/graphql/topics.ts (limited to 'src/services/graphql/topics.ts') diff --git a/src/services/graphql/topics.ts b/src/services/graphql/topics.ts deleted file mode 100644 index d8a9b6a..0000000 --- a/src/services/graphql/topics.ts +++ /dev/null @@ -1,154 +0,0 @@ -import type { - EdgesResponse, - GraphQLEdgesInput, - PageLink, - RawArticle, - RawTopic, - RawTopicPreview, - Slug, - Topic, - TotalItems, -} from '../../types'; -import { - getImageFromRawData, - getPageLinkFromRawData, - sortPageLinksByName, -} from '../../utils/helpers'; -import { fetchAPI } from './api'; -import { getArticleFromRawData } from './articles'; -import { - topicBySlugQuery, - topicsListQuery, - topicsSlugQuery, - totalTopicsQuery, -} from './topics.query'; - -/** - * Retrieve the total number of topics. - * - * @returns {Promise} - The topics total number. - */ -export const getTotalTopics = async (): Promise => { - const response = await fetchAPI({ - query: totalTopicsQuery, - }); - - return response.topics.pageInfo.total; -}; - -/** - * Retrieve the given number of topics from API. - * - * @param {GraphQLEdgesInput} props - An object of GraphQL variables. - * @returns {Promise>} The topics data. - */ -export const getTopicsPreview = async ( - props: GraphQLEdgesInput -): Promise> => { - const response = await fetchAPI({ - query: topicsListQuery, - variables: props, - }); - - return response.topics; -}; - -/** - * Convert raw data to a Topic object. - * - * @param {RawTopic} data - The page raw data. - * @returns {Topic} The page data. - */ -export const getTopicFromRawData = async (data: RawTopic): Promise => { - const { - acfTopics, - contentParts, - databaseId, - date, - featuredImage, - info, - modified, - slug, - title, - seo, - } = data; - - /** - * Retrieve an array of related topics. - * - * @param posts - The topic posts. - * @returns {PageLink[]} An array of topics links. - */ - const getRelatedThematics = (posts: RawArticle[]): PageLink[] => { - const thematics: PageLink[] = []; - - posts.forEach((post) => { - if (post.acfPosts.postsInThematic) { - for (const thematic of post.acfPosts.postsInThematic) { - thematics.push(getPageLinkFromRawData(thematic, 'thematic')); - } - } - }); - - const thematicsIds = thematics.map((thematic) => thematic.id); - const uniqueThematics = thematics.filter( - ({ id }, index) => !thematicsIds.includes(id, index + 1) - ); - - return uniqueThematics.sort(sortPageLinksByName); - }; - - return { - content: contentParts.afterMore, - id: databaseId, - intro: contentParts.beforeMore, - meta: { - articles: await Promise.all( - acfTopics.postsInTopic.map(async (post) => getArticleFromRawData(post)) - ), - cover: featuredImage?.node - ? getImageFromRawData(featuredImage.node) - : undefined, - dates: { publication: date, update: modified }, - website: acfTopics.officialWebsite, - seo: { - description: seo?.metaDesc ?? '', - title: seo?.title ?? '', - }, - thematics: getRelatedThematics(acfTopics.postsInTopic), - wordsCount: info.wordsCount, - }, - slug, - title, - }; -}; - -/** - * Retrieve a Topic object by slug. - * - * @param {string} slug - The topic slug. - * @returns {Promise
} The requested topic. - */ -export const getTopicBySlug = async (slug: string): Promise => { - const response = await fetchAPI({ - query: topicBySlugQuery, - variables: { slug }, - }); - - return getTopicFromRawData(response.topic); -}; - -/** - * Retrieve all the topics slugs. - * - * @returns {Promise} - An array of topics slugs. - */ -export const getAllTopicsSlugs = async (): Promise => { - const totalTopics = await getTotalTopics(); - const response = await fetchAPI({ - query: topicsSlugQuery, - variables: { first: totalTopics }, - }); - - return response.topics.edges.map((edge) => edge.node.slug); -}; -- cgit v1.2.3