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

export type PostsSlugsResponse = {
  posts: Nullable<GraphQLNodes<SlugNode>>;
};

const postsSlugsQuery = `query PostsSlugs($first: Int) {
  posts(first: $first) {
    nodes {
      slug
    }
  }
}`;

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

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

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