GraphQL Tutorial #20 - Updating the Resolve Functions
Key Takeaways
Updating resolve functions in GraphQL to interact with MongoDB, using book and author models to grab data from collections
Full Transcript
alright then gang so I've just spent the last five minutes making mutations here from graphical to add the different books to our books collection on MongoDB so now we have these five right here much like we had before and we also still have these three authors right so now we have all of this data stored in our different collections on MongoDB what we can do is return to our resolve functions which were responsible for grabbing the data remember previously we use the local arrays to grab the data now we're going to update those result functions to interact with our two collections in MongoDB the book and the author so we're going to use these two models right here and we're gonna grab the data from there instead right so what I'm going to do in this tutorial is just go from top to bottom in this file and update the resolve functions wherever we find them so then the first one is this it's on the book type and we said whenever someone makes a rig query for a book then they can also request the author as well and we use this resolve function to go and grab that author the author of that book right so this is what we did before underscore defined authors and then any author with an idea which much the parent/daughter ID so this time again we're gonna return something but instead of using underscore on the local array instead we're going to use the author model right here remember this author model is how we interact with the author collection so let's copy that and paste it there and we can use a method called find by ID now this is going to go out and it's going to look in the author collection and it's gonna find a record with whatever ID we pass in here so if I pass in some kind of weird ID it's going to look in our collection our author collection for a record with this ID now obviously this ID doesn't exist but because we have access to the parent I am right here remember the parent refers to the original query of the book we can pass in parents dots author ID so we're looking in the author collection for any book or any record which has an ID equal to this thing right here makes sense right cool so that's all there is to it that so I'm not going to test each one as we go along here because it'll take way too long instead I'm going to go down update them all then we'll test some stuff out from the front end so the next one is this ortho type right here so the result function is going to go out and get a list of books associated with the author again previously we used underscores then dot filter to grab the books this time what we're going to do is use the book model source a return and then book then we're going to use a method called find and what this is going to do is look for all records in the book collection based on certain criteria and this criteria is going to be placed inside this object now what do we want to find books based on well we want the author ID of that book to equal the ID of whatever the parent is right here of the author makes sense much like we did right here so in here we'll say we want the author ID property to equal parent dot ID that's the ID of the author sweet so we've done two of them now okay let's move on to our roots queries down here and the first one is going to be this and it's for a particular book so they're going to pass through an argument which has an ID property and we need to find a book based on that ID so we know how to do this we can just return book dots find by ID so again it's going to look in the book collection for a particular book and we want to pass through args no ID so the ID that the user sent along with the query cool all right let's move on to the next one this time it's for the author so this is gonna look very similar again return this time author dot find by ID will pass in args the ID again alright and then this one is for all books now how do we search our collection for all books well really simply we can just say return book dot find like so and pass through an empty object and when we use the find method without passing through any criteria inside this object it's going to return all of them to us right because they all match so that's how we return all books and then finally the author so return author dots find and then inside an empty object like so all right so we've updated all of those resolved fortunes now we don't need to do these because we did those in the last tutorial our tutorial before so now we're ready to save this and test it out in the front end so let's go to graphical we don't want a mutation anymore instead let's first of all ask for all the books so open your curly braces then we use the books query and from the books we want the name and we want the genre okay let's try that and it works cool we get an array with all of those different books okay so let's say we also want the author of each book and from each author we want the name and the age okay let's play that cool and we get the books and the author of each book as well okay so this is working now let's try getting all the authors to begin with and from the authors we want the name and the age press play cool this is working and now from each also we also want to get a list of books so we'll say we want the name of each book that that author is done as well so play that and we get the books associated with each author as well awesome this is brilliant okay let's do a couple more examples we'll say we want a single book this time with an ID property equal to three and we want the name of that book and we also want the genre of the book and finally we want the author of the book on the name of that author right so let's press play and we don't get anything with an ID of three and obviously that's because we changed the IDS we no longer have ID of three the IDS are these things right here so let's just grab this big ID and place that in here instead okay let's press play now now we get that book the color of magic the genre and the author now if we wanted to nest things further we could do we could say we want the books of the author so all the other books that it's written and say we want the name of each book so press play and we get that property as well this is awesome everything seems to be working we'll do one final query which is going to be for a single author the ID of this author will just grab from here we'll say Brandon Sanderson and we'll paste this dude over here and from this author we want the name and we want the age property oops think the indentation is a little bit off the age property and we also want a list of books by the author and the name of each book so let's give this a whirl and this works as well sweet so there we go my friends now we've updated all of our resolve functions to interact instead with our MongoDB instance and our different collections using our book model and our author model so I want to show you one more thing server-side in the next tutorial but after that what we're going to be doing is adding a front-end as well using react
Original Description
Hey gang, in this GraphQL tutorial we'll go through all of our resolve functions and update them, so that now they interact with our Mongodb instance, instead of local dummy data.
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
Playlist
Uploads from Net Ninja · Net Ninja · 23 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
▶
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
📰
📰
📰
📰
I Gave the Right Answer in a Senior Backend Interview. The Interviewer Changed One Constraint and My
Medium · Programming
Django vs Flask vs FastAPI: Which Python Web Framework Should You Learn in 2026?
Medium · Python
Our Spring Boot API Was Fast for Months. Then Production Data Exposed What Hibernate Was Really
Medium · Programming
Our Spring Boot API Was Fast in Testing. One Hibernate Query Turned It Into 1,247 SQL Queries
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI