GraphQL Tutorial #35 - Making a Single Query

Net Ninja · Beginner ·🔧 Backend Engineering ·8y ago

Key Takeaways

Demonstrates making a single query in GraphQL

Full Transcript

all right there so we have at this query right here called get book query and the idea of this is to go and get a single book based on this query variable the ID right here now we've bound this query to the book details component down here but at the minute it's not really doing anything because we've not passed that query variable to it so it can't go out and query that book for us so what we need to do is find out a way that when we click on one of these items we take the ID of that book pass it in to the book details component then attach that ID as a query variable to the query so we can go and get the details of that book and display it right here sounds complex but it's not but there's a couple of different steps to doing this first of all in this book list component we need to listen out for when a user clicks on one of these items then we need to find out the ID of that and pass the ID as a prop down into this component right here when we have it is a prop in this component we can easily use that prop as a query variable when in the query associated with this component to go out and guessing a book so let's start this process the first thing we need to do is go into the book list component and attach an event listener to each Ally that is output to the screen so let us do an on click event and set it equal to something now what do we want to do inside this function well it's first of all it's going to take the event as a parameter then we want to update the state to keep track of what book has been selected using the ID of the book now to do that first of all we'll do our constructor up here so we'll say constructor and it takes in the props and then inside we'll do super props and then under that we'll say this dot state is equal to an object and we'll use a property called selected and set it to null originally so this selected property right here is going to keep track of the ID of whichever book we've clicked on right to begin with it's not because we don't click on a book to begin with when it first loads so when we click on an individual item what we would like to do is say this dot set states then we want to set the selected property on the state equal to the book ID remember we have access to that single book right here okay because we're mapping through them so well updating the state right here to match whichever book we've clicked on right then what we can do is pass this selected property down as a prop into the book details so we could make up a prop name here I'm going to call it book ID and set it equal to this dots state dot selected all right so whenever we click on a new book now it's going to update the selected property so that it becomes the ID of that book then when that changes it passes the new value down as a prop into the book details component so the prop on the book details component now is going to be updating as we click on different books so we can see that if I in the render function just console dot log this dot props we're going to see update as we click on different books so let's just scoot over here and click on a book and we can see now that this book ID right here this prop is on the props object and we can see it's 5a all this random string if I click on a different one it's gonna change now it's 5a be something the long universe etc okay so they're all changing now we're keeping track of that book ID and passing it down as a prop in to this component now so now we have that prop this book ID in this component we can use it to attach it as a query variable to this thing right here so how do we do that exactly well it's pretty simple all we need to do is pass through a second parameter right here and do an options property and this options property is going to be a function which takes in the props as a parameter so whenever we update the props or a new prop comes in here we're going to refire this function so this function is going to set the variables or rather it's going to return an object and it's going to set the variable for this query and the ID variable is going to be drops dots book ID so whenever that Prop updates this is going to rerun and reset the variable for this query alright so if we now console.log this dot props then we should hopefully see the data that comes back from that query so let's go over here refresh and if I click on a book then I'm going to see this thing over here so we have the book ID we also now have this data property which is for the query itself and if I open up that data property we can see the book that's been returned so we have genre the ID the name color of magic we also have author information including all the different books that that author has written as well awesome so now what we're doing is successfully taking the idea of each book when we click on it passing it as a prop then using that ID on the props as a query variable so it's attaching itself to the query we're going out grabbing that book returning it to ourselves and locking it to the console at the minute so that's cool now we have access to the book inside this thing racking inside this component so what we can do is check for when that book exists right one doesn't exist to begin with when we first load the page but when we click on something a book will exist so we can check to see when that book exists if it does we output the details of the book if it doesn't then we output something else like no book selected so how are we going to do that well I'd like to create a function to control this kind of output of HTML so I'll call that function display book details alright so inside this function first of all I'm going to say Const and we're doing a bit of D structure in here and I'm going to grab the book variable from this dot props data all right so this is just ear six destructor it is the same as saying cons booked equals this dots props dated up book I'm just using es6 destructor in to grab that book property from this dot props top data right so now we need to check if that book actually exists right so we can say if book now if this returns true if this passes the evaluation then we do have a book if it doesn't then we don't so if we do have a book then we want to say return and we're going to return some HTML here first of all a div and we'll close that off then inside we're going to output information about this book that we've just grabbed okay so the first thing we want to output is the book name so we'll do the book name in h2 then close that off the second thing you want to output is going to be the book genre so let's do that as well book genre and close that P off then we'll output the author name maybe so again book dot author then we need to say dot name because this author property is nested on the book and we have a name property on the author so close that off and then I'll also output the other books by the author so I'll say all books oops not in capitals all books by this author like so and then underneath I'd like to output all of these books inside a you app so the way you out and then this is gonna have a class name equal to other books this is just for stylistic purposes later on when we use a bit of CSS on the app so inside the URL we need to output all the different books that this other author has done so how are we going to do that because we have booked authored books and that is an array of different books so what we could do is map through those books and output some Li tags for each one of them or one Li tag for each one of them rather so let's open up our curly braces to do some code then we're gonna say book dots author dot books dots map and inside we're going to take each item as we map through the array and fire a function and inside the function we're just going to return an li tag and it's going to have a key equal to item dot I D so that's the book ID of the item then we're going to output the item dots name right here and we'll close the Li tag off cool so let's just scoot this back up here now alright so now we're outputting the data of that book if it exists if it doesn't exist we want to output something out so let's do else and then say return and in brackets we'll just return something like a div and then no book selected all right so div again then we need to call this function so let's copy that and paste it down here right so and we need to say this dot display book details all right so now when we call this function what we're doing is we're first of all grabbing the book property from this props data from the data that comes back from the query if that book exists right if we have a book available to us then we're returning this content right here where we're at put in the name the genre the author name and the other books and we're outputting the other books by mapping through just like we did with previous things in previous tutorials if we don't have a book then we're just outputting this div with no book selected inside it so let's save this cross our fingers and hope it works no it doesn't we get an error and that's because stupidly we've placed this display book details inside the render function how daft can you get so let's take all of that and scoot it out of here do we need that as well I think we do so let's grab that so let's take it out of the render function and put it above the render function and this should now work so let's have a look in a browser okay cool so if we click on a book now we can see currently it says no book selected if we click on a book then we get details about that book and the other books by the author that's awesome all right and every time we click on a different book it updates cool so there we've got my friends now we can successfully add new books we can make those mutations we can list the box and update them as we add them and we can display the details of a book when we click on one so in the next video I'd like to take a few moments just to make this app look a little bit better because it looks pretty cruddy at the minute so we'll get our CSS wizardry on in the very next tutorial

