mobilizon/js/src/apollo/user.ts

90 lines
2.1 KiB
TypeScript
Raw Normal View History

import { CURRENT_ACTOR_CLIENT } from "@/graphql/actor";
import { CURRENT_USER_CLIENT } from "@/graphql/user";
import { ICurrentUserRole } from "@/types/enums";
import { ApolloCache, NormalizedCacheObject } from "@apollo/client/cache";
import { Resolvers } from "@apollo/client/core/types";
2019-08-12 16:04:16 +02:00
export default function buildCurrentUserResolver(
cache: ApolloCache<NormalizedCacheObject>
): Resolvers {
cache.writeQuery({
query: CURRENT_USER_CLIENT,
2019-08-12 16:04:16 +02:00
data: {
currentUser: {
__typename: "CurrentUser",
2019-08-12 16:04:16 +02:00
id: null,
email: null,
isLoggedIn: false,
role: ICurrentUserRole.USER,
2019-08-12 16:04:16 +02:00
},
},
});
cache.writeQuery({
query: CURRENT_ACTOR_CLIENT,
data: {
currentActor: {
__typename: "CurrentActor",
id: null,
preferredUsername: null,
name: null,
avatar: null,
},
2019-01-18 14:47:10 +01:00
},
2019-08-12 16:04:16 +02:00
});
2019-01-18 14:47:10 +01:00
2019-08-12 16:04:16 +02:00
return {
2019-08-21 11:25:09 +02:00
Mutation: {
updateCurrentUser: (
_: any,
{
id,
email,
isLoggedIn,
role,
}: { id: string; email: string; isLoggedIn: boolean; role: string },
{ cache: localCache }: { cache: ApolloCache<NormalizedCacheObject> }
) => {
2019-08-21 11:25:09 +02:00
const data = {
2019-01-18 14:47:10 +01:00
currentUser: {
id,
email,
isLoggedIn,
role,
__typename: "CurrentUser",
2019-01-18 14:47:10 +01:00
},
2019-08-21 11:25:09 +02:00
};
2019-01-18 14:47:10 +01:00
localCache.writeQuery({ data, query: CURRENT_USER_CLIENT });
},
updateCurrentActor: (
_: any,
{
id,
preferredUsername,
avatar,
name,
}: {
id: string;
preferredUsername: string;
avatar: string;
name: string;
},
{ cache: localCache }: { cache: ApolloCache<NormalizedCacheObject> }
) => {
const data = {
currentActor: {
id,
preferredUsername,
avatar,
name,
__typename: "CurrentActor",
},
};
localCache.writeQuery({ data, query: CURRENT_ACTOR_CLIENT });
2019-08-21 11:25:09 +02:00
},
2019-01-18 14:47:10 +01:00
},
2019-08-12 16:04:16 +02:00
};
2019-08-13 08:43:37 +02:00
}