Skip to content

Commit

Permalink
chore: replace restful api to graphql (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark86092 authored Jan 11, 2025
1 parent 24e1eb4 commit cfd317b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 49 deletions.
38 changes: 11 additions & 27 deletions src/apis/experiencesApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,24 @@ import fetchUtil from 'utils/fetchUtil';

import graphqlClient from 'utils/graphqlClient';
import {
queryExperienceRepliesGql,
deleteExpereinceLikeGql,
createExperienceLikeGql,
queryExperienceGql,
queryExperienceLikeGql,
changeExperienceStatusGql,
queryRelatedExperiencesGql,
queryExperienceCountGql,
} from 'graphql/experience';
import { getPopularExperiencesQuery } from 'graphql/popularExperience';
import { deleteReplyLike, createReplyLike } from 'graphql/reply';

const getExperienceReplyOptions = {
start: 0,
limit: 100,
};

export const getExperienceReply = options => {
const finalOptions = {
...getExperienceReplyOptions,
...options,
};

const { experienceId, start, limit, token } = finalOptions;

const url = `/experiences/${experienceId}/replies`;

return fetchUtil(url).get({
query: {
start,
limit,
},
export const queryExperienceReplies = async ({ id, token }) =>
graphqlClient({
query: queryExperienceRepliesGql,
variables: { id },
token,
});
};
}).then(data => data.experience.replies);

export const postExperienceReply = ({ id, comment, token }) =>
fetchUtil(`/experiences/${id}/replies`).post({
Expand Down Expand Up @@ -131,11 +116,10 @@ export const getPopularExperiences = () =>
query: getPopularExperiencesQuery,
}).then(data => data.popular_experiences);

export const patchExperience = ({ id, status, token }) =>
fetchUtil(`/experiences/${id}`).patch({
body: {
status,
},
export const changeExperienceStatus = ({ id, status, token }) =>
graphqlClient({
query: changeExperienceStatusGql,
variables: { input: { id, status } },
token,
});

Expand Down
6 changes: 3 additions & 3 deletions src/apis/timeAndSalaryApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import graphqlClient from 'utils/graphqlClient';
import {
getSearchCompanyQuery,
getSearchJobTitleQuery,
changeSalaryWorkTimeStatus,
changeSalaryWorkTimeStatusGql,
} from 'graphql/timeAndSalary';

const endpoint = '/workings';
Expand All @@ -23,9 +23,9 @@ export const fetchSearchJobTitle = ({ jobTitle }) =>
export const postWorkings = ({ body, token }) =>
fetchUtil(endpoint).post({ body, token });

export const patchWorking = ({ id, status, token }) =>
export const changeSalaryWorkTimeStatus = ({ id, status, token }) =>
graphqlClient({
query: changeSalaryWorkTimeStatus,
query: changeSalaryWorkTimeStatusGql,
variables: { input: { id, status } },
token,
});
11 changes: 9 additions & 2 deletions src/components/ExperienceDetail/MessageBoard/CommentBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const formatDate = d => {

const CommentBlock = ({ reply, toggleReplyLike }) => {
return (
<section className={styles.container} id={`reply-${reply._id}`}>
<section className={styles.container} id={`reply-${reply.id}`}>
<div className={styles.reaction}>
<ThumbsUp
count={reply.like_count}
Expand All @@ -36,7 +36,14 @@ const CommentBlock = ({ reply, toggleReplyLike }) => {
};

CommentBlock.propTypes = {
reply: PropTypes.object.isRequired,
reply: PropTypes.shape({
content: PropTypes.string.isRequired,
created_at: PropTypes.string.isRequired,
floor: PropTypes.number.isRequired,
id: PropTypes.string.isRequired,
like_count: PropTypes.number.isRequired,
liked: PropTypes.bool,
}).isRequired,
toggleReplyLike: PropTypes.func.isRequired,
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/ExperienceDetail/MessageBoard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const MessageBoard = ({ experienceId }) => {
<hr />
{repliesState.value.map(reply => (
<CommentBlock
key={reply._id}
key={reply.id}
reply={reply}
toggleReplyLike={async () => {
await likeReply(reply);
Expand Down
20 changes: 9 additions & 11 deletions src/components/ExperienceDetail/hooks/useQueryReplies.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { useAsyncFn } from 'react-use';
import { useToken } from 'hooks/auth';
import { getExperienceReply as getExperienceReplyApi } from 'apis/experiencesApi';
import { queryExperienceReplies } from 'apis/experiencesApi';

const useQueryReplies = experienceId => {
const token = useToken();
return useAsyncFn(async () => {
const result = await getExperienceReplyApi({
experienceId,
start: 0,
limit: 100,
token,
});

return result.replies;
}, [experienceId, token]);
return useAsyncFn(
() =>
queryExperienceReplies({
id: experienceId,
token,
}),
[experienceId, token],
);
};

export default useQueryReplies;
8 changes: 4 additions & 4 deletions src/components/Me/useQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { useAsyncFn } from 'react-use';
import { useToken } from 'hooks/auth';
import { queryMyPublishesApi } from 'apis/me';
import {
patchExperience as patchExperienceApi,
patchReply as patchReplyApi,
changeExperienceStatus,
} from 'apis/experiencesApi';
import { patchWorking as patchWorkingApi } from 'apis/timeAndSalaryApi';
import { changeSalaryWorkTimeStatus } from 'apis/timeAndSalaryApi';

export const useFetchMyPublishes = () => {
const token = useToken();
Expand All @@ -22,7 +22,7 @@ export const useToggleExperienceStatus = () => {
const token = useToken();
return useCallback(
o => {
return patchExperienceApi({
return changeExperienceStatus({
id: o.id,
status: o.status === 'published' ? 'hidden' : 'published',
token,
Expand All @@ -36,7 +36,7 @@ export const useToggleSalaryWorkTimeStatus = () => {
const token = useToken();
return useCallback(
o => {
return patchWorkingApi({
return changeSalaryWorkTimeStatus({
id: o.id,
status: o.status === 'published' ? 'hidden' : 'published',
token,
Expand Down
25 changes: 25 additions & 0 deletions src/graphql/experience.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ export const queryExperienceLikeGql = /* GraphQL */ `
}
`;

export const queryExperienceRepliesGql = /* GraphQL */ `
query($id: ID!) {
experience(id: $id) {
replies {
id
content
like_count
floor
created_at
liked
}
}
}
`;

export const createInterviewExperience = /* GraphQL */ `
mutation CreateInterviewExperience($input: CreateInterviewExperienceInput!) {
createInterviewExperience(input: $input) {
Expand Down Expand Up @@ -175,3 +190,13 @@ export const queryExperienceCountGql = /* GraphQL */ `
experienceCount
}
`;

export const changeExperienceStatusGql = /* GraphQL */ `
mutation($input: ChangeExperienceStatusInput!) {
changeExperienceStatus(input: $input) {
experience {
id
}
}
}
`;
2 changes: 1 addition & 1 deletion src/graphql/timeAndSalary.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getSearchJobTitleQuery = /* GraphQL */ `
}
`;

export const changeSalaryWorkTimeStatus = /* GraphQL */ `
export const changeSalaryWorkTimeStatusGql = /* GraphQL */ `
mutation($input: ChangeSalaryWorkTimeStatusInput!) {
changeSalaryWorkTimeStatus(input: $input) {
salary_work_time {
Expand Down

0 comments on commit cfd317b

Please sign in to comment.