Strapi Crash Course (with React & GraphQL) #8 - GraphQL Plugin & Overview

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Sets up GraphQL with Strapi using the GraphQL plugin and provides a GraphQL refresh

Full Transcript

all right then gang so far we've been using strappy's rest api and endpoints to fetch data using the use fetch hook and that's completely fine if you want to work that way but now i want to take a side step and i want to show you how to use graphql with strappy instead to query and fetch our data and how that works inside our react application so to begin with we need to install the graphql plugin for strappy so head to the admin area and then into the marketplace over here then click on the download button next to graphql this should download and install the graphql plugin for this instance of strappy now sometimes this takes a little while to install and you might get a message saying something like the server is taking a long time to restart or something like that if that happens don't worry just sit it out and it will finish after about a minute at most if it doesn't install after about three minutes just try refreshing the page and installing it again to see if it works so then now that's installed for us strap it is set up and ready to work with graphql now behind the scenes it's generated all the graphql scheme as it needs to run on the server for our content types and you can see them in the export folder inside graphql right here so this contains schemas for our content types the user content type and other things that strappy uses and this is what strappy will use to handle our queries when we use graphql to fetch data now before we start making queries using graphql in our application i just want to cover the basics of graphql for those that need a refresh or an introduction and to do that we can use a graphql playground tool that strappy gives to us to test out graphql queries it's a tiny bit like postman but for graphql instead now to see that tool open up a browser tab and go to localhost port 1337 then forward slash graphql and hit enter so then over here on the left is where we write our queries then we press the play button to run them and the response is going to appear on the right over here now if you don't feel like you need a refresh on graphql then by all means feel free to skip to the next lesson but for the rest of us let's try out a few simple queries so my friends graphql is a query language it gives us a different way to query data from a server than a rest api in a rest api we would use something like fetch or axios to make requests to a specific endpoint that sends back a bunch of data right instead in graphql we construct what's known as queries to be very specific about the data that we want to get back from the server and it's much more flexible than using a rest api and it's really helpful when you start to work with relational data as we'll see later on in the course so what does a query look like well typically we start it with the query keyword and then we give our query a name which i'm going to call get reviews and then after that we need to open up some curly braces and the actual query what we want to get goes inside these curly braces so all we need to do is say what data we want what types of data now i want to get all of the reviews so i just say reviews and this name right here must match the content type that we have now that on its own is not enough we also have to specify what fields from the reviews that we want to get so we open our curly braces again and specify those inside for example if i just want the title field of each review i just say title and now this is a completely valid query so i could press play now and we'll see the response from strappy on the right and we can see that it's a json object with a data property now inside that is a reviews property which is an array of reviews each review is an object and the only property inside is the title which we specified we wanted so now we have a list of reviews with just their titles if we want more fields we have to specify them in the query so after title i might want also the id so i comma separate these i could do the id and also after that the rating as well so now i'm saying get me all the reviews of each one get me the title the id and the rating and now if we press play strappy sends us back the list of reviews with the title id and rating of each review included so this is really cool graphql allows us to ask for only the data fields that we want i can also add the body field into this if i wanted to so let's do that at the end and then once we've done that we can hit play and this time the body field is added to the response that we get back so this is the kind of query we'll be making soon from our react application to get the data we need now just quickly if you want you can take away the query keyword and the name that we give to this query and we just leave the curly braces to surround the rest and this will still work we don't technically need the query keyword and the name of the query but i will be using them because they're going to allow us to later on use query variables which is a way to pass dynamic values into a query and we'll see how they work later on anyway that's a quick overview of the basics and how to make queries we will be diving deeper into these queries over the coming videos and some of them will become a little bit more complex especially when we start to work with relational data the next step though is to set up our react application to be able to make these types of graphql queries and we're going to do that in the next lesson

Original Description

In this tutorial we'll see how to set up GraphQL with Strapi using the GraphQL plugin & also have a little GraphQL refresh. 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/Strapi-Crash-Course 🐱‍👤 Get access to extra premium courses on Net Ninja Pro: https://netninja.dev 🐱‍💻Starter Project CSS (lesson 5): https://github.com/iamshaunjp/Strapi-Crash-Course/blob/lesson-5/frontend/src/index.css 🐱‍💻 Full React Course: https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
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
11 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 Reads

Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →