Gatsby Tutorial #15 - Multiple Queries
Key Takeaways
Generates pages using Gatsby
Full Transcript
All right, then. So, another thing I'd like to show you when it comes to making queries is how to make multiple queries on one page because sometimes you might want to grab two different data sets. Now, to demonstrate this, what I'm going to do is just add a little bit of data to our metadata. And remember that's in Gatsby-config over here. So, all of this stuff is the metadata. Now, all I'm going to do is add on a contact field, and it's just going to be an email address. So, we'll say me@thewebwarrior uk.com. All right, then. So, let me now save this and open up the terminal. I'm going to cancel out of the process and restart the server using Gatsby develop just because we've edited this Gatsby-config file. So, once we've done that, now I want to make a query for that metadata inside this project index page. But we already have a query right here. I just want to make another query as well as this. Well, we can easily do that. All we have to do is come down below this query right here and add in another one. Now, it's still inside this thing inside these angle brackets. We don't create another query like this. All we do is place another item inside this query right here. So, then, what I'd like to do is this time query site and then site meta data. We've seen all this before. This is how we query the metadata. And then inside that, we just want the contact field. So, now we have two things that we're querying, and this is absolutely fine to do. Now, up here, we're logging the data to the console. So, what I'd like to do is open up the console over here. I'm going to refresh, go to the console, and then we can see this object. This is our data object right here. So, anything that this query gets us. So, if we open up this data object, we can see we have the all markdown remark. That's for our markdown files, and we use that right here to get the nodes, the project. But, we also have this other property now, the site. And if we open up that, the site metadata, and then the contact. So, that's how easy it is to make those two queries in one page. Okay. Now, another thing, we could actually name these parts of the query if we want to. So, what I'm going to do is call this one project, like so, and then I'm going to call this one contact. Now, you don't have to do this. This is just sometimes easier when you're using the data in the component. So, if I come up here now, this is actually not going to work. We're going to get an error. Let me save this and show you. You can see this big fat error over here. Now, I still want to see the object when it's output. So, what I'm going to do is just comment out a lot of this over here, like so, and what I'll do is just a little hello inside here, just so we have some content. I'm also going to comment out this thing right here, just so we can see this data output in the console. Now, if I save this, I'm going to refresh over here. Now, you can see inside this data object, we now have two properties, contact and project. Now, before, that was all markdown remark and site. But, this time, those properties have changed to project instead of all markdown remark and contact instead of site. So, it just makes it a little more clear in our component now which properties we want to get, because it might be the case that we use site twice for two separate queries here, or all markdown remark for two separate queries as well. So, by naming them, we're making it clear what the data is inside the component. So, now if I want to get the project, I have to change this thing right here to project, because it's on the data object, then it's the project property. Inside that, it's still the nodes. So, we still say nodes on top of this. So, I can uncomment this now and uncomment all of this as well. And now it should work. I'll get rid of this hello. We don't need that dude anymore. Save it. Come over here. And now it's all working, right? But we've just called it something different. So, we've called this one project, but I also want to get the contact data as well. So, let me below this say const contact and set that equal to data.contact. siteMetadata. contact. So, if we take a look at the object, that was how we got it. So, it was on the data object, then let me just move this over here. If we open this, .contact.siteMetadata.contact again. This thing right here. All right. So, now we can use that inside our template. I'm just going to output it right at the bottom somewhere down here. So, below this div, I'll do a paragraph and I'll say like what you see. Question mark. Email if I can type that is me at and then I'm going to output the contact like so. For a quote. So, we should now just output that contact data. So, if I save it and preview all, we can see this right at the bottom and it's output that email address. So, there we go, my friends. That's how we query several different things inside a single page.
Original Description
Hey gang, in this Gatsby tutorial I'll show you how to query multiple items / bits of data from a single page component.
🐱💻 🐱💻 Course Files:
+ https://github.com/iamshaunjp/gatsby-tutorial
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Useful playlists:
+ Full React tutorial - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d
+ GraphQL tutorial - https://www.youtube.com/watch?v=Y0lDGjwRYKw&list=PL4cUxeGkcC9iK6Qhn-QLcXCXPQUov1U7f
🐱💻 🐱💻 Other links:
+ Gatsby docs - https://www.gatsbyjs.com/docs/
+ Markdown guide - https://guides.github.com/features/mastering-markdown/
+ Download Git - https://git-scm.com/downloads
+ Get VS Code - https://code.visualstudio.com/
🐱💻 🐱💻 Social Links:
Facebook - https://www.facebook.com/thenetninjauk
Twitter - https://twitter.com/thenetninjauk
Instagram - https://www.instagram.com/thenetninja/
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Medium · Programming
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Medium · Programming
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Medium · Programming
Browser-Based PDF Editing with Vue 3 and pdf-lib
Dev.to · sunshey
🎓
Tutor Explanation
DeepCamp AI