From f111685c5886f3e77edfd3621c98d8ac1b9bcce4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 24 Nov 2023 20:00:08 +0100 Subject: refactor(services, types): reorganize GraphQL fetchers and data types The Typescript mapped types was useful for autocompletion in fetchers but their are harder to maintain. I think it's better to keep each query close to its fetcher to have a better understanding of the fetched data. So I: * colocate queries with their own fetcher * colocate mutations with their own mutator * remove Typescript mapped types for queries and mutations * move data convertors inside graphql services * rename most of data types and fetchers --- .../graphql/fetchers/comments/fetch-comments.ts | 65 +++++++++++++++ src/services/graphql/fetchers/comments/index.ts | 1 + src/services/graphql/fetchers/index.ts | 4 + .../fetchers/posts/fetch-all-posts-slugs.ts | 34 ++++++++ .../fetchers/posts/fetch-last-post-cursor.ts | 37 +++++++++ src/services/graphql/fetchers/posts/fetch-post.ts | 92 ++++++++++++++++++++ .../graphql/fetchers/posts/fetch-posts-count.ts | 43 ++++++++++ .../graphql/fetchers/posts/fetch-posts-list.ts | 97 ++++++++++++++++++++++ .../graphql/fetchers/posts/fetch-recent-posts.ts | 76 +++++++++++++++++ src/services/graphql/fetchers/posts/index.ts | 6 ++ .../thematics/fetch-all-thematics-slugs.ts | 34 ++++++++ .../graphql/fetchers/thematics/fetch-thematic.ts | 96 +++++++++++++++++++++ .../fetchers/thematics/fetch-thematics-count.ts | 43 ++++++++++ .../fetchers/thematics/fetch-thematics-list.ts | 78 +++++++++++++++++ src/services/graphql/fetchers/thematics/index.ts | 4 + .../fetchers/topics/fetch-all-topics-slugs.ts | 34 ++++++++ .../graphql/fetchers/topics/fetch-topic.ts | 97 ++++++++++++++++++++++ .../graphql/fetchers/topics/fetch-topics-count.ts | 43 ++++++++++ .../graphql/fetchers/topics/fetch-topics-list.ts | 84 +++++++++++++++++++ src/services/graphql/fetchers/topics/index.ts | 4 + 20 files changed, 972 insertions(+) create mode 100644 src/services/graphql/fetchers/comments/fetch-comments.ts create mode 100644 src/services/graphql/fetchers/comments/index.ts create mode 100644 src/services/graphql/fetchers/index.ts create mode 100644 src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts create mode 100644 src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts create mode 100644 src/services/graphql/fetchers/posts/fetch-post.ts create mode 100644 src/services/graphql/fetchers/posts/fetch-posts-count.ts create mode 100644 src/services/graphql/fetchers/posts/fetch-posts-list.ts create mode 100644 src/services/graphql/fetchers/posts/fetch-recent-posts.ts create mode 100644 src/services/graphql/fetchers/posts/index.ts create mode 100644 src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts create mode 100644 src/services/graphql/fetchers/thematics/fetch-thematic.ts create mode 100644 src/services/graphql/fetchers/thematics/fetch-thematics-count.ts create mode 100644 src/services/graphql/fetchers/thematics/fetch-thematics-list.ts create mode 100644 src/services/graphql/fetchers/thematics/index.ts create mode 100644 src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts create mode 100644 src/services/graphql/fetchers/topics/fetch-topic.ts create mode 100644 src/services/graphql/fetchers/topics/fetch-topics-count.ts create mode 100644 src/services/graphql/fetchers/topics/fetch-topics-list.ts create mode 100644 src/services/graphql/fetchers/topics/index.ts (limited to 'src/services/graphql/fetchers') diff --git a/src/services/graphql/fetchers/comments/fetch-comments.ts b/src/services/graphql/fetchers/comments/fetch-comments.ts new file mode 100644 index 0000000..85ae6c1 --- /dev/null +++ b/src/services/graphql/fetchers/comments/fetch-comments.ts @@ -0,0 +1,65 @@ +import type { + GraphQLCommentWhere, + GraphQLEdgesInput, + GraphQLNodes, + Nullable, + WPComment, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type CommentsListResponse = { + comments: Nullable>; +}; + +const commentsListQuery = `query CommentsList($first: Int, $contentId: ID, $contentName: String, $status: String) { + comments( + first: $first + where: {contentId: $contentId, contentName: $contentName, order: ASC, orderby: COMMENT_DATE, status: $status} + ) { + nodes { + approved + author { + node { + avatar { + height + url + width + } + name + url + } + } + content + databaseId + date + parentDatabaseId + status + } + } +}`; + +export type FetchCommentsListInput = Pick & { + where?: GraphQLCommentWhere; +}; + +/** + * Retrieve the comments list. + * + * @param {FetchCommentsListInput} input - The input to retrieve comments. + * @returns {Promise} An array of comments. + */ +export const fetchCommentsList = async ({ + where, + ...vars +}: FetchCommentsListInput): Promise => { + const response = await fetchGraphQL({ + query: commentsListQuery, + url: getGraphQLUrl(), + variables: { ...vars, ...where }, + }); + + if (!response.comments) + return Promise.reject(new Error('No comments found.')); + + return response.comments.nodes; +}; diff --git a/src/services/graphql/fetchers/comments/index.ts b/src/services/graphql/fetchers/comments/index.ts new file mode 100644 index 0000000..6a15970 --- /dev/null +++ b/src/services/graphql/fetchers/comments/index.ts @@ -0,0 +1 @@ +export * from './fetch-comments'; diff --git a/src/services/graphql/fetchers/index.ts b/src/services/graphql/fetchers/index.ts new file mode 100644 index 0000000..f45b1c0 --- /dev/null +++ b/src/services/graphql/fetchers/index.ts @@ -0,0 +1,4 @@ +export * from './comments'; +export * from './posts'; +export * from './thematics'; +export * from './topics'; diff --git a/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts new file mode 100644 index 0000000..28f2bbf --- /dev/null +++ b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts @@ -0,0 +1,34 @@ +import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; +import { fetchPostsCount } from './fetch-posts-count'; + +type PostsSlugsResponse = { + posts: Nullable>; +}; + +const postsSlugsQuery = `query PostsSlugs($first: Int) { + posts(first: $first) { + nodes { + slug + } + } +}`; + +/** + * Retrieve the WordPress posts slugs. + * + * @returns {Promise} The posts slugs. + */ +export const fetchAllPostsSlugs = async (): Promise => { + const postsCount = await fetchPostsCount(); + const response = await fetchGraphQL({ + query: postsSlugsQuery, + url: getGraphQLUrl(), + variables: { first: postsCount }, + }); + + if (!response.posts) + return Promise.reject(new Error('Unable to find the posts slugs.')); + + return response.posts.nodes.map((node) => node.slug); +}; diff --git a/src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts b/src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts new file mode 100644 index 0000000..d5ed174 --- /dev/null +++ b/src/services/graphql/fetchers/posts/fetch-last-post-cursor.ts @@ -0,0 +1,37 @@ +import type { GraphQLPageInfo, Nullable } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type LastPostCursorResponse = { + posts: Nullable<{ + pageInfo: Pick; + }>; +}; + +const lastPostCursorQuery = `query LastPostCursor($first: Int) { + posts(first: $first) { + pageInfo { + endCursor + } + } +}`; + +/** + * Retrieve the cursor of the last post for a given number of posts. + * + * @param {number} count - The number of posts to fetch. + * @returns {Promise} The cursor of the last post. + */ +export const fetchLastPostCursor = async (count: number): Promise => { + const response = await fetchGraphQL({ + url: getGraphQLUrl(), + query: lastPostCursorQuery, + variables: { first: count }, + }); + + if (!response.posts?.pageInfo.endCursor) + return Promise.reject( + new Error('Unable to find the cursor of the last post.') + ); + + return response.posts.pageInfo.endCursor; +}; diff --git a/src/services/graphql/fetchers/posts/fetch-post.ts b/src/services/graphql/fetchers/posts/fetch-post.ts new file mode 100644 index 0000000..53c6bc3 --- /dev/null +++ b/src/services/graphql/fetchers/posts/fetch-post.ts @@ -0,0 +1,92 @@ +import type { Nullable, WPPost } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type PostResponse = { + post: Nullable; +}; + +const postQuery = `query Post($slug: ID!) { + post(id: $slug, idType: SLUG) { + acfPosts { + postsInThematic { + ... on Thematic { + databaseId + slug + title + } + } + postsInTopic { + ... on Topic { + databaseId + featuredImage { + node { + altText + mediaDetails { + height + width + } + sourceUrl + title + } + } + slug + title + } + } + } + author { + node { + name + } + } + commentCount + contentParts { + afterMore + beforeMore + } + databaseId + date + featuredImage { + node { + altText + mediaDetails { + height + width + } + sourceUrl + title + } + } + info { + wordsCount + } + modified + seo { + metaDesc + title + } + slug + title + } +}`; + +/** + * Retrieve a WordPress post by slug. + * + * @param {string} slug - The post slug. + * @returns {Promise} The requested post. + */ +export const fetchPost = async (slug: string): Promise => { + const response = await fetchGraphQL({ + query: postQuery, + url: getGraphQLUrl(), + variables: { slug }, + }); + + if (!response.post) + return Promise.reject( + new Error(`No post found for the following slug ${slug}.`) + ); + + return response.post; +}; diff --git a/src/services/graphql/fetchers/posts/fetch-posts-count.ts b/src/services/graphql/fetchers/posts/fetch-posts-count.ts new file mode 100644 index 0000000..a72af52 --- /dev/null +++ b/src/services/graphql/fetchers/posts/fetch-posts-count.ts @@ -0,0 +1,43 @@ +import type { + GraphQLPageInfo, + GraphQLPostWhere, + Nullable, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type PostsCountResponse = { + posts: Nullable<{ + pageInfo: Pick; + }>; +}; + +const postsCountQuery = `query PostsCount($authorName: String, $search: String, $title: String) { + posts(where: {authorName: $authorName, search: $search, title: $title}) { + pageInfo { + total + } + } +}`; + +/** + * Retrieve the total of WordPress posts. + * + * @param {GraphQLPostWhere} [input] - The input to filter the posts. + * @returns {Promise} The total number of posts. + */ +export const fetchPostsCount = async ( + input?: GraphQLPostWhere +): Promise => { + const response = await fetchGraphQL({ + query: postsCountQuery, + url: getGraphQLUrl(), + variables: { ...input }, + }); + + if (!response.posts) + return Promise.reject( + new Error('Unable to find the total number of posts.') + ); + + return response.posts.pageInfo.total; +}; diff --git a/src/services/graphql/fetchers/posts/fetch-posts-list.ts b/src/services/graphql/fetchers/posts/fetch-posts-list.ts new file mode 100644 index 0000000..452892b --- /dev/null +++ b/src/services/graphql/fetchers/posts/fetch-posts-list.ts @@ -0,0 +1,97 @@ +import type { + GraphQLConnection, + GraphQLEdgesInput, + GraphQLPostOrderBy, + GraphQLPostWhere, + Nullable, + WPPostPreview, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type PostsListResponse = { + posts: Nullable>; +}; + +const postsListQuery = `query PostsList($after: String, $before: String, $first: Int, $last: Int, $authorName: String, $orderby: [PostObjectsConnectionOrderbyInput], $search: String, $title: String) { + posts( + after: $after + before: $before + first: $first + last: $last + where: {authorName: $authorName, orderby: $orderby, search: $search, title: $title} + ) { + edges { + cursor + node { + acfPosts { + postsInThematic { + ... on Thematic { + databaseId + slug + title + } + } + } + commentCount + contentParts { + beforeMore + } + databaseId + date + featuredImage { + node { + altText + mediaDetails { + height + width + } + sourceUrl + title + } + } + info { + wordsCount + } + modified + slug + title + } + } + pageInfo { + endCursor + hasNextPage + total + } + } +}`; + +export type FetchPostsListInput = GraphQLEdgesInput & { + orderBy?: GraphQLPostOrderBy; + where?: GraphQLPostWhere; +}; + +/** + * Retrieve a paginated list of WordPress posts. + * + * @param {FetchPostsListInput} input - The input to retrieve posts. + * @returns {Promise>} The paginated posts. + */ +export const fetchPostsList = async ({ + orderBy, + where, + ...vars +}: FetchPostsListInput): Promise> => { + const response = await fetchGraphQL({ + query: postsListQuery, + url: getGraphQLUrl(), + variables: { + ...vars, + ...where, + orderBy: orderBy ? [orderBy] : undefined, + }, + }); + + if (!response.posts) return Promise.reject(new Error('No posts found.')); + + return response.posts; +}; diff --git a/src/services/graphql/fetchers/posts/fetch-recent-posts.ts b/src/services/graphql/fetchers/posts/fetch-recent-posts.ts new file mode 100644 index 0000000..12785d6 --- /dev/null +++ b/src/services/graphql/fetchers/posts/fetch-recent-posts.ts @@ -0,0 +1,76 @@ +import type { + GraphQLConnection, + GraphQLEdgesInput, + GraphQLPostWhere, + Nullable, + RecentWPPost, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type RecentPostsResponse = { + posts: Nullable>; +}; + +const recentPostsQuery = `query RecentPosts($after: String, $before: String, $first: Int, $last: Int, $authorName: String, $search: String, $title: String) { + posts( + after: $after + before: $before + first: $first + last: $last + where: {authorName: $authorName, search: $search, title: $title, orderby: {field: DATE, order: DESC}} + ) { + edges { + cursor + node { + databaseId + date + featuredImage { + node { + altText + mediaDetails { + height + width + } + sourceUrl + title + } + } + slug + title + } + } + pageInfo { + endCursor + hasNextPage + hasPreviousPage + startCursor + total + } + } +}`; + +export type FetchRecentPostsInput = GraphQLEdgesInput & { + where?: GraphQLPostWhere; +}; + +/** + * Retrieve a paginated list of recent WordPress posts. + * + * @param {FetchRecentPostsInput} input - The input to retrieve recent posts. + * @returns {Promise>} The recent posts. + */ +export const fetchRecentPosts = async ({ + where, + ...vars +}: FetchRecentPostsInput): Promise> => { + const response = await fetchGraphQL({ + query: recentPostsQuery, + url: getGraphQLUrl(), + variables: { ...vars, ...where }, + }); + + if (!response.posts) + return Promise.reject(new Error('No recent posts found.')); + + return response.posts; +}; diff --git a/src/services/graphql/fetchers/posts/index.ts b/src/services/graphql/fetchers/posts/index.ts new file mode 100644 index 0000000..fd725cd --- /dev/null +++ b/src/services/graphql/fetchers/posts/index.ts @@ -0,0 +1,6 @@ +export * from './fetch-all-posts-slugs'; +export * from './fetch-last-post-cursor'; +export * from './fetch-post'; +export * from './fetch-posts-count'; +export * from './fetch-posts-list'; +export * from './fetch-recent-posts'; diff --git a/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts new file mode 100644 index 0000000..739c009 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts @@ -0,0 +1,34 @@ +import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; +import { fetchThematicsCount } from './fetch-thematics-count'; + +type ThematicsSlugsResponse = { + thematics: Nullable>; +}; + +const thematicsSlugsQuery = `query ThematicsSlugs($first: Int) { + thematics(first: $first) { + nodes { + slug + } + } +}`; + +/** + * Retrieve the WordPress thematics slugs. + * + * @returns {Promise} The thematics slugs. + */ +export const fetchAllThematicsSlugs = async (): Promise => { + const thematicsCount = await fetchThematicsCount(); + const response = await fetchGraphQL({ + query: thematicsSlugsQuery, + url: getGraphQLUrl(), + variables: { first: thematicsCount }, + }); + + if (!response.thematics) + return Promise.reject(new Error('Unable to find the thematics slugs.')); + + return response.thematics.nodes.map((node) => node.slug); +}; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematic.ts b/src/services/graphql/fetchers/thematics/fetch-thematic.ts new file mode 100644 index 0000000..a9958bc --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematic.ts @@ -0,0 +1,96 @@ +import type { Nullable, WPThematic } from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type ThematicResponse = { + thematic: Nullable; +}; + +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} The requested thematic. + */ +export const fetchThematic = async (slug: string): Promise => { + const response = await fetchGraphQL({ + 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; +}; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts new file mode 100644 index 0000000..29a3b17 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-count.ts @@ -0,0 +1,43 @@ +import type { + GraphQLPageInfo, + GraphQLTaxonomyWhere, + Nullable, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type ThematicsCountResponse = { + thematics: Nullable<{ + pageInfo: Pick; + }>; +}; + +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} The total number of thematics. + */ +export const fetchThematicsCount = async ( + input?: GraphQLTaxonomyWhere +): Promise => { + const response = await fetchGraphQL({ + 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; +}; diff --git a/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts b/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts new file mode 100644 index 0000000..f4d22c6 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/fetch-thematics-list.ts @@ -0,0 +1,78 @@ +import type { + GraphQLConnection, + GraphQLEdgesInput, + GraphQLTaxonomyOrderBy, + GraphQLTaxonomyWhere, + Nullable, + WPThematicPreview, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type ThematicsListResponse = { + thematics: Nullable>; +}; + +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>} The paginated thematics. + */ +export const fetchThematicsList = async ({ + orderBy, + where, + ...vars +}: FetchThematicsListInput): Promise> => { + const response = await fetchGraphQL({ + 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; +}; diff --git a/src/services/graphql/fetchers/thematics/index.ts b/src/services/graphql/fetchers/thematics/index.ts new file mode 100644 index 0000000..c002793 --- /dev/null +++ b/src/services/graphql/fetchers/thematics/index.ts @@ -0,0 +1,4 @@ +export * from './fetch-all-thematics-slugs'; +export * from './fetch-thematic'; +export * from './fetch-thematics-count'; +export * from './fetch-thematics-list'; 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>; +}; + +const topicsSlugsQuery = `query TopicsSlugs($first: Int) { + topics(first: $first) { + nodes { + slug + } + } +}`; + +/** + * Retrieve the WordPress topics slugs. + * + * @returns {Promise} The topics slugs. + */ +export const fetchAllTopicsSlugs = async (): Promise => { + const topicsCount = await fetchTopicsCount(); + const response = await fetchGraphQL({ + 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); +}; 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; +}; + +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} The requested topic. + */ +export const fetchTopic = async (slug: string): Promise => { + const response = await fetchGraphQL({ + 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; +}; diff --git a/src/services/graphql/fetchers/topics/fetch-topics-count.ts b/src/services/graphql/fetchers/topics/fetch-topics-count.ts new file mode 100644 index 0000000..868b01e --- /dev/null +++ b/src/services/graphql/fetchers/topics/fetch-topics-count.ts @@ -0,0 +1,43 @@ +import type { + GraphQLPageInfo, + GraphQLTaxonomyWhere, + Nullable, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type TopicsCountResponse = { + topics: Nullable<{ + pageInfo: Pick; + }>; +}; + +const topicsCountQuery = `query TopicsCount($search: String, $title: String) { + topics(where: {search: $search, title: $title}) { + pageInfo { + total + } + } +}`; + +/** + * Retrieve the total of WordPress topics. + * + * @param {GraphQLTaxonomyWhere} [input] - The input to filter the topics. + * @returns {Promise} The total number of topics. + */ +export const fetchTopicsCount = async ( + input?: GraphQLTaxonomyWhere +): Promise => { + const response = await fetchGraphQL({ + query: topicsCountQuery, + url: getGraphQLUrl(), + variables: { ...input }, + }); + + if (!response.topics) + return Promise.reject( + new Error('Unable to find the total number of topics.') + ); + + return response.topics.pageInfo.total; +}; diff --git a/src/services/graphql/fetchers/topics/fetch-topics-list.ts b/src/services/graphql/fetchers/topics/fetch-topics-list.ts new file mode 100644 index 0000000..1bc2e38 --- /dev/null +++ b/src/services/graphql/fetchers/topics/fetch-topics-list.ts @@ -0,0 +1,84 @@ +import type { + GraphQLConnection, + GraphQLEdgesInput, + GraphQLTaxonomyOrderBy, + GraphQLTaxonomyWhere, + Nullable, + WPTopicPreview, +} from '../../../../types'; +import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; + +type TopicsListResponse = { + topics: Nullable>; +}; + +const topicsListQuery = `query TopicsList($after: String, $before: String, $first: Int, $last: Int, $orderby: [PostObjectsConnectionOrderbyInput], $search: String, $title: String) { + topics( + 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 + } + slug + title + } + } + slug + title + } + } + pageInfo { + endCursor + hasNextPage + hasPreviousPage + startCursor + total + } + } +}`; + +export type FetchTopicsListInput = GraphQLEdgesInput & { + orderBy?: GraphQLTaxonomyOrderBy; + where?: GraphQLTaxonomyWhere; +}; + +/** + * Retrieve a paginated list of WordPress topics. + * + * @param {FetchTopicsListInput} input - The input to retrieve topics. + * @returns {Promise>} The paginated topics. + */ +export const fetchTopicsList = async ({ + orderBy, + where, + ...vars +}: FetchTopicsListInput): Promise> => { + const response = await fetchGraphQL({ + query: topicsListQuery, + url: getGraphQLUrl(), + variables: { + ...vars, + ...where, + orderBy: orderBy ? [orderBy] : undefined, + }, + }); + + if (!response.topics) return Promise.reject(new Error('No topics found.')); + + return response.topics; +}; diff --git a/src/services/graphql/fetchers/topics/index.ts b/src/services/graphql/fetchers/topics/index.ts new file mode 100644 index 0000000..e381883 --- /dev/null +++ b/src/services/graphql/fetchers/topics/index.ts @@ -0,0 +1,4 @@ +export * from './fetch-all-topics-slugs'; +export * from './fetch-topic'; +export * from './fetch-topics-count'; +export * from './fetch-topics-list'; -- cgit v1.2.3