summaryrefslogtreecommitdiffstats
path: root/src/services/graphql/topics.ts
blob: 0f59bad3227550fc7e451a76fa44c6b50c5f5547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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<number>} - The topics total number.
 */
export const getTotalTopics = async (): Promise<number> => {
  const response = await fetchAPI<TotalItems, typeof totalTopicsQuery>({
    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<EdgesResponse<RawTopicPreview>>} The topics data.
 */
export const getTopicsPreview = async (
  props: EdgesVars
): Promise<EdgesResponse<RawTopicPreview>> => {
  const response = await fetchAPI<RawTopicPreview, typeof topicsListQuery>({
    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;
};