From 06ea295857e508a830669cb402d2156204309b1e Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 13 May 2022 16:28:06 +0200 Subject: chore: add blog page widgets --- src/services/graphql/topics.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create 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 new file mode 100644 index 0000000..0f59bad --- /dev/null +++ b/src/services/graphql/topics.ts @@ -0,0 +1,42 @@ +import { RawTopicPreview, TotalItems } from '@ts/types/raw-data'; +import { EdgesResponse, EdgesVars, fetchAPI, getAPIUrl } from './api'; +import { topicsListQuery, 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({ + api: getAPIUrl(), + query: totalTopicsQuery, + }); + + return response.topics.pageInfo.total; +}; + +/** + * Retrieve the given number of topics from API. + * + * @param {EdgesVars} props - An object of GraphQL variables. + * @returns {Promise>} The topics data. + */ +export const getTopicsPreview = async ( + props: EdgesVars +): Promise> => { + const response = await fetchAPI({ + api: getAPIUrl(), + query: topicsListQuery, + variables: props, + }); + + return response.topics; +}; + +export const getAllTopicsLinks = async () => { + const allTopics = []; + const initialTopics = await getTopicsPreview({ first: 1 }); + + if (!initialTopics.pageInfo.hasNextPage) return initialTopics; +}; -- cgit v1.2.3