GraphQL Tutorial #14 - GraphQL Lists
Skills:
API Design80%
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
What You'll Learn
Covers GraphQL lists for handling multiple data items
Full Transcript
alright then so before we begin I just want to point out that I've added three more books into this book array right here so we have the hero of Ages call of magic and the light fantastic IDs of four five six and author IDs of two three three so this one's Brandon Sanderson down here and these two are Terry Pratchett all right so we have at the minute a kind of one-way relationship going on when we request a book we can say we also want the author of that book and then in this resolve function we're going out and finding that author so we're associating an author with a book but at the minute if we try to make a request it looks a bit like this we're saying that we want an author for the route query and then we want the books of that author we can't currently do it and graph across throwing a wobbler right here say any kind of query field books on type author so that's because we've not yet set this to foot so what we really want to do is allow for that because every author is going to have a list of books presumably so when we query an author we want to return these books and to do that we're going to have to register this books field right here so much like we did down here we'll say books and then specify the type of this now what type is this going to be well you might be thinking it's just book type but not really because remember each author has potentially a whole list of books not just one book and this right here implies that least just a single book what we really want to do is tell graph QL that it's going to be a list of book types so how are we going to do that well we need to grab something else from this graph QL package up here so let's do a comment and we want to grab the graph QL list like so so let's now copy that dude because we're going to use it down here so inside author type where we see the books it's gonna be not type book type bought a new graph QL list and inside book type because it's going to be a graphical list of book types all right so that's the type sorted next up we have the resolve function again we're taking the parent and the arcs so remember the reason of function is there for us to go out and grab the data we need so at this point in time when we've made this request we're initially making the query for the author right so we have that data on this parent object remember so we can use that parent to say okay well I know now the ID of this author is one so I'm going to look through the books array and find every book with an earth ID of one and I'm going to return that to you so down here the way we do that is by saying first of all return and then we're going to use underscore again dot and this time use a method called filter and what filter is going to do is filter through a particular array in our case the books array because that's what we're searching for and it's going to look for objects inside that books array which match up to this criteria right here now the criteria is going to be that the author ID property is equal to the parent ID remember the parent is the initial author we've just requested so for example this could be for ID too so we're taking that ID right here and we're looking in the books array for any book which has an author ID equal to two so it'll look up here say okay well this one's got an author ID of two and this one sum of those two can remain inside the array everything else I'm going to filter out of the array okay so eventually we're just returning the array with these two objects inside which is what we want a list of the books by that particular author so now we've done that let's save it and give this a whirl I'm going to go to the front end and open that up oops and if we try to make this request now in fact first of all I'm just going to refresh the page and we can see that squiggly line has gone because now graph QL recognizes that we can nest this book square it inside the author so I'm gonna press play and we can see we get back Brandon Sanderson and we also get the books associated with that author and likewise we don't need to request the name and the genre could just be the name press play and now we get an array of books with just domain property awesome right so let's try this with a different date number one and we just get to one book because there's only one book by Patrick Rothfuss in that array let's change this to three and now we get three books awesome so now we have this kind of two-way relationship going on we've said that each author can have a list of books and each book has a particular author right now there's one more thing I want to show you before we end this tutorial and that is this thing right here because previously I said that we have to wrap the Fields property inside a function so what is the reason behind them well first of all let's just delete that function so that the fields property is just an object like so and we'll do it down here as well and then we'll run this let's see what happens so if I try to now open up this thing and request then it's going to spin for a little bit eventually it will give us an error type error failed to fetch and if I try to refresh the whole screen over here then it's gonna error out as well so obviously something is going wrong and it says right here that book type is not defined so if we go to the code and have a look obviously the code runs from top to bottom right so it comes to author type right here and it sees this book type and it's saying okay well I don't really know what this is because it's not been defined yet we're defining it down here so that's why we're getting an error so you might be thinking well okay why don't we just define book type at both above author so if we take that stuff and paste it right up here then save it and see what happens now if we try to refresh still not gonna work and if we look in the console this time we get a different error or the type is not defined so right here we're running through the code top to bottom it comes across this off the type and it's same well now I don't know what author type is because author type is not defined until down here so it's kind of like catch-22 changing the order of these things is not going to solve the problem which is why we wrap these things inside a function like so because if we wrap the fields inside a function then what we're doing is still running the code from top to bottom but we're not actually executing this function until some point after the whole of the file has run okay so by the time we do execute the code inside the function it knows what author type is because this has already been run previously all right and vice-versa so I hope that makes a little bit more sense as to why we wrap these fields inside a function
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 17 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
▶
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
More on: API Design
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
Build Secure Authentication System Using Access and Refresh Tokens
Medium · Python
5 PHP Features You're Probably Not Using (But Should)
Dev.to · Mahdyar
How to Use the any Type and When to Avoid It
Dev.to · Krunal Kanojiya
🎓
Tutor Explanation
DeepCamp AI