aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/fetchers/posts/fetch-posts-list.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-24 20:00:08 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-27 14:47:51 +0100
commitf111685c5886f3e77edfd3621c98d8ac1b9bcce4 (patch)
tree62a541fe3afeb64bf745443706fbfb02e96c5230 /src/services/graphql/fetchers/posts/fetch-posts-list.ts
parentbee515641cb144be9a855ff2cac258d2fedab21d (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/fetch-posts-list.ts')
-rw-r--r--src/services/graphql/fetchers/posts/fetch-posts-list.ts97
1 files changed, 97 insertions, 0 deletions
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;
+};