GraphQL Tutorial #33 - Re-fetching Queries
Key Takeaways
This video tutorial demonstrates how to re-fetch queries in GraphQL to automatically update a component without refreshing the page, using Apollo and the refetchQueries option in a mutation.
Full Transcript
alright then so we've hooked up now this farm so that when we add a new book then it's gonna add it to the database in M lab but when we add a new book it's not automatically updating here so for example if I add a new book called thawed genre fantasy and select author Terry Pratchett all these books of fantasy by the way except for one I think which is sci-fi anyway if we add it now then yeah it's gonna send that mutation to the server and that's going to add a new record to the database but it's not updating here right so we want that to occur so let's have a look in ml of first of all and refresh over here to see if that record has indeed been added yes it has third fantasy and the author ID is right there but it's not appearing here until we refresh the page now that in my eyes is not great so we need to figure out a way to refetch this query so rerun the query to update the list when we've added this book when we've made that mutation so let's have a look at the code because there is an easy way we can do this so if we open up the clients folder and then go into components and then it's book list this is the component that's rendering those books and the query that we're using is the get books query that is what is retrieving all of those books the list of books right so what we really want to do when we add a book is rerun that query we want to go out and get those books again so that the component will rerender with the added book makes sense right now there is an easy way we can do this if we go to add bulk when we add a book here by using this mutation what we can do is tell graphically well now or Apollo to go and refetch a particular query so we can do this by adding on a comma after variables right here and then same refetch queries right and this is going to be an array of different queries that we want to research so each item is going to be an object and it's going to have a query property now we need to say which query we want to research after this ad book mutation has fired now the query we want to refract refetch is going to be this get books query so let's copy that and paste it right in here but this is not going to work just yet because we've not imported this into this file so if we try to run this file now it's not going to know what this thing is so let's import it up at the top it's from the same place as the other queries right here so we'll paste it right in there so now hopefully when we make a mutation it's going to research this query right here and then therefore have more data in the props of this component and therefore re-render the component with that new data but let's give this a whirl in the browser just to make sure all right I'm going to refresh first of all then for the book name I'm gonna say the long universe I think that's a sequel to this but I'm not sure the genre is going to be sci-fi makes a change and the author is gonna be Terry Pratchett again alright so if I press add now then fingers crossed hopefully we'll see something appear here so yep we do awesome now it's appearing here because what we're doing is after we run this mutation is we're saying okay now refetch the query that is responsible for this component refetch that data it reflexes the data figured out it's got more data available to it on the props and it rear Enders that component so we now see the new book here at the bottom awesome so we've taken another step forward here in the next video we're going to take this one step further and we're going to create a book details component which appears when you click on one of these different books it's going to show you details over that particular book
Original Description
Hey gang, in this GraphQL tutorial I'll show you how we can re-fetch queries to automatically update a component without have to refresh the page,
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 · 36 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
▶
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: Backend Performance
View skill →Related Reads
📰
📰
📰
📰
Designing Better Backend Systems for Data That Cannot Be Lost
Dev.to · Marc Keebler
DNS Record Deletion in Node.js: List Identity Before Delete
Dev.to · QuintonShaw1483
Compile HTTP Error Indexes From Exception Classes; Humans Own Recovery Copy
Dev.to · Avery Lin
JavaScript Map: Store and Find Values Using Keys
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI