GraphQL Tutorial #8 - Root Query

Net Ninja · Beginner ·🔧 Backend Engineering ·8y ago
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

What You'll Learn

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
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 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
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
23 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

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →