From 5bcf8a2305ce98f5205fbf9c727d40d49069a098 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 5 Aug 2021 15:49:54 +0200 Subject: [PATCH] Fix apollo cache bugs with identity creation/edition/deletion Closes #798 Signed-off-by: Thomas Citharel --- js/src/graphql/actor.ts | 13 +++++ .../views/Account/children/EditIdentity.vue | 53 +++++++++++++------ 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/js/src/graphql/actor.ts b/js/src/graphql/actor.ts index 431ba2b84..54f616d94 100644 --- a/js/src/graphql/actor.ts +++ b/js/src/graphql/actor.ts @@ -117,6 +117,19 @@ export const GET_PERSON = gql` } `; +export const PERSON_FRAGMENT = gql` + fragment PersonFragment on Person { + id + avatar { + id + url + } + type + preferredUsername + name + } +`; + export const LIST_PROFILES = gql` query ListProfiles( $preferredUsername: String diff --git a/js/src/views/Account/children/EditIdentity.vue b/js/src/views/Account/children/EditIdentity.vue index 52cc77f5a..4d4b6bcea 100644 --- a/js/src/views/Account/children/EditIdentity.vue +++ b/js/src/views/Account/children/EditIdentity.vue @@ -218,6 +218,7 @@ import { DELETE_PERSON, FETCH_PERSON, IDENTITIES, + PERSON_FRAGMENT, UPDATE_PERSON, } from "../../../graphql/actor"; import { IPerson, Person } from "../../../types/actor"; @@ -236,6 +237,8 @@ import { IConfig } from "@/types/config.model"; import { CONFIG } from "@/graphql/config"; import { ServerParseError } from "@apollo/client/link/http"; import { ApolloCache, FetchResult, InMemoryCache } from "@apollo/client/core"; +import pick from "lodash/pick"; +import { ActorType } from "@/types/enums"; @Component({ components: { @@ -345,11 +348,14 @@ export default class EditIdentity extends mixins(identityEditionMixin) { }); if (data) { - data.identities = data.identities.filter( - (i) => i.id !== this.identity.id - ); - - store.writeQuery({ query: IDENTITIES, data }); + store.writeQuery({ + query: IDENTITIES, + data: { + identities: data.identities.filter( + (i) => i.id !== this.identity.id + ), + }, + }); } }, }); @@ -392,14 +398,16 @@ export default class EditIdentity extends mixins(identityEditionMixin) { }); if (data && updateData?.updatePerson) { - const index = data.identities.findIndex( - (i) => i.id === this.identity.id - ); - - this.$set(data.identities, index, updateData?.updatePerson); this.maybeUpdateCurrentActorCache(updateData?.updatePerson); - store.writeQuery({ query: IDENTITIES, data }); + store.writeFragment({ + fragment: PERSON_FRAGMENT, + id: `Person:${updateData?.updatePerson.id}`, + data: { + ...updateData?.updatePerson, + type: ActorType.PERSON, + }, + }); } }, }); @@ -430,9 +438,15 @@ export default class EditIdentity extends mixins(identityEditionMixin) { }); if (data && updateData?.createPerson) { - data.identities.push(updateData?.createPerson); - - store.writeQuery({ query: IDENTITIES, data }); + store.writeQuery({ + query: IDENTITIES, + data: { + identities: [ + ...data.identities, + { ...updateData?.createPerson, type: ActorType.PERSON }, + ], + }, + }); } }, }); @@ -582,7 +596,7 @@ export default class EditIdentity extends mixins(identityEditionMixin) { } } - private async buildVariables() { + private async buildVariables(): Promise> { /** * We set the avatar only if user has selected one */ @@ -594,8 +608,13 @@ export default class EditIdentity extends mixins(identityEditionMixin) { `${this.identity.preferredUsername}'s avatar` ); } - const res = { ...this.identity, ...avatarObj }; - return res; + return pick({ ...this.identity, ...avatarObj }, [ + "id", + "preferredUsername", + "name", + "summary", + "avatar", + ]); } private async redirectIfNoIdentitySelected(identityParam?: string) {