Skip to content

Commit

Permalink
cache-reset
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 16, 2020
1 parent c35aa00 commit 2939a82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 62 deletions.
7 changes: 5 additions & 2 deletions web/src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Box, Link, Flex, Button, Heading } from "@chakra-ui/core";
import NextLink from "next/link";
import { useMeQuery, useLogoutMutation } from "../generated/graphql";
import { isServer } from "../utils/isServer";
import { useRouter } from "next/router";

interface NavBarProps {}

export const NavBar: React.FC<NavBarProps> = ({}) => {
const router = useRouter();
const [{ fetching: logoutFetching }, logout] = useLogoutMutation();
const [{ data, fetching }] = useMeQuery({
pause: isServer(),
Expand Down Expand Up @@ -39,8 +41,9 @@ export const NavBar: React.FC<NavBarProps> = ({}) => {
</NextLink>
<Box mr={2}>{data.me.username}</Box>
<Button
onClick={() => {
logout();
onClick={async () => {
await logout();
router.reload();
}}
isLoading={logoutFetching}
variant="link"
Expand Down
71 changes: 11 additions & 60 deletions web/src/utils/createUrqlClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cacheExchange, Resolver } from "@urql/exchange-graphcache";
import { cacheExchange, Resolver, Cache } from "@urql/exchange-graphcache";
import {
dedupExchange,
Exchange,
Expand Down Expand Up @@ -64,61 +64,17 @@ const cursorPagination = (): Resolver => {
hasMore,
posts: results,
};

// const visited = new Set();
// let result: NullArray<string> = [];
// let prevOffset: number | null = null;

// for (let i = 0; i < size; i++) {
// const { fieldKey, arguments: args } = fieldInfos[i];
// if (args === null || !compareArgs(fieldArgs, args)) {
// continue;
// }

// const links = cache.resolveFieldByKey(entityKey, fieldKey) as string[];
// const currentOffset = args[cursorArgument];

// if (
// links === null ||
// links.length === 0 ||
// typeof currentOffset !== "number"
// ) {
// continue;
// }

// if (!prevOffset || currentOffset > prevOffset) {
// for (let j = 0; j < links.length; j++) {
// const link = links[j];
// if (visited.has(link)) continue;
// result.push(link);
// visited.add(link);
// }
// } else {
// const tempResult: NullArray<string> = [];
// for (let j = 0; j < links.length; j++) {
// const link = links[j];
// if (visited.has(link)) continue;
// tempResult.push(link);
// visited.add(link);
// }
// result = [...tempResult, ...result];
// }

// prevOffset = currentOffset;
// }

// const hasCurrentPage = cache.resolve(entityKey, fieldName, fieldArgs);
// if (hasCurrentPage) {
// return result;
// } else if (!(info as any).store.schema) {
// return undefined;
// } else {
// info.partial = true;
// return result;
// }
};
};

function invalidateAllPosts(cache: Cache) {
const allFields = cache.inspectFields("Query");
const fieldInfos = allFields.filter((info) => info.fieldName === "posts");
fieldInfos.forEach((fi) => {
cache.invalidate("Query", "posts", fi.arguments || {});
});
}

export const createUrqlClient = (ssrExchange: any, ctx: any) => {
let cookie = "";
if (isServer()) {
Expand Down Expand Up @@ -185,13 +141,7 @@ export const createUrqlClient = (ssrExchange: any, ctx: any) => {
}
},
createPost: (_result, args, cache, info) => {
const allFields = cache.inspectFields("Query");
const fieldInfos = allFields.filter(
(info) => info.fieldName === "posts"
);
fieldInfos.forEach((fi) => {
cache.invalidate("Query", "posts", fi.arguments || {});
});
invalidateAllPosts(cache);
},
logout: (_result, args, cache, info) => {
betterUpdateQuery<LogoutMutation, MeQuery>(
Expand All @@ -216,6 +166,7 @@ export const createUrqlClient = (ssrExchange: any, ctx: any) => {
}
}
);
invalidateAllPosts(cache);
},
register: (_result, args, cache, info) => {
betterUpdateQuery<RegisterMutation, MeQuery>(
Expand Down

0 comments on commit 2939a82

Please sign in to comment.