diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-24 20:00:08 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-27 14:47:51 +0100 |
| commit | f111685c5886f3e77edfd3621c98d8ac1b9bcce4 (patch) | |
| tree | 62a541fe3afeb64bf745443706fbfb02e96c5230 /src/services/graphql/fetchers/posts | |
| parent | bee515641cb144be9a855ff2cac258d2fedab21d (diff) | |
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
Diffstat (limited to 'src/services/graphql/fetchers/posts')
7 files changed, 385 insertions, 0 deletions
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<GraphQLNodes<SlugNode>>; +}; + +const postsSlugsQuery = `query PostsSlugs($first: Int) { + posts(first: $first) { + nodes { + slug + } + } +}`; + +/** + * Retrieve the WordPress posts slugs. + * + * @returns {Promise<string[]>} The posts slugs. + */ +export const fetchAllPostsSlugs = async (): Promise<string[]> => { + const postsCount = await fetchPostsCount(); + const response = await fetchGraphQL<PostsSlugsResponse>({ + 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<GraphQLPageInfo, 'endCursor'>; + }>; +}; + +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<string>} The cursor of the last post. + */ +export const fetchLastPostCursor = async (count: number): Promise<string> => { + const response = await fetchGraphQL<LastPostCursorResponse>({ + 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<WPPost>; +}; + +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<WPPost>} The requested post. + */ +export const fetchPost = async (slug: string): Promise<WPPost> => { + const response = await fetchGraphQL<PostResponse>({ + 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<GraphQLPageInfo, 'total'>; + }>; +}; + +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<number>} The total number of posts. + */ +export const fetchPostsCount = async ( + input?: GraphQLPostWhere +): Promise<number> => { + const response = await fetchGraphQL<PostsCountResponse>({ + 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<GraphQLConnection<WPPostPreview>>; +}; + +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<GraphQLConnection<WPPostPreview>>} The paginated posts. + */ +export const fetchPostsList = async ({ + orderBy, + where, + ...vars +}: FetchPostsListInput): Promise<GraphQLConnection<WPPostPreview>> => { + const response = await fetchGraphQL<PostsListResponse>({ + 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<GraphQLConnection<RecentWPPost>>; +}; + +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<GraphQLConnection<RecentWPPost>>} The recent posts. + */ +export const fetchRecentPosts = async ({ + where, + ...vars +}: FetchRecentPostsInput): Promise<GraphQLConnection<RecentWPPost>> => { + const response = await fetchGraphQL<RecentPostsResponse>({ + 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'; |
