aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts
blob: f4d22c6126e83c90007963223da147ebda37dc2e (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type {
  GraphQLConnection,
  GraphQLEdgesInput,
  GraphQLTaxonomyOrderBy,
  GraphQLTaxonomyWhere,
  Nullable,
  WPThematicPreview,
} from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';

type ThematicsListResponse = {
  thematics: Nullable<GraphQLConnection<WPThematicPreview>>;
};

const thematicsListQuery = `query ThematicsList($after: String, $before: String, $first: Int, $last: Int, $orderby: [PostObjectsConnectionOrderbyInput], $search: String, $title: String) {
  thematics(
    after: $after
    before: $before
    first: $first
    last: $last
    where: {orderby: $orderby, search: $search, title: $title}
  ) {
    edges {
      cursor
      node {
        contentParts {
          beforeMore
        }
        databaseId
        featuredImage {
          node {
            altText
            mediaDetails {
              height
              width
            }
            sourceUrl
            title
          }
        }
        slug
        title
      }
    }
  }
}`;

export type FetchThematicsListInput = GraphQLEdgesInput & {
  orderBy?: GraphQLTaxonomyOrderBy;
  where?: GraphQLTaxonomyWhere;
};

/**
 * Retrieve a paginated list of WordPress thematics.
 *
 * @param {FetchThematicsListInput} input - The input to retrieve thematics.
 * @returns {Promise<GraphQLConnection<WPThematicPreview>>} The paginated thematics.
 */
export const fetchThematicsList = async ({
  orderBy,
  where,
  ...vars
}: FetchThematicsListInput): Promise<GraphQLConnection<WPThematicPreview>> => {
  const response = await fetchGraphQL<ThematicsListResponse>({
    query: thematicsListQuery,
    url: getGraphQLUrl(),
    variables: {
      ...vars,
      ...where,
      orderBy: orderBy ? [orderBy] : undefined,
    },
  });

  if (!response.thematics)
    return Promise.reject(new Error('No thematics found.'));

  return response.thematics;
};