GraphQL Tutorial #19 - More on Mutations
Skills:
API Design80%
Key Takeaways
Explains more about mutations in GraphQL
Full Transcript
okay then so in the last tutorial we started adding authors to our instance of MongoDB here to the author's collection now I just want to point out that I deleted those two test ones we did when we created mutations in graphical over here which was shown as Steven and I've just added instead Patrick Rothfuss Brandon Sanderson and terry pratchett so I did that by pressing the Delete icon right here to delete the current records and then just making some more mutations for those authors right here okay so we have this mutation now to add an author but we also need a mutation to add a book so I'd like to do that now in the code under mutation we're going to create a new field after the add author field so let us put a comment after that and then create another mutation called add book so this mutation again means first of all a type and this will be a book type and then we also expect some arguments so again we'll say arcs and this is going to be an object with three different properties this time we need the name the genre and the author ID so let us say name on the type of that name is going to be a graphic of our string so let's choose graph QL string we also need the genre and again that is going to be off type graph cue our string so let's choose that and then finally we also need the author ID so author ID is going to be off type this time graph QL ID right oops capital D at the end all right so now we have these three different arguments that we expect a user to send when they make this ad book mutation because we don't know the name is genre or the author ID the user does right so they're going to send all of those arguments with this mutation now then we need that resolve function so that we can take that data that the user sends with this mutation and actually create new book story in our books collection on MongoDB so much like we did with author up here let us create now a resolve function and inside we take the parent and the arcs and inside the result function create a another local variable call this book let book equal to a new book and remember we can do that because we imported the book model at the top up here right so we're using this book model now to create a new instance of a book and inside the book we need to pass through the different properties so we need to name which is going to be equal to the args name so the name that the user sends along then the genre which is going to be args genre and then finally author ID which is going to be args dot author ID all right so now we have those three properties on our local instance of this new book so now we can take that book remember and we can save it to the database and remember in order for us to get data back after we've made this mutation request we have to return that data right here right graph QL takes care of which properties to send back dependent on what we've requested to receive back in the query or the mutation but all we need to do is return the value that this gives us booked on save so that was simple right so now we've done that let us check out this in the front-end again we're going to make a mutation this time it's called ad book and the name of this book is going to be called the long earth and we don't need an age property but we need as on or a property that's going to be sci-fi and then we also need an author ID property but what is the author ID well when we create new records in MongoDB it automatically gives us an ID right so I'm going to this ID right here because I need not this ID this one because I need terry pratchet so I'm gonna copy that and I'm gonna paste it right here like so alright so I'm saying this is the Arthur ID which corresponds to terry pratchett the name of the book is the longer and the genre is sci-fi now what properties do I want to get back from this ad book mutation when you've made that mutation well I'll take the name back and I'll also take the genre bug now if I press play then I'm gonna see that this works and we get these two things back but has it been saved to our database well let us go into the home first of all screen then go into our database then we'll see that it's creating now a books collection awesome and if we go inside this books collection we can see that we have the longer sci-fi and this author ID matches up to the ID of Terry Pratchett in his record cool so there we go that's our mutation for adding books what I'm gonna do now is just make more mutations to add all the different books to this collection right here but I'm not going to record that you really don't want to see me doing it all I'm going to be doing is adding the different properties right here so we have them to play with as we go forward
Original Description
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 · 22 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
▶
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 Reads
📰
📰
📰
📰
10th Anniversary of the Excelize Open Source, New ersion 2.11.0 Released
Dev.to AI
TCP vs UDP, Explained — Part 1: Building a TCP Server in C++
Medium · Programming
The Session ID That Wouldn't Stop Changing
Dev.to · Michał Iżewski
Stop Data Leaks: Tenant Scopes in Laravel 🛡️
Dev.to · Prajapati Paresh
🎓
Tutor Explanation
DeepCamp AI