From a0d00743cbbdb77b27c1a3d5711407ffed5befac Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sat, 20 Aug 2022 21:39:51 +0200 Subject: 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. --- src/services/graphql/comments.ts | 47 +++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 20 deletions(-) (limited to 'src/services/graphql/comments.ts') 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; + +/** + * Retrieve the comments list from GraphQL. + * + * @param {FetchCommentsInput} variables - An object of variables. + * @returns {Promise} The raw comments. + */ +export const fetchComments = async ( + variables: FetchCommentsInput +): Promise => { + const response = await fetchAPI({ + 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} The comments list. */ export const getPostComments = async (id: number): Promise => { - const response = await fetchAPI({ - 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} The mutation response. */ export const sendComment = async ( - data: SendCommentVars + data: SendCommentInput ): Promise => { const response = await fetchAPI({ - api: getAPIUrl(), query: sendCommentMutation, variables: { ...data }, }); -- cgit v1.2.3