GraphQL Tutorial #8 - Root Query
Key Takeaways
Explains the root query in a GraphQL schema and how to set it up
Full Transcript
all right they're my friends so right now we're making this schema file and remember the schema file has three responsibilities for our sake right now the first one is to define types and we've already done that we've defined a book type right here all right the second is to define relationships between types now we only have one type at the minute so we don't really need to define any relationships because a book type can't really have a relationship with itself so we're going to jump on to the third task of the schema file which is defining route queries and route queries are how we describe that a user can initially jump into the graph and grab data so if we just refer back to this diagram again which I keep showing you these things right here when we jump from the front end into the graph they are the route queries how do we initially get into the graph to grab data so we're going to define these require ease inside the schema file so let us now down here create a new constant and call this routes query and we're going to set that equal to a new graph QL object type like we did above up here and then inside we're going to pass through an object again and this is also going to have some parameters first of all the name is going to be route query type like so and then also we're going to define the fields of this route query type now each one of these fields is going to be a type of requiring if you like so remember when we were looking at real queries we said okay well one particular requiring could be to jump in to grab a particular book another type of route query could be to jump in to grab a particular author and another one could be to jump in and grab all books and another one could be to jump in and grab all authors so there's four kind of basic route queries there that I want to set up and each route query each one of those is going to be a different field inside this route query type so we don't need to wrap this fields object right here or these fields per inside a function like we did here because we don't really need to worry about the order so much in our case inside this root query so the first type of root query I want to do is to query for a particular book so I'm gonna call this book and this name matters because when we're trying to query from the front-end remember before when we said we want to grab all the books and then inside we want to grab the name and you know the genre something like that this thing right here this is the name of this parameter now if we call this book then when we're making a query from the front-end our query is gonna be called book at the start like this okay so that's going to be the name of this query right here and inside we're going to give this a type and the type of data that we're querying is going to be this thing a book type so let's copy that dude and paste it down here and then the next property is going to be any kind of arguments this is called arcs now let me just type this out then I'm going to explain it so ID and the type is going to be a graphic URL string so we'll pass that in here like so okay so what we're doing here well we're saying when someone queries this book tight right here when they want to make this query for a particular book then I expect them to pass some arguments along because I don't know you know automatically what book they want to query right is it a book id-1 id2 ID 4 ID 10 I don't know so I want the user to pass that argument along when they're making that query right so we're defining which arguments should come along for the ride inside this query when they're looking for a book and we're saying okay well the argument or one of the arguments should be an ID property alright and the type of this ID property is going to be a graph to our string much like we defined the ID up here that was a graph to our string so we expect this ID to come along inside the query and the way that kind of looks from the front end is by making the query this book and then parentheses and then ID and then whatever the ideas right and then inside we want the name and we want the genre of that book so this thing right here this ID argument is passed into the query that's what we're expecting and it's of type string okay and that is how we find the book inside the graph QR server that's how it determines which book to land on on which book to return so let's get rid of that we're saying here these are the argument it's just one argument which is the ID then finally we have the resolve function right here now the resolve function takes in two parameters which I'm going to call parent and arcs and this resolve function this is the function where we write code to get whichever data we need from our database or some other source so let's just put a little comment in their code to get data from DB for slash or the source all right just so we know what this function is doing so this parent thing right here this will come into play when we start to look at relationships between data the arguments right here is this thing right here so if the user passes here an ID of one two three then we have access to that ID right here on the arguments parameter so in our case there's going to be an Arg dot ID property because we defined this ID argument right here that we're expecting we have access to this and we can use this ID then to go out and query our database to grab that book with that ID does that make sense now we'll look at the resolve function in more detail in the next tutorial but for now what I'd like to do is export this schema right here so that we can use it as a property inside this thing right here so let us now say module exports and set that equal to a new graph QL schema I'm gonna have to grab that at the top up here so I'll say graph QL schema so what we're doing is creating a new graph cue our schema here and inside the graph Kowalski man what we're going to do is pass through our initial route query okay so what is going to be the query of this schema so the query is going to be the route query that we defined so a lot of what we've written here might seem like gobbledygook and you might not understand it all but essentially we're going to be doing this kind of thing over and over again and it's going to get much easier to understand as we got forward but just to go over it one more time what we've done is we've grabbed all of these different properties from the graph QL package then what we've done is defined our first object type and that is a book type this book type has different fields ID name and genre which are wrapped inside this function and we'll explain that a little bit later on but anyway we're defining that book type then down here we're defining this root query which is how we initially jump into the graph and these fields right here these are all going to have the different options of how we can initially jump into the graph these different queries if you like so the first one that we've created is this book field and this book field is saying okay when someone queries a bulk then I want you to use this thing right here to go and get it now the type of data we're looking for here is a book type and the arguments that I'm expecting to come along with the query is an ID which must be a graphic URL string so we're expecting a string which is an idea to come along with this query then what we're going to do when we receive that query is we're going to fire this resolve function this result function is going to take in a parent parameter which we'll explore later on we don't need it yet and also this args parameter which is going to contain the ID field that the user sends along with the query so when this fires down here this resolve function we're going to have access to that argument that ID and we can use it to grab the book that the user wants from a database or some or the source all right so this is our schema so far we've defined an object ID and how we can reach in and potentially grab one of those objects so now what we're doing is we're creating a new schema right here graph QR schema and we're passing some options into the schema and we're defining which query were allowing the user to use when they're making queries from the front-end and we're saying okay well that query is going to be this route query right here all right so now we have this scheme it over here and we're exporting it we can use it as a property over here so let's first of all import it by at the top saying Const and then schema is equal to require and then this time what we need to do is say dot forward slash schema to go into the schema folder and then forward slash schema for the schema file all right so we're importing that and now we can use it inside this middleware so we'll say the schema property is going to be equal to the schema that we just introduced over here but using es6 we can shorten this so it's just schema because both names are the same all right so now we have that schema inside this middleware right here and the next thing we need to do is go into this resolve function right here to actually tell graph QL how to go out and get that data when someone makes a request
Original Description
Hey gang, in this GraphQL tutorial I'll go through exactly what the root query is and how we can set one up in our GraphQL schema.
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 · 11 of 60
1
2
3
4
5
6
7
8
9
10
▶
12
13
14
15
16
17
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
Related AI Lessons
⚡
⚡
⚡
⚡
Applying Scalability in Backend (CodeBuddy)
Medium · LLM
Why Every Backend Developer Should Learn Nginx Before Going to Production
Medium · DevOps
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
Build Secure Authentication System Using Access and Refresh Tokens
Medium · Python
🎓
Tutor Explanation
DeepCamp AI