Fix apollo cache bugs with identity creation/edition/deletion

Closes #798

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-08-05 15:49:54 +02:00
parent 51cd066a14
commit 5bcf8a2305
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 49 additions and 17 deletions

View File

@ -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` export const LIST_PROFILES = gql`
query ListProfiles( query ListProfiles(
$preferredUsername: String $preferredUsername: String

View File

@ -218,6 +218,7 @@ import {
DELETE_PERSON, DELETE_PERSON,
FETCH_PERSON, FETCH_PERSON,
IDENTITIES, IDENTITIES,
PERSON_FRAGMENT,
UPDATE_PERSON, UPDATE_PERSON,
} from "../../../graphql/actor"; } from "../../../graphql/actor";
import { IPerson, Person } from "../../../types/actor"; import { IPerson, Person } from "../../../types/actor";
@ -236,6 +237,8 @@ import { IConfig } from "@/types/config.model";
import { CONFIG } from "@/graphql/config"; import { CONFIG } from "@/graphql/config";
import { ServerParseError } from "@apollo/client/link/http"; import { ServerParseError } from "@apollo/client/link/http";
import { ApolloCache, FetchResult, InMemoryCache } from "@apollo/client/core"; import { ApolloCache, FetchResult, InMemoryCache } from "@apollo/client/core";
import pick from "lodash/pick";
import { ActorType } from "@/types/enums";
@Component({ @Component({
components: { components: {
@ -345,11 +348,14 @@ export default class EditIdentity extends mixins(identityEditionMixin) {
}); });
if (data) { if (data) {
data.identities = data.identities.filter( store.writeQuery({
(i) => i.id !== this.identity.id query: IDENTITIES,
); data: {
identities: data.identities.filter(
store.writeQuery({ query: IDENTITIES, data }); (i) => i.id !== this.identity.id
),
},
});
} }
}, },
}); });
@ -392,14 +398,16 @@ export default class EditIdentity extends mixins(identityEditionMixin) {
}); });
if (data && updateData?.updatePerson) { 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); 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) { if (data && updateData?.createPerson) {
data.identities.push(updateData?.createPerson); store.writeQuery({
query: IDENTITIES,
store.writeQuery({ query: IDENTITIES, data }); 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<Record<string, unknown>> {
/** /**
* We set the avatar only if user has selected one * 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` `${this.identity.preferredUsername}'s avatar`
); );
} }
const res = { ...this.identity, ...avatarObj }; return pick({ ...this.identity, ...avatarObj }, [
return res; "id",
"preferredUsername",
"name",
"summary",
"avatar",
]);
} }
private async redirectIfNoIdentitySelected(identityParam?: string) { private async redirectIfNoIdentitySelected(identityParam?: string) {