Hygraph (with Next.js) Tutorial #11 - Fetching User Posts

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·8mo ago

Key Takeaways

This video tutorial demonstrates how to fetch user posts using Hygraph CMS and Next.js by making GraphQL requests, implementing pagination, and displaying related posts in a sidebar.

Full Transcript

Right. So now we fetched the page content, we fetched blog posts and we've also implemented pageionation on the homepage for those blog posts. If we click on a post, we go to the details page for that post where we see the whole thing. And we also see this sidebar section over here which says more posts by Net Ninja and it's completely empty at the moment. So what I'd like to do is make another query now to fetch the latest three posts by the same author and display the titles of those posts as links right here. Then we can click on those and it's going to take us to the details page for that post. So let's head to the high studio first of all as usual and put this query together. And I just want to mention at this point that I've already invited another user to this high project by coming to the project settings down here and then from there going to the team section where you can click on this button to invite new members and you can see right here I've already got that new member listed. So, if I go to the content section now and look at all the posts, you're going to see I've added a handful of new posts by this new user just so we've got some extra posts to play with which are by a different author. And that means we're not always just going to be seeing Net Ninja as the author in the sidebar. Anyway, now let's head to the playground section and try putting this query together. So, then let's start by making a brand new query. And I'm going to call this query posts by author. And this time we don't need pageionation because we're just going to query three posts, right? So let's click on the standard posts thing right here, this option. And now for each post that we fetch, we only need two things. The title, which is what we'll be showing in the sidebar, and the slog to link to that post page. We don't need the summary or anything like that because we're literally just listing the titles. So add those two fields to the query. And now there's two more things we need to do. First, we need to only fetch posts by a certain author name, right? So, let's click on the where filter to say we only want posts where and then we want the created by property and then the name property within that. And we want to make sure that is equal to a certain value, right? And there's a ton of options right here. So, it might take you a while to find this name property, but they're all in alphabetical order, which is nice. Anyway, we can hardcode this name for now. for example, net ninja, but later we'll use a query variable instead. So secondly, we only want to get the first three posts. So let's say click on the first filter and let's use three as the amount. And if we send the query by clicking on the play button, we should only see three posts returned where the name of the author is Net Ninja. Awesome. So let's copy the query now and use it within our code. So then the first thing we're going to do is add a new query to this queries object. And I'm going to do it right down here at the bottom. And we're going to call this post or posts rather by author. Right? And then we'll do a colon and then it has to be the hygraph client.gql again just like we've constructed the rest. Then template strings. And then we're going to paste in this query. I'm also going to format this a little better. All right. So at the moment we have this hardcoded the name but we want to take this in as a query variable. So let's do parenthesis and this is going to be called name and this will be a string then an exclamation mark because it must not be null. Okay. So now we can place the name variable right here instead. So that is the query created. Now let's use this inside the actions file. We'll create a new action for this. So let's say export and then an async function again this time get posts by author and it's going to take in a name variable and inside that we're going to say const response is equal to await queries again and then dot posts by author. Click on this one. Then we'll use the send method and we want to pass through that name variable. Okay cool. So now we just need to return the post. So let's say or rather the posts. So return response.posts like so. All right. So now we've got the server function ready. We can use it inside the blog details page to fetch new posts by the same user. So let's head to that page component which is inside the app folder. Then inside the blog folder, then in the slug folder. And inside this page we're going to import the function at the top which is called get posts by author. And that comes from the same actions file. All right. So now we can use that function down here in the component by saying const post posts is equal to await and it's get posts by author. And we want to invoke that. And we need to pass in the current author name into this as an argument which we have access to in this page from the created by property we get right here when we access the post. So we can just pass in the created by name and hopefully that's going to return us three posts by the same author which we can then output down here in the sidebar. Now I'm just going to delete the current paragraph tag. And then I'm just going to paste in a snippet of code that maps through the posts and outputs a link for each one where the href for the links is made using the slug for the post and the title is the text content for the link itself. Right? So now we're fetching additional posts by the same author as the post we're currently viewing and we're displaying those in the sidebar. Now, before we try this out in a browser, we need to do one more thing, and that is to import the link component at the top of the page so that it can be used in that sidebar. So, I'm just going to copy this from my course files, and then I'm going to come right back up here and paste it in. All right, so now we can try this out. All right, so I'm going to just go to load more posts and click on one of these. And we're going to see more posts by Net Ninja. And we have these three posts right here. So, if you click on one of these, it goes to that particular post. Awesome. Now, if we go back over here and click on one of the new ones, we'll click on read more. This should be Yoshi now. More posts by Yoshi. And now we see Yoshi's posts. So, that's all working. Awesome.

Original Description

In this Hygraph CMS tutorial, you'll learn how to create a simple blog site by hooking it into a Next.js application and make GraphQL requests. 🍿👇 Get early access to the full course on NetNinja.dev: https://netninja.dev/p/hygraph-with-next-js-tutorial 🔥👇 Get access to premium courses with Net Ninja Pro: https://netninja.dev/p/net-ninja-pro/#prosignup 🔗👇 Course files on GitHub: https://github.com/iamshaunjp/hygraph-with-next 🔗👇 Hygraph Docs: https://hygraph.com/docs 🧠👇 Git and GitHub Masterclass: https://netninja.dev/p/git-github-masterclass 🧠👇 Next.js Masterclass: https://netninja.dev/p/next-13-masterclass
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This tutorial teaches how to create a simple blog site using Hygraph CMS and Next.js, and how to fetch and display related posts in a sidebar. It covers making GraphQL requests, implementing pagination, and using query variables.

Key Takeaways
  1. Create a new query in Hygraph CMS
  2. Add fields to the query
  3. Use the where filter to fetch posts by a specific author
  4. Limit the number of posts fetched
  5. Use a query variable for the author name
  6. Create a new action in the actions file
  7. Use the action in the blog details page to fetch related posts
  8. Display the related posts in the sidebar
💡 Using query variables allows for dynamic fetching of related posts based on the current author

Related Reads

📰
The AI Tool I Built for Myself Ended Up Becoming the Product Clients Asked About the Most
Building a personal AI tool can lead to unexpected business opportunities, as seen in the story of an AI tool built for personal use becoming a product in high demand by clients.
Medium · Programming
📰
Can AI Help Plan Your Next Holiday?
Discover how AI can help plan your next holiday, from destination suggestions to itinerary planning
Medium · AI
📰
Beyond ChatGPT: How I Curated a Toolkit to Build an AI-Powered Workflow (And Why I Built a Tool to…
Learn how to curate a toolkit to build an AI-powered workflow beyond ChatGPT and discover the motivations behind building a custom tool
Medium · AI
📰
Beyond ChatGPT: How I Curated a Toolkit to Build an AI-Powered Workflow (And Why I Built a Tool to…
Learn how to curate a toolkit to build an AI-powered workflow beyond ChatGPT and discover the motivations behind building a custom tool
Medium · Startup
Up next
Google Just Dropped A FREE Marketing Agent And It's INSANE (Pomelli)
Income stream surfers
Watch →