diff options
| author | Armand Philippot <git@armandphilippot.com> | 2021-12-20 00:15:20 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2021-12-20 00:15:20 +0100 |
| commit | fa6adedc42e9c6ec39cc30df16b54900c220b094 (patch) | |
| tree | 6bb498beadaa382245cecb86ce56931580313c6f /src/services/graphql/blog.ts | |
| parent | 2ff898626c5c0abc6b8195224067b992403e313b (diff) | |
refactor: rewrite types and services
I was repeating myself a lot in services. So I rewrited the different
functions to improve readability and I extracted some formatting
functions to put them in utils. I also rewrited/reorganized some types
to keep consistent names.
Diffstat (limited to 'src/services/graphql/blog.ts')
| -rw-r--r-- | src/services/graphql/blog.ts | 158 |
1 files changed, 0 insertions, 158 deletions
diff --git a/src/services/graphql/blog.ts b/src/services/graphql/blog.ts deleted file mode 100644 index 27b972b..0000000 --- a/src/services/graphql/blog.ts +++ /dev/null @@ -1,158 +0,0 @@ -import { ArticlePreview } from '@ts/types/articles'; -import { - AllPostsSlugResponse, - FetchAllPostsSlugReturn, - FetchPostsListReturn, - GetPostsListReturn, - PostsListResponse, -} from '@ts/types/blog'; -import { gql } from 'graphql-request'; -import { getGraphQLClient } from './client'; - -export const fetchPublishedPosts: FetchPostsListReturn = async ( - first = 10, - after = '' -) => { - const client = getGraphQLClient(); - const query = gql` - query AllPublishedPosts($first: Int, $after: String) { - posts( - after: $after - first: $first - where: { status: PUBLISH, orderby: { field: DATE, order: DESC } } - ) { - edges { - cursor - node { - acfPosts { - postsInSubject { - ... on Subject { - databaseId - featuredImage { - node { - altText - sourceUrl - title - } - } - id - slug - title - } - } - postsInThematic { - ... on Thematic { - databaseId - id - slug - title - } - } - } - commentCount - contentParts { - beforeMore - } - date - featuredImage { - node { - altText - sourceUrl - title - } - } - id - databaseId - modified - slug - title - } - } - pageInfo { - endCursor - hasNextPage - } - } - } - `; - - const variables = { first, after }; - - try { - const response: PostsListResponse = await client.request(query, variables); - return response; - } catch (error) { - console.error(JSON.stringify(error, undefined, 2)); - process.exit(1); - } -}; - -export const getPublishedPosts: GetPostsListReturn = async ({ - first = 10, - after = '', -}) => { - const rawPostsList = await fetchPublishedPosts(first, after); - const postsList: ArticlePreview[] = rawPostsList.posts.edges.map((post) => { - const { - acfPosts, - commentCount, - contentParts, - databaseId, - date, - featuredImage, - id, - modified, - slug, - title, - } = post.node; - const content = contentParts.beforeMore; - const cover = featuredImage ? featuredImage.node : null; - const dates = { publication: date, update: modified }; - const subjects = - acfPosts.postsInSubject && acfPosts.postsInSubject?.length > 0 - ? acfPosts.postsInSubject - : []; - const thematics = - acfPosts.postsInThematic && acfPosts.postsInThematic?.length > 0 - ? acfPosts.postsInThematic - : []; - - return { - commentCount, - content, - databaseId, - date: dates, - featuredImage: cover, - id, - slug, - subjects, - thematics, - title, - }; - }); - - return { posts: postsList, pageInfo: rawPostsList.posts.pageInfo }; -}; - -export const fetchAllPostsSlug: FetchAllPostsSlugReturn = async () => { - const client = getGraphQLClient(); - - // 10 000 is an arbitrary number for small websites. - const query = gql` - query AllPostsSlug { - posts(first: 10000) { - nodes { - slug - } - } - } - `; - - try { - const response: AllPostsSlugResponse = await client.request(query); - return response.posts.nodes; - } catch (error) { - console.error(JSON.stringify(error, undefined, 2)); - process.exit(1); - } -}; |
