GraphQL Tutorial #20 - Updating the Resolve Functions

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

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
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 23 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
▶ 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

Update resolve functions in GraphQL to interact with MongoDB collections using book and author models. Learn how to use find by ID and find methods to grab data from collections.

Key Takeaways
  1. Update resolve functions to interact with MongoDB collections
  2. Use book and author models to grab data
  3. Use find by ID method to grab specific data
  4. Use find method to grab all data
  5. Test queries in GraphQL
💡 Resolve functions in GraphQL can be updated to interact with MongoDB collections using book and author models, allowing for more efficient data retrieval.

Related Reads

📰
I Gave the Right Answer in a Senior Backend Interview. The Interviewer Changed One Constraint and My
Learn how to adapt your architecture design to changing constraints in a senior backend interview
Medium · Programming
📰
Django vs Flask vs FastAPI: Which Python Web Framework Should You Learn in 2026?
Learn which Python web framework to use in 2026 and why it matters for your career
Medium · Python
📰
Our Spring Boot API Was Fast for Months. Then Production Data Exposed What Hibernate Was Really
Optimize Spring Boot API performance by identifying and addressing Hibernate-related issues exposed by production data
Medium · Programming
📰
Our Spring Boot API Was Fast in Testing. One Hibernate Query Turned It Into 1,247 SQL Queries
Optimize Hibernate queries to prevent N+1 query problems and improve API performance
Medium · Programming
Up next
Unlock CRAZY Performance: Native Code Compilation No JNI! #shorts #quarkusinsights #projectpanama
Quarkusio
Watch →