aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/fetchers/thematics/fetch-thematic.ts
blob: a9958bc2b0dae998338aa561bcea54dff63b0ab3 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import type { Nullable, WPThematic } from '../../../../types';
import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';

type ThematicResponse = {
  thematic: Nullable<WPThematic>;
};

const thematicQuery = `query Thematic($slug: ID!) {
  thematic(id: $slug, idType: SLUG) {
    acfThematics {
      postsInThematic {
        ... on Post {
          acfPosts {
            postsInTopic {
              ... on Topic {
                databaseId
                slug
                title
              }
            }
          }
          author {
            node {
              name
            }
          }
          commentCount
          contentParts {
            beforeMore
          }
          databaseId
          date
          featuredImage {
            node {
              altText
              mediaDetails {
                height
                width
              }
              sourceUrl
              title
            }
          }
          info {
            wordsCount
          }
          modified
          slug
          title
        }
      }
    }
    contentParts {
      afterMore
      beforeMore
    }
    featuredImage {
      node {
        altText
        mediaDetails {
          height
          width
        }
        sourceUrl
        title
      }
    }
    seo {
      metaDesc
      title
    }
    slug
    title
  }
}`;

/**
 * Retrieve a WordPress thematic by slug.
 *
 * @param {string} slug - The thematic slug.
 * @returns {Promise<WPThematic>} The requested thematic.
 */
export const fetchThematic = async (slug: string): Promise<WPThematic> => {
  const response = await fetchGraphQL<ThematicResponse>({
    query: thematicQuery,
    url: getGraphQLUrl(),
    variables: { slug },
  });

  if (!response.thematic)
    return Promise.reject(
      new Error(`No thematic found for the following slug ${slug}.`)
    );

  return response.thematic;
};