diff options
Diffstat (limited to 'src/services/graphql/fetchers/topics/fetch-topic.ts')
| -rw-r--r-- | src/services/graphql/fetchers/topics/fetch-topic.ts | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/services/graphql/fetchers/topics/fetch-topic.ts b/src/services/graphql/fetchers/topics/fetch-topic.ts new file mode 100644 index 0000000..efc1d9e --- /dev/null +++ b/src/services/graphql/fetchers/topics/fetch-topic.ts @@ -0,0 +1,97 @@ +import type { Nullable, WPTopic } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type TopicResponse = { + topic: Nullable<WPTopic>; +}; + +const topicQuery = `query Topic($slug: ID!) { + topic(id: $slug, idType: SLUG) { + acfTopics { + officialWebsite + postsInTopic { + ... on Post { + acfPosts { + postsInThematic { + ... on Thematic { + 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 topic by slug. + * + * @param {string} slug - The topic slug. + * @returns {Promise<WPTopic>} The requested topic. + */ +export const fetchTopic = async (slug: string): Promise<WPTopic> => { + const response = await fetchGraphQL<TopicResponse>({ + query: topicQuery, + url: getGraphQLUrl(), + variables: { slug }, + }); + + if (!response.topic) + return Promise.reject( + new Error(`No topic found for the following slug ${slug}.`) + ); + + return response.topic; +}; |
