aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts
blob: 29a3b173baaccf8b568cfe25a3dc44f07b8f2947 (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
43
import type {
  GraphQLPageInfo,
  GraphQLTaxonomyWhere,
  Nullable,
} from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';

type ThematicsCountResponse = {
  thematics: Nullable<{
    pageInfo: Pick<GraphQLPageInfo, 'total'>;
  }>;
};

const thematicsCountQuery = `query ThematicsCount($search: String, $title: String) {
  thematics(where: {search: $search, title: $title}) {
    pageInfo {
      total
    }
  }
}`;

/**
 * Retrieve the total of WordPress thematics.
 *
 * @param {GraphQLTaxonomyWhere} [input] - The input to filter the thematics.
 * @returns {Promise<number>} The total number of thematics.
 */
export const fetchThematicsCount = async (
  input?: GraphQLTaxonomyWhere
): Promise<number> => {
  const response = await fetchGraphQL<ThematicsCountResponse>({
    query: thematicsCountQuery,
    url: getGraphQLUrl(),
    variables: { ...input },
  });

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

  return response.thematics.pageInfo.total;
};