From d2b6fb40f7b86a991144593f71b0c3c077e42992 Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Tue, 10 Dec 2024 19:06:06 +0530 Subject: [PATCH] chore: optimize queries for specific user Signed-off-by: Yash Khare --- lib/api.ts | 49 +++++++++++++++++++++++++++++++++++++++- pages/authors/[slug].tsx | 32 ++++++++++++++------------ 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/lib/api.ts b/lib/api.ts index c54fec2..41e2577 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -515,7 +515,54 @@ export async function getMoreStoriesForSlugs(tags, slug) { }; } - +export async function getPostsByAuthorName(authorName) { + let allEdges = []; + const data = await fetchAPI( + `query MyQuery3 { + posts(where: {authorName: "${authorName}"}) { + edges { + node { + title + excerpt + slug + date + postId + featuredImage { + node { + sourceUrl + } + } + author { + node { + name + firstName + lastName + avatar { + url + } + } + } + ppmaAuthorName + categories { + edges { + node { + name + } + } + } + seo { + metaDesc + title + } + } + } + } + }` + ); + const edges = data.posts.edges; + allEdges = [...allEdges, ...edges]; + return { edges: allEdges }; +} export async function getPostAndMorePosts(slug, preview, previewData) { diff --git a/pages/authors/[slug].tsx b/pages/authors/[slug].tsx index 83978b4..0ae6144 100644 --- a/pages/authors/[slug].tsx +++ b/pages/authors/[slug].tsx @@ -3,10 +3,8 @@ import Header from "../../components/header"; import Container from "../../components/container"; import { getAllAuthors, - getAllPostsForCommunity, - getAllPostsForTechnology, getContent, - getPostsByAuthor, + getPostsByAuthorName, } from "../../lib/api"; import { GetStaticPaths, GetStaticProps } from "next"; import PostByAuthorMapping from "../../components/postByAuthorMapping"; @@ -59,26 +57,32 @@ export const getStaticProps: GetStaticProps = async ({ }) => { const { slug } = params as { slug: string }; - // Fetch posts from both sources - const postsByTechnology = await getAllPostsForTechnology(preview); - const postsByCommunity = await getAllPostsForCommunity(preview); + // these are users mapped by first name on the username + const usersMappedByFirstName = ["Animesh Pathak","Shubham Jain","Yash Khare"] - // Combine posts from both sources - const allPosts = [...postsByTechnology.edges, ...postsByCommunity.edges]; + // if slug is in above array then take only first name and pass it to getPostsByAuthorName + // else pass the slug as it is to getPostsByAuthorName + let userName =slug + if(usersMappedByFirstName.includes(slug)){ + userName = slug.split(" ")[0] + } - // Filter the combined posts by the given slug - const filteredPosts = allPosts.filter( - (item) => item.node.ppmaAuthorName === slug - ); + const posts = await getPostsByAuthorName(userName) + + const allPosts = posts.edges // Extract postId from the first matching post (if any) - const postId = filteredPosts[0]?.node?.postId; + const postId = allPosts[0].node?.postId; // Fetch content using postId (if available) const content = postId ? await getContent(postId) : null; return { - props: { preview, filteredPosts, content }, + props: { + preview, + filteredPosts: allPosts, + content + }, revalidate: 10, // ISR with 10 seconds revalidation }; };