Gatsby Tutorial #9 - Site Metadata & Page Queries

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Queries site metadata and page queries in Gatsby using GraphQL

Full Transcript

all right then so an easy way to test out this graphql layer that gatsby create for us in a more useful way that gives us more useful data is to add some metadata to our website and then gatsby is automatically going to take that metadata and add it to the graphql layer no plugins or anything needed so how do we add this metadata well we do it inside this gatsbitconfig file now we've not seen this yet but all it does is export an object where we can add plug-in information later on and we'll see that later but we can also add some science metadata and the way we do that is by adding another property to this object and that is called site meta data like so now it must be camel case this must be capital and this is going to be equal to an object and we can have different properties and values inside this object so i could do a title and i would call that web warrior and then after that i could do a description and that would be something like web dev portfolio like so and then after that i could do a copyright message and that would be this website is copyright 2021 web warrior now you might not have these exact fields in your metadata i just want to do some different properties so we can work with an example but now let's save this file and what we need to do every time we change the gatsby config file is restart the server so i need to open up the console and i need to cancel let me come to the bottom over here cancel out of this process first of all and then i want to run gatsby develop again we need to do this so that gatsby can re-run through this config file and in our case it's going to add this site metadata to the graphql layer so let's wait for this to finish first of all and then now it's done i'm going to come over to graphical and i'm just going to refresh for now so now we've added that metadata you can look in sites and you can come down to the bottom and we see this new property site metadata that wasn't there before and we can see these three properties as well so i could grab all of those and this is what our query would look like for the site metadata and by the way you'll notice this at the top where it says query and then my query this is a name of the query you can call it whatever you want it doesn't really matter and if you don't include this part in your queries it doesn't really matter but there is an advantage to including this part in your queries and that is to do with query variables and we're going to talk about those later on in the course but generally whenever i make queries from my code over here i'm going to take the whole thing like this so anyway let's test this out i'm going to play this and over here we can see this is the data that we get back so it grabs the data site site metadata and all of these properties with the values so this is how we make a query for that data but then how do we actually make that query from our components well there's a couple of different ways that we can make the queries from our components in this lesson i'm going to show you how to make page queries from our page components and then later on i'll show you how to make queries from other components that are not pages as well the first thing i'll do is copy this query right here so all of this and then i'm going to come over to our home page and we're going to make this query from the home page now the way we do this is by exporting the query at the bottom so we say export const and then we'll call this query and we set it equal to graphql and we need to import this so click on this second one down here and it imports graphql from gatsby so we import that so we can use graphql and this is what we use to make a query now we make our query inside a template string so we place template string directly after graphql and a template string is the backticks found underneath the escape key on most keyboards so inside here we can paste in our query now so it's just query my query which we can call something else i'm going to call it site info since that's what we're going to get site info and then we want the site data the site metadata the copyright description and title now i don't want the copyright i just want the description and title for this example so now how do we actually use this data inside our component well gatsby is going to find this query first of all and then it's going to give us access to a data property inside our component on the props so we automatically take in props right here this props object right but what we could do is just destructure from that object a data property and that is any data that we get back from this thing right here this query so what i'm going to do is just log this to the console so we can see it in the browser so console.log data like so now if i save this and come over here i'm going to open up the dev tools and then i'm going to open up the console i'm going to refresh and hopefully we can see this data object right here now so inside that we can see the site property then we can see the site metadata property then we can see the description and the title so we get access to this object and the object mirrors our query this thing right here all of those properties are on this data object so what i'm going to do is destructure the title and the description from the site metadata object so remember we have to say data then dot site then dot metadata and i'm going to destructure the description and the title from that so what i'll say over here is const and then we want the title destructured and also the description like so and then we can set that equal to data dot site dot site meta data so that's this object then dot site then dot site metadata okay let me just zoom in this so we can see it a bit better all right then so now if i go back over here we have access to these two different things the title and description and we can just output them anywhere in our template all i'm going to do for now is a paragraph tag and we'll output the title inside that and then we'll also output the description like so now ultimately we're not going to do this this is not going to be the final design i'm just showing you here how we can actually make a page query so let me save this and come back over here and it says cannot read property title of undefined and that error is because i've misspelled metadata right here so let me save that now hopefully this should work let me just refresh over here yep that's worked now and we can see right here web warrior web dev portfolio so this is the title and this is the description so that has all worked so that's how we make queries from our pages we export the query at the bottom we use graphql which can be imported from gatsby right here we place our query inside template strings directly after graphql then gatsbit is going to make this query for us to the api and whatever data it gets back we can destructure from the props right here and we can use it inside our components but then how do we actually make queries from other components that are not pages like the layout or the navbar we don't do it this way and i'm going to show you how to do that next

Original Description

Hey gang, in this Gatsby tutorial we'll see how to query some metadata from our page components using GraphQL. 🐱‍💻 🐱‍💻 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 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

Related Reads

📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Learn how to prepare for a Frontend SDE-2 interview at Wayfair, including online assessments, machine coding, and system design.
Medium · Programming
📰
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Learn how HTMX rebuilt a React SPA in a week, replacing 2 years of maintenance work, and discover the benefits of this alternative approach
Medium · Programming
📰
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Learn the 5 levels of front end engineering to improve your skills and avoid getting stuck in a career rut
Medium · Programming
📰
Browser-Based PDF Editing with Vue 3 and pdf-lib
Learn to build a browser-based PDF editor using Vue 3 and pdf-lib, enabling users to edit PDFs directly in the browser
Dev.to · sunshey
Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →