From 48055f6055aff585d9ac865ceac514339890466a Mon Sep 17 00:00:00 2001 From: Aseer KT Date: Sun, 22 Aug 2021 19:03:16 +0530 Subject: [PATCH] fix: update profile after edit profile --- client/src/generated/graphql.tsx | 10 ++++++---- .../graphql/fragments/UserWIthProfile.graphql | 6 ------ .../src/graphql/mutations/editProfile.graphql | 4 +++- client/src/routes/EditProfile.tsx | 17 ++++++++++++----- server/src/resolvers/ProfileResolver.ts | 9 ++++----- 5 files changed, 25 insertions(+), 21 deletions(-) delete mode 100644 client/src/graphql/fragments/UserWIthProfile.graphql diff --git a/client/src/generated/graphql.tsx b/client/src/generated/graphql.tsx index 5f6d9d8..c31f9e8 100644 --- a/client/src/generated/graphql.tsx +++ b/client/src/generated/graphql.tsx @@ -46,7 +46,7 @@ export type CreatePostResponse = { export type EditProfileResponse = { __typename?: 'EditProfileResponse'; - ok: Scalars['Boolean']; + user?: Maybe; errors?: Maybe>; }; @@ -317,7 +317,7 @@ export type EditProfileMutationVariables = Exact<{ }>; -export type EditProfileMutation = { __typename?: 'Mutation', editProfile: { __typename?: 'EditProfileResponse', ok: boolean, errors?: Maybe> } }; +export type EditProfileMutation = { __typename?: 'Mutation', editProfile: { __typename?: 'EditProfileResponse', user?: Maybe<{ __typename?: 'User', id: string, username: string, email: string, isFollowing: boolean, profile: { __typename?: 'Profile', id: string, name: string, website: string, bio: string, gender: string, imgURL: string, followersCount: number, followingsCount: number } }>, errors?: Maybe> } }; export type LoginMutationVariables = Exact<{ username: Scalars['String']; @@ -654,14 +654,16 @@ export const EditProfileDocument = gql` gender: $gender email: $email ) { - ok + user { + ...UserWithProfile + } errors { path message } } } - `; + ${UserWithProfileFragmentDoc}`; export type EditProfileMutationFn = Apollo.MutationFunction; /** diff --git a/client/src/graphql/fragments/UserWIthProfile.graphql b/client/src/graphql/fragments/UserWIthProfile.graphql deleted file mode 100644 index 03ed3e6..0000000 --- a/client/src/graphql/fragments/UserWIthProfile.graphql +++ /dev/null @@ -1,6 +0,0 @@ -fragment UserWithProfile on User { - ...UserField - profile { - ...RegularProfile - } -} \ No newline at end of file diff --git a/client/src/graphql/mutations/editProfile.graphql b/client/src/graphql/mutations/editProfile.graphql index c2a1dbe..9770045 100644 --- a/client/src/graphql/mutations/editProfile.graphql +++ b/client/src/graphql/mutations/editProfile.graphql @@ -12,7 +12,9 @@ mutation EditProfile( gender: $gender email: $email ) { - ok + user { + ...UserWithProfile + } errors { path message diff --git a/client/src/routes/EditProfile.tsx b/client/src/routes/EditProfile.tsx index e072291..f6574c1 100644 --- a/client/src/routes/EditProfile.tsx +++ b/client/src/routes/EditProfile.tsx @@ -1,7 +1,11 @@ import Button from '../components-ui/Button'; import Container from '../components-ui/Container'; import InputField from '../components-ui/InputField'; -import { useEditProfileMutation, useMeQuery } from '../generated/graphql'; +import { + MeDocument, + useEditProfileMutation, + useMeQuery, +} from '../generated/graphql'; import ChangeProfilePhoto from '../components/ChangeProfilePhoto'; import { Field, Form, Formik } from 'formik'; import { useMessageCtx } from '../context/MessageContext'; @@ -42,18 +46,21 @@ const EditProfile: React.FC = () => { }} onSubmit={async (values, action) => { try { - const res = await editProfile({ + await editProfile({ variables: values, update: (cache, { data }) => { if (data) { - const { errors, ok } = data.editProfile; + const { errors, user } = data.editProfile; if (errors) { errors.forEach(({ path, message }) => { action.setFieldError(path, message); }); } - if (ok) { - cache.evict({ id: cache.identify(user) }); + if (user) { + cache.writeQuery({ + query: MeDocument, + data: { me: user }, + }); setMessage('Profile updated'); } } diff --git a/server/src/resolvers/ProfileResolver.ts b/server/src/resolvers/ProfileResolver.ts index 24bf612..0584fbd 100644 --- a/server/src/resolvers/ProfileResolver.ts +++ b/server/src/resolvers/ProfileResolver.ts @@ -46,8 +46,8 @@ export class EditProfileArgs { @ObjectType() export class EditProfileResponse { - @Field() - ok: boolean; + @Field(() => User, { nullable: true }) + user?: User; @Field(() => [FieldError], { nullable: true }) errors?: FieldError[]; } @@ -158,13 +158,13 @@ export class ProfileResolver { const profileErrors = await validate(profile); if (userErrors.length > 0 || profileErrors.length > 0) { const errors = formatErrors([...userErrors, ...profileErrors]); - return { ok: false, errors }; + return { errors }; } await getManager().transaction(async (tem) => { await tem.save(user); await tem.save(profile); }); - return { ok: true }; + return { user }; } catch (err) { console.log(err); } @@ -172,7 +172,6 @@ export class ProfileResolver { console.log(profile, user); } return { - ok: false, errors: [{ path: 'unknown', message: 'Server Error' }], }; }