GraphQL Tutorial #29 - External Query File
Key Takeaways
Externalizes queries in GraphQL by storing them in a separate file
Full Transcript
alright then so in the last video we created this form component right here and we output the authors in the select box you'll notice now I've doing those other random ones that were appearing from M lab so it's just Patrick Rothfuss Brandon Sanderson and terry pratchett now so we have this component kind of visually sorted but obviously at the minute if we type in stuff here and go to add it's not gonna do anything it's just kind of reloading the page right so we need to hook up that kind of functionality eventually to add the data but before we do that I just want to talk about one little thing kind of take a sidestep if you will and that is how we can just externalize our queries so yeah at the minute we're defining our queries right here inside the component files and we do it in add book and also book list but sometimes we might have multiple queries in different components and it might get a bit messy inside these components so what I like to do is sometimes externalize my queries into a separate file so I'm going to show you how we can do that in this tutorial so let's go into source and create a new folder first of all and I'll call this folder queries and inside that folder let's create a new file and call this queries ojs ok so now we have queries GS and remember when we're making kind of queries we need to use that gql from apollo boost so i'm gonna paste this up here this is what we've been using here remember to make these queries right so we're gonna need that in this file because we're going to be constructing those queries here and all we're gonna do is copy from here in fact we'll cut because we don't want it in this file anymore and we'll paste it in query so that was book list and also we'll go to this file where we made this query cut it from there and we'll paste this here as well all right so we still have those queries here we're making them in this separate file and now we need to export them so we'll say export and about exporting multiple things so let's do it in an object we'll say we're export get author query and also get books query and author is plural its authors because we're getting all of them so what exporting both of those things from this file now so since we're exporting them we can access them in the other files so over here in book list we want to get books query so we can now import that by saying import get box query from and it's going to be from out of the components directory so we need to jump out of that by saying double dot forward slash then we need to go into the queries folder and then we say forward slash queries again okay because that's the name of the file query structures so we're importing this get books query from that file so we should still be able to use it just the same as we did before down here should have no kind of difference to doing it this way we can't now get rid of this because we don't need gql in this file we've already imported it right here where we do use it okay so let's save that I'm going to copy this and I'm going to go to add book over here again we can get rid of this gql and below this will import this time get authors query from the same place double dot forward slash to jump out of the components directory then into the queries folder then the queries file alright so again we are importing this so therefore we can use it just the same way down here all right so let's save this now and make sure everything still works on the front end we can see all of those but let's refresh just in case no errors over here we can see all of the books there and we can see all of the authors right here and again let's make sure we have no errors in the consoles as well no we don't awesome so that was just a quick sidestep to show you how we can kind of meeting up our component files so we don't have to keep writing our queries in the components themselves you can do if you want I just like to externalize them keep them in a queries file and then we can require or import them when we need them
Original Description
Hey gang, in this GraphQL tutorial we'll talk about we can externalise our queries into a query file, so as not to clog up our component files.
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 · 32 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
▶
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
⚡
⚡
⚡
⚡
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
5 PHP Features You're Probably Not Using (But Should)
Dev.to · Mahdyar
🎓
Tutor Explanation
DeepCamp AI