aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts')
-rw-r--r--src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts
new file mode 100644
index 0000000..eab4a7c
--- /dev/null
+++ b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts
@@ -0,0 +1,34 @@
+import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types';
+import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers';
+import { fetchTopicsCount } from './fetch-topics-count';
+
+type TopicsSlugsResponse = {
+ topics: Nullable<GraphQLNodes<SlugNode>>;
+};
+
+const topicsSlugsQuery = `query TopicsSlugs($first: Int) {
+ topics(first: $first) {
+ nodes {
+ slug
+ }
+ }
+}`;
+
+/**
+ * Retrieve the WordPress topics slugs.
+ *
+ * @returns {Promise<string[]>} The topics slugs.
+ */
+export const fetchAllTopicsSlugs = async (): Promise<string[]> => {
+ const topicsCount = await fetchTopicsCount();
+ const response = await fetchGraphQL<TopicsSlugsResponse>({
+ query: topicsSlugsQuery,
+ url: getGraphQLUrl(),
+ variables: { first: topicsCount },
+ });
+
+ if (!response.topics)
+ return Promise.reject(new Error('Unable to find the topics slugs.'));
+
+ return response.topics.nodes.map((node) => node.slug);
+};