aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts
blob: 80da462e69a8053f24e77d86c6e9dd289d0ecd42 (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
import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';

export type ThematicsSlugsResponse = {
  thematics: Nullable<GraphQLNodes<SlugNode>>;
};

const thematicsSlugsQuery = `query ThematicsSlugs($first: Int) {
  thematics(first: $first) {
    nodes {
      slug
    }
  }
}`;

/**
 * Retrieve the WordPress thematics slugs.
 *
 * @param {number} count - The number of thematics slugs to retrieve.
 * @returns {Promise<string[]>} The thematics slugs.
 */
export const fetchAllThematicsSlugs = async (
  count: number
): Promise<string[]> => {
  const response = await fetchGraphQL<ThematicsSlugsResponse>({
    query: thematicsSlugsQuery,
    url: getGraphQLUrl(),
    variables: { first: count },
  });

  if (!response.thematics)
    return Promise.reject(new Error('Unable to find the thematics slugs.'));

  return response.thematics.nodes.map((node) => node.slug);
};