diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-08-20 21:39:51 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-08-20 21:39:51 +0200 |
| commit | a0d00743cbbdb77b27c1a3d5711407ffed5befac (patch) | |
| tree | f4caab160daf6b93c74d60ab93edd265c8edb158 /src/services/graphql/comments.ts | |
| parent | 46b158bf9d2f7fa9cc253915151e0b53c052a444 (diff) | |
refactor(types): move and rename GraphQL types
The api file in services was not really readable. So I move the types
and I also rewrite a little the fetch function.
I also rename most of the type to avoid conflict with preexisting
types (like Node) and to keep consistency.
Diffstat (limited to 'src/services/graphql/comments.ts')
| -rw-r--r-- | src/services/graphql/comments.ts | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/src/services/graphql/comments.ts b/src/services/graphql/comments.ts index 28ddfd0..86b6a35 100644 --- a/src/services/graphql/comments.ts +++ b/src/services/graphql/comments.ts @@ -1,10 +1,33 @@ import { Comment } from '@ts/types/app'; +import { GraphQLEdgesInput } from '@ts/types/graphql/generics'; +import { SendCommentInput, SentComment } from '@ts/types/graphql/mutations'; +import { ContentId } from '@ts/types/graphql/queries'; import { RawComment } from '@ts/types/raw-data'; import { getAuthorFromRawData } from '@utils/helpers/author'; -import { fetchAPI, getAPIUrl, SendCommentVars } from './api'; +import { fetchAPI } from './api'; import { sendCommentMutation } from './comments.mutation'; import { commentsQuery } from './comments.query'; +type FetchCommentsInput = ContentId & + Pick<GraphQLEdgesInput, 'after' | 'first'>; + +/** + * Retrieve the comments list from GraphQL. + * + * @param {FetchCommentsInput} variables - An object of variables. + * @returns {Promise<RawComment[]>} The raw comments. + */ +export const fetchComments = async ( + variables: FetchCommentsInput +): Promise<RawComment[]> => { + const response = await fetchAPI<RawComment, typeof commentsQuery>({ + query: commentsQuery, + variables, + }); + + return response.comments.nodes; +}; + /** * Create a comments tree with replies. * @@ -62,27 +85,12 @@ export const getCommentFromRawData = (comment: RawComment): Comment => { * @returns {Promise<Comment[]>} The comments list. */ export const getPostComments = async (id: number): Promise<Comment[]> => { - const response = await fetchAPI<RawComment, typeof commentsQuery>({ - api: getAPIUrl(), - query: commentsQuery, - variables: { contentId: id }, - }); - - const comments = response.comments.nodes.map((comment) => - getCommentFromRawData(comment) - ); + const rawComments = await fetchComments({ contentId: id }); + const comments = rawComments.map((comment) => getCommentFromRawData(comment)); return buildCommentsTree(comments); }; -export type SentComment = { - clientMutationId: string; - success: boolean; - comment: { - approved: boolean; - } | null; -}; - /** * Send a comment using GraphQL API. * @@ -90,10 +98,9 @@ export type SentComment = { * @returns {Promise<SentEmail>} The mutation response. */ export const sendComment = async ( - data: SendCommentVars + data: SendCommentInput ): Promise<SentComment> => { const response = await fetchAPI<SentComment, typeof sendCommentMutation>({ - api: getAPIUrl(), query: sendCommentMutation, variables: { ...data }, }); |