Original Description

Hey gang, in this GraphQL tutorial I'll show you how to make a query for a single record in our database. DONATE :) - https://www.paypal.me/thenetninja ----- COURSE LINKS: + Course files - https://github.com/iamshaunjp/graphql-playlist + Atom editor - https://atom.io/a + CMDER - http://cmder.net/ + Node Download - https://nodejs.org/en/ ======== Other Tutorials ========= ----- NODE.JS TUTORIALS https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp ----- MONGODB TUTORIALS https://www.youtube.com/playlist?list=PL4cUxeGkcC9jpvoYriLI0bY8DOgWZfi6u ----- REACT TUTORIALS https://www.youtube.com/playlist?list=PL4cUxeGkcC9i0_2FF-WhtRIfIJ1lXlTZR ----- SUBSCRIBE TO CHANNEL - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1 ======== Social Links ========== Twitter - @TheNetNinja - https://twitter.com/thenetninjauk Patreon - https://www.patreon.com/thenetninja
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 39 of 60

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
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

📰
HTTP Status Code Reference – All 1xx–5xx Codes with Descriptions & Use Cases
Learn about all HTTP status codes from 1xx to 5xx with descriptions and use cases to improve your web development skills
Dev.to · Dev Nestio
📰
CodeIgniter 4 vs Laravel — When to Choose Which (From a Dev Who Uses Both)
Learn when to choose CodeIgniter 4 vs Laravel for your PHP projects, based on a developer's 12+ years of experience with both frameworks
Dev.to · sunakshi Thakur
📰
The Only Git Commands You Actually Need — 47 Patterns for Daily Work
Master the essential Git commands to boost your development productivity
Dev.to · The AI producer
📰
Common Next.js Errors (and How I Solved Them)
Learn to troubleshoot common Next.js errors and improve your development workflow
Dev.to · gary killen
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →