2020-02-18 08:57:00 +01:00
|
|
|
import { AUTH_ACCESS_TOKEN, AUTH_REFRESH_TOKEN } from "@/constants";
|
|
|
|
import { REFRESH_TOKEN } from "@/graphql/auth";
|
2021-05-12 18:10:07 +02:00
|
|
|
import { IFollower } from "@/types/actor/follower.model";
|
2021-05-17 09:38:04 +02:00
|
|
|
import { IParticipant } from "@/types/participant.model";
|
2021-05-12 18:10:07 +02:00
|
|
|
import { Paginate } from "@/types/paginate";
|
2020-02-18 08:57:00 +01:00
|
|
|
import { saveTokenData } from "@/utils/auth";
|
2021-05-12 18:10:07 +02:00
|
|
|
import {
|
|
|
|
ApolloClient,
|
|
|
|
FieldPolicy,
|
|
|
|
NormalizedCacheObject,
|
|
|
|
Reference,
|
|
|
|
TypePolicies,
|
|
|
|
} from "@apollo/client/core";
|
2021-04-27 16:51:58 +02:00
|
|
|
import introspectionQueryResultData from "../../fragmentTypes.json";
|
2021-05-17 09:38:04 +02:00
|
|
|
import { IMember } from "@/types/actor/member.model";
|
|
|
|
import { IComment } from "@/types/comment.model";
|
|
|
|
import { IEvent } from "@/types/event.model";
|
2021-05-25 18:37:16 +02:00
|
|
|
import { IActivity } from "@/types/activity.model";
|
2021-06-10 09:41:27 +02:00
|
|
|
import uniqBy from "lodash/uniqBy";
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2021-05-12 18:10:07 +02:00
|
|
|
type possibleTypes = { name: string };
|
|
|
|
type schemaType = {
|
|
|
|
kind: string;
|
|
|
|
name: string;
|
|
|
|
possibleTypes: possibleTypes[];
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
const types = introspectionQueryResultData.__schema.types as schemaType[];
|
|
|
|
export const possibleTypes = types.reduce((acc, type) => {
|
|
|
|
if (type.kind === "INTERFACE") {
|
|
|
|
acc[type.name] = type.possibleTypes.map(({ name }) => name);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {} as Record<string, string[]>);
|
|
|
|
|
2021-06-23 16:06:35 +02:00
|
|
|
const replaceMergePolicy = <TExisting = any, TIncoming = any>(
|
|
|
|
_existing: TExisting,
|
|
|
|
incoming: TIncoming
|
|
|
|
): TIncoming => incoming;
|
|
|
|
|
2021-05-12 18:10:07 +02:00
|
|
|
export const typePolicies: TypePolicies = {
|
|
|
|
Discussion: {
|
|
|
|
fields: {
|
2021-06-11 15:05:01 +02:00
|
|
|
comments: paginatedLimitPagination<IComment>(),
|
2021-05-12 18:10:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Group: {
|
|
|
|
fields: {
|
2021-05-17 09:38:04 +02:00
|
|
|
organizedEvents: paginatedLimitPagination([
|
|
|
|
"afterDatetime",
|
|
|
|
"beforeDatetime",
|
|
|
|
]),
|
2021-05-25 18:37:16 +02:00
|
|
|
activity: paginatedLimitPagination<IActivity>(["type", "author"]),
|
2021-05-12 18:10:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Person: {
|
|
|
|
fields: {
|
|
|
|
organizedEvents: pageLimitPagination(),
|
2021-05-17 09:38:04 +02:00
|
|
|
participations: paginatedLimitPagination<IParticipant>(["eventId"]),
|
|
|
|
memberships: paginatedLimitPagination<IMember>(["group"]),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Event: {
|
|
|
|
fields: {
|
|
|
|
participants: paginatedLimitPagination<IParticipant>(["roles"]),
|
2021-06-10 14:44:56 +02:00
|
|
|
comments: pageLimitPagination<IComment>(),
|
2021-05-17 09:38:04 +02:00
|
|
|
relatedEvents: pageLimitPagination<IEvent>(),
|
2021-06-23 16:06:35 +02:00
|
|
|
options: { merge: replaceMergePolicy },
|
|
|
|
participantStats: { merge: replaceMergePolicy },
|
2021-05-17 09:38:04 +02:00
|
|
|
},
|
|
|
|
},
|
2021-05-12 18:10:07 +02:00
|
|
|
RootQueryType: {
|
|
|
|
fields: {
|
|
|
|
relayFollowers: paginatedLimitPagination<IFollower>(),
|
|
|
|
relayFollowings: paginatedLimitPagination<IFollower>([
|
|
|
|
"orderBy",
|
|
|
|
"direction",
|
|
|
|
]),
|
2021-05-17 09:38:04 +02:00
|
|
|
events: paginatedLimitPagination(),
|
|
|
|
groups: paginatedLimitPagination([
|
2021-05-12 18:10:07 +02:00
|
|
|
"preferredUsername",
|
|
|
|
"name",
|
|
|
|
"domain",
|
|
|
|
"local",
|
|
|
|
"suspended",
|
|
|
|
]),
|
2021-05-17 09:38:04 +02:00
|
|
|
persons: paginatedLimitPagination([
|
2021-05-12 18:10:07 +02:00
|
|
|
"preferredUsername",
|
|
|
|
"name",
|
|
|
|
"domain",
|
|
|
|
"local",
|
|
|
|
"suspended",
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
export async function refreshAccessToken(
|
|
|
|
apolloClient: ApolloClient<NormalizedCacheObject>
|
|
|
|
): Promise<boolean> {
|
|
|
|
// Remove invalid access token, so the next request is not authenticated
|
|
|
|
localStorage.removeItem(AUTH_ACCESS_TOKEN);
|
|
|
|
|
|
|
|
const refreshToken = localStorage.getItem(AUTH_REFRESH_TOKEN);
|
|
|
|
|
|
|
|
console.log("Refreshing access token.");
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await apolloClient.mutate({
|
|
|
|
mutation: REFRESH_TOKEN,
|
|
|
|
variables: {
|
|
|
|
refreshToken,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
saveTokenData(res.data.refreshToken);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 18:10:07 +02:00
|
|
|
|
|
|
|
type KeyArgs = FieldPolicy<any>["keyArgs"];
|
|
|
|
|
|
|
|
export function pageLimitPagination<T = Reference>(
|
|
|
|
keyArgs: KeyArgs = false
|
|
|
|
): FieldPolicy<T[]> {
|
|
|
|
return {
|
|
|
|
keyArgs,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
merge(existing, incoming, { args }) {
|
|
|
|
if (!incoming) return existing;
|
|
|
|
if (!existing) return incoming; // existing will be empty the first time
|
|
|
|
|
|
|
|
return doMerge(existing as Array<T>, incoming as Array<T>, args);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function paginatedLimitPagination<T = Paginate<any>>(
|
|
|
|
keyArgs: KeyArgs = false
|
|
|
|
): FieldPolicy<Paginate<T>> {
|
|
|
|
return {
|
|
|
|
keyArgs,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
merge(existing, incoming, { args }) {
|
|
|
|
if (!incoming) return existing;
|
|
|
|
if (!existing) return incoming; // existing will be empty the first time
|
|
|
|
|
|
|
|
return {
|
|
|
|
total: incoming.total,
|
|
|
|
elements: doMerge(existing.elements, incoming.elements, args),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function doMerge<T = any>(
|
|
|
|
existing: Array<T>,
|
|
|
|
incoming: Array<T>,
|
|
|
|
args: Record<string, any> | null
|
|
|
|
): Array<T> {
|
2021-06-30 18:04:01 +02:00
|
|
|
const merged = existing && Array.isArray(existing) ? existing.slice(0) : [];
|
2021-05-12 18:10:07 +02:00
|
|
|
let res;
|
|
|
|
if (args) {
|
|
|
|
// Assume an page of 1 if args.page omitted.
|
|
|
|
const { page = 1, limit = 10 } = args;
|
|
|
|
for (let i = 0; i < incoming.length; ++i) {
|
|
|
|
merged[(page - 1) * limit + i] = incoming[i];
|
|
|
|
}
|
|
|
|
res = merged;
|
|
|
|
} else {
|
|
|
|
// It's unusual (probably a mistake) for a paginated field not
|
|
|
|
// to receive any arguments, so you might prefer to throw an
|
|
|
|
// exception here, instead of recovering by appending incoming
|
|
|
|
// onto the existing array.
|
|
|
|
res = [...merged, ...incoming];
|
2021-06-10 09:41:27 +02:00
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
res = uniqBy(res, (elem: any) => elem.__ref);
|
2021-05-12 18:10:07 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|