Skip to content

Commit

Permalink
urql-pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 14, 2020
1 parent e967790 commit 590e1fe
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
24 changes: 20 additions & 4 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { usePostsQuery } from "../generated/graphql";
import { Layout } from "../components/Layout";
import { Link, Stack, Box, Heading, Text, Flex, Button } from "@chakra-ui/core";
import NextLink from "next/link";
import { useState } from "react";

const Index = () => {
const [variables, setVariables] = useState({
limit: 10,
cursor: null as null | string,
});

console.log(variables);

const [{ data, fetching }] = usePostsQuery({
variables: {
limit: 10,
},
variables,
});

if (!fetching && !data) {
Expand Down Expand Up @@ -39,7 +45,17 @@ const Index = () => {
)}
{data ? (
<Flex>
<Button isLoading={fetching} m="auto" my={8}>
<Button
onClick={() => {
setVariables({
limit: variables.limit,
cursor: data.posts[data.posts.length - 1].createdAt,
});
}}
isLoading={fetching}
m="auto"
my={8}
>
load more
</Button>
</Flex>
Expand Down
91 changes: 89 additions & 2 deletions web/src/utils/createUrqlClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { cacheExchange } from "@urql/exchange-graphcache";
import { dedupExchange, Exchange, fetchExchange } from "urql";
import { cacheExchange, Resolver } from "@urql/exchange-graphcache";
import {
dedupExchange,
Exchange,
fetchExchange,
stringifyVariables,
} from "urql";
import { pipe, tap } from "wonka";
import {
LoginMutation,
Expand All @@ -10,6 +15,7 @@ import {
} from "../generated/graphql";
import { betterUpdateQuery } from "./betterUpdateQuery";
import Router from "next/router";
import { FieldsOnCorrectTypeRule } from "graphql";

const errorExchange: Exchange = ({ forward }) => (ops$) => {
return pipe(
Expand All @@ -22,6 +28,82 @@ const errorExchange: Exchange = ({ forward }) => (ops$) => {
);
};

const cursorPagination = (): Resolver => {
return (_parent, fieldArgs, cache, info) => {
const { parentKey: entityKey, fieldName } = info;
const allFields = cache.inspectFields(entityKey);
console.log("allFields: ", allFields);
const fieldInfos = allFields.filter((info) => info.fieldName === fieldName);
const size = fieldInfos.length;
if (size === 0) {
return undefined;
}

const fieldKey = `${fieldName}(${stringifyVariables(fieldArgs)})`;
const isItInTheCache = cache.resolveFieldByKey(entityKey, fieldKey);
info.partial = !isItInTheCache;
const results: string[] = [];
fieldInfos.forEach((fi) => {
const data = cache.resolveFieldByKey(entityKey, fi.fieldKey) as string[];
results.push(...data);
});

return 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;
// }
};
};

export const createUrqlClient = (ssrExchange: any) => ({
url: "http://localhost:4000/graphql",
fetchOptions: {
Expand All @@ -30,6 +112,11 @@ export const createUrqlClient = (ssrExchange: any) => ({
exchanges: [
dedupExchange,
cacheExchange({
resolvers: {
Query: {
posts: cursorPagination(),
},
},
updates: {
Mutation: {
logout: (_result, args, cache, info) => {
Expand Down

0 comments on commit 590e1fe

Please sign in to comment.