summaryrefslogtreecommitdiffstats
path: root/src/services/graphql/comments.ts
blob: b7a9ed2ab3829fac6a66c739e3cd7d0d0cc08f06 (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
import {
  CreatedCommentResponse,
  CreatedCommentReturn,
} from '@ts/types/comments';
import { gql } from 'graphql-request';
import { getGraphQLClient } from './client';

export const createComment: CreatedCommentReturn = async (
  author: string,
  authorEmail: string,
  authorUrl: string,
  content: string,
  parent: number,
  commentOn: number,
  mutationId: string
) => {
  const client = getGraphQLClient();
  const mutation = gql`
    mutation CreateComment(
      $author: String!
      $authorEmail: String!
      $authorUrl: String!
      $content: String!
      $parent: ID!
      $commentOn: Int!
      $mutationId: String!
    ) {
      createComment(
        input: {
          author: $author
          authorEmail: $authorEmail
          authorUrl: $authorUrl
          content: $content
          parent: $parent
          commentOn: $commentOn
          clientMutationId: $mutationId
        }
      ) {
        clientMutationId
        success
        comment {
          approved
        }
      }
    }
  `;

  const variables = {
    author,
    authorEmail,
    authorUrl,
    content,
    parent,
    commentOn,
    mutationId,
  };

  try {
    const response: CreatedCommentResponse = await client.request(
      mutation,
      variables
    );
    return response.createComment;
  } catch (error) {
    console.error(error, undefined, 2);
    throw new Error(`An uncaught exception has occurred: ${error}`);
  }
};