Skip to content

Commit

Permalink
chore: optimize queries for specific user (keploy#73)
Browse files Browse the repository at this point in the history
* chore: optimize queries for specific user

Signed-off-by: Yash Khare <[email protected]>

* handle null exception

Signed-off-by: Yash Khare <[email protected]>

---------

Signed-off-by: Yash Khare <[email protected]>
Co-authored-by: Animesh Pathak <[email protected]>
  • Loading branch information
khareyash05 and Sonichigo authored Dec 11, 2024
1 parent f8e3568 commit 78af7a5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
49 changes: 48 additions & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 19 additions & 14 deletions pages/authors/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -59,26 +57,33 @@ 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);
// Users mapped by first name
const usersMappedByFirstName = ["Animesh Pathak", "Shubham Jain", "Yash Khare"];

// Combine posts from both sources
const allPosts = [...postsByTechnology.edges, ...postsByCommunity.edges];
// Determine the userName based on the slug
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
);
// Fetch posts by author name
const posts = await getPostsByAuthorName(userName);

// Safely extract edges from posts
const allPosts = posts?.edges || [];

// Extract postId from the first matching post (if any)
const postId = filteredPosts[0]?.node?.postId;
const postId = allPosts.length > 0 ? allPosts[0]?.node?.postId : null;

// Fetch content using postId (if available)
const content = postId ? await getContent(postId) : null;

return {
props: { preview, filteredPosts, content },
props: {
preview,
filteredPosts: allPosts, // Ensure filteredPosts is always an array
content,
},
revalidate: 10, // ISR with 10 seconds revalidation
};
};

0 comments on commit 78af7a5

Please sign in to comment.