aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/post.ts
blob: c7144fc9faf526b7c19f408079b4655aa6cf4cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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;
};