diff options
Diffstat (limited to 'src/services/graphql/post.ts')
| -rw-r--r-- | src/services/graphql/post.ts | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/services/graphql/post.ts b/src/services/graphql/post.ts new file mode 100644 index 0000000..c7144fc --- /dev/null +++ b/src/services/graphql/post.ts @@ -0,0 +1,136 @@ +import { + Article, + FetchPostByReturn, + GetPostByReturn, + PostByResponse, +} from '@ts/types/articles'; +import { gql } from 'graphql-request'; +import { getGraphQLClient } from './client'; + +const fetchPostBySlug: FetchPostByReturn = async (slug: string) => { + const client = getGraphQLClient(); + const query = gql` + query PostBySlug($slug: String!) { + postBy(slug: $slug) { + acfPosts { + postsInSubject { + ... on Subject { + id + featuredImage { + node { + altText + sourceUrl + title + } + } + slug + title + } + } + postsInThematic { + ... on Thematic { + id + slug + title + } + } + } + commentCount + comments { + nodes { + approved + author { + node { + gravatarUrl + name + url + } + } + commentId + content + date + id + parentDatabaseId + parentId + } + } + contentParts { + afterMore + beforeMore + } + date + featuredImage { + node { + altText + sourceUrl + title + } + } + modified + seo { + metaDesc + opengraphAuthor + opengraphDescription + opengraphImage { + altText + sourceUrl + srcSet + } + opengraphModifiedTime + opengraphPublishedTime + opengraphPublisher + opengraphSiteName + opengraphTitle + opengraphType + opengraphUrl + readingTime + } + title + } + } + `; + + const variables = { slug }; + + try { + const response: PostByResponse = await client.request(query, variables); + return response; + } catch (error) { + console.error(JSON.stringify(error, undefined, 2)); + process.exit(1); + } +}; + +export const getPostBySlug: GetPostByReturn = async (slug: string) => { + const rawPost = await fetchPostBySlug(slug); + + const comments = rawPost.postBy.comments.nodes; + const content = rawPost.postBy.contentParts.afterMore; + const featuredImage = rawPost.postBy.featuredImage + ? rawPost.postBy.featuredImage.node + : {}; + const date = { + publication: rawPost.postBy.date, + update: rawPost.postBy.modified, + }; + const intro = rawPost.postBy.contentParts.beforeMore; + const subjects = rawPost.postBy.acfPosts.postsInSubject + ? rawPost.postBy.acfPosts.postsInSubject + : []; + const thematics = rawPost.postBy.acfPosts.postsInThematics + ? rawPost.postBy.acfPosts.postsInThematics + : []; + + const formattedPost: Article = { + ...rawPost.postBy, + comments, + content, + featuredImage, + date, + intro, + subjects, + thematics, + }; + + return formattedPost; +}; |
