diff options
Diffstat (limited to 'src/services/graphql/fetchers/posts/fetch-recent-posts.ts')
| -rw-r--r-- | src/services/graphql/fetchers/posts/fetch-recent-posts.ts | 76 |
1 files changed, 76 insertions, 0 deletions
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; +}; |
