diff --git a/faust.config.js b/faust.config.js index 30c8636e..7f83e939 100644 --- a/faust.config.js +++ b/faust.config.js @@ -1,13 +1,35 @@ +import { relayStylePagination } from "@apollo/client/utilities"; import { setConfig } from "@faustwp/core"; import possibleTypes from "./possibleTypes.json"; import templates from "./src/wp-templates"; +class PostTypePolicyPlugin { + apply({ addFilter }) { + addFilter( + "apolloClientInMemoryCacheOptions", + "faust", + (inMemoryCacheObject) => { + return { + ...inMemoryCacheObject, + typePolicies: { + Query: { + fields: { + posts: relayStylePagination(), + }, + }, + }, + }; + }, + ); + } +} + /** * @type {import('@faustwp/core').FaustConfig} */ export default setConfig({ templates, - plugins: [], + plugins: [new PostTypePolicyPlugin()], possibleTypes, usePersistedQueries: true, }); diff --git a/src/pages/blog/index.jsx b/src/pages/blog/index.jsx index 53e3ae62..a87c1959 100644 --- a/src/pages/blog/index.jsx +++ b/src/pages/blog/index.jsx @@ -2,7 +2,7 @@ import { gql, useQuery } from "@apollo/client"; import { getNextStaticProps } from "@faustwp/core"; import { ArrowPathIcon, ChevronDownIcon } from "@heroicons/react/24/outline"; import Link from "next/link"; -import { useState, useEffect } from "react"; +import { useState } from "react"; import Card from "@/components/card"; import Date from "@/components/date"; import Seo from "@/components/seo"; @@ -16,6 +16,7 @@ const GET_POSTS = gql` } edges { node { + id title excerpt date @@ -29,28 +30,18 @@ const GET_POSTS = gql` const BATCH_SIZE = 12; export default function BlogIndex() { - const { data, loading, error, fetchMore } = useQuery(GET_POSTS, { - variables: { first: BATCH_SIZE, after: undefined }, + const { + data, + loading = true, + error, + fetchMore, + } = useQuery(GET_POSTS, { + // eslint-disable-next-line unicorn/no-null + variables: { first: BATCH_SIZE, after: null }, notifyOnNetworkStatusChange: true, fetchPolicy: "cache-and-network", }); - const [posts, setPosts] = useState([]); - const [loadingMore, setLoadingMore] = useState(false); - const [cursor, setCursor] = useState(false); - const [hasMorePosts, setHasMorePosts] = useState(true); - - useEffect(() => { - if (data) { - setPosts((previousPosts) => [ - ...previousPosts, - ...data.posts.edges.map((edge) => edge.node), - ]); - setCursor(data.posts.pageInfo.endCursor); - setHasMorePosts(data.posts.pageInfo.hasNextPage); - } - }, [data]); - if (loading && !data) return (