BACK

how to use shopify graphql

Getting Started with GraphQL on Shopify we've talked about graphql multiple,times on this channel bu

WeeklyHow

Updated on Mar 03,2023

Getting Started with GraphQL on Shopify

The above is a brief introduction to how to use shopify graphql

Let's move on to the first section of how to use shopify graphql

Let TThunt's experts help you find the best TikTok product on your Shopify business!

Find TikTok products (It's Free)
No difficulty
No complicated process
Find winning products
3.5K Ratings

WHY YOU SHOULD CHOOSE TTHUNT

TThunt has the world's largest selection of TikTok products to choose from, and each product has a large number of advertising materials, so you can choose advertising materials for TikTok ads or Facebook ads without any hassle.

how to use shopify graphql catalogs

Getting Started with GraphQL on Shopify

we've talked about graphql multiple,times on this channel but we haven't,completely covered what is graphql in,shopify as well as the tools that you,can use to test graphql queries and,mutations so let's do that after this,intro,so let's start this video by answering,the question what is graphql graphql is,a query language for apis and a runtime,for fulfilling those queries with your,existing data in simpler terms graphql,gives you the ability to retrieve or,modify an existing data from an,application by making queries or,mutations so with that said you're,probably familiar with rest it's another,way to ask or modify data from an,application one of the differences,between graphql and rest api or rest is,the amount of api endpoints with rest,you have this all you have this api,endpoints like product api web hook api,script tag api the list goes on whereas,with graphql you only have this one api,endpoint where you can send your http,request so with that said graphql is,better than rest for known reasons and,one of the reasons is that graphql,prevents overfetching or under fetching,which means for each request you get the,exact data that you're looking for in,rest most requests do over fetch or,under fetch let's use the shopify api as,an example let's say we use the,following api endpoint by using this,endpoint you get all of the products,from your shopify store including their,properties and fields or fields so what,are these properties these are the id,the title the body html the variants the,images and so on now what if you only,need the id and the title of the,products that's an example of over,fetching since we don't really need the,other properties and having these,properties that we don't need can be,very inefficient not just for our,application but also for shopify in,contrast to over fetching under fetching,is when you don't get enough data from a,single request let's use the same api,endpoint as an example let's say we want,to get the images of the product,variants so by using the product api,endpoint you can get the variance of the,products unfortunately there are no,images filled in the variance field so,we're going to do is to create another,request or http request to another api,endpoint by passing the product id as,well as the image id so you see you had,to make two requests just to get the,images from the variant api and that is,very inefficient this is where graphql,comes in you create the query and then,in this query you specify the data that,you only need this is an example of a,graphql query you create an object and,then specify the fields that you want to,get now i mentioned object and that's,because if you notice the structure of,this object or this example kind of,looks like a json object right you get,the main object then inside of it it's,nested by another object then finally,the data or you can say fields this is,how query is a structure which makes it,a little different compared to rest api,however with graphql we have full,control over our http request or queries,and mutations so you see we not only,avoided under fetching and over fetching,but we also avoided making multiple api,calls now where do you send this graphql,query language data you can send this,query to a graphql endpoint like this as,an http post request now that i,mentioned post you're probably thinking,right now where are the get,post put and delete in graphql there are,only two actions that you can make query,and then mutations query is the,equivalent of get this is what you're,going to use if you want to retrieve,something from shopify api server,whereas mutation this is the equivalent,of post put and delete this is what,you're going to use or mutation is where,you're going to use if you want to,modify or delete the data from the,shopify server okay so now that we know,what is graphql let's learn about the,tools that you can use to make graphql,request the first tool that you can use,is curl if you're a big fan of curl,commands or if you want to use php you,can check out the following video where,i showed you how to use,curl to make graphql queries or,mutations i'll put its link in the video,description below or you can check out,the icon right over here the second tool,that you can use is the graphical ide,it's an ide or integrated development,environment made for testing out graphql,endpoints luckily for us we do have a,graphical explorer that we can use to,play around and test graphql queries,this is shopify admin api graphical,explorer this is where you can try,making queries but unfortunately you,cannot do mutations in this app or in,this explorer if you want to build not,just queries but also mutations you will,have to install the graphical app to,your shopify store or shopify,development store okay so we can just do,it in the future but for now let's just,focus here in the example that we have,in the explorer here we have the,following field called shop and we are,using the property name if we execute,this query using display button right,over here we will receive the following,json response with a data property and,in this data we get the shop and the,name of the shopify store now notice,that the structure of the json response,matches the structure that we set up,here in the left side of the explorer so,if you add another property here say for,example we get the id of the shop you,will receive the json response with the,same structure now let's try to use the,example that we mentioned in this video,let's use the product api here use the,products field alright so before we,continue there are times that a query or,a field will ask you for an argument to,pass an argument you will have to create,a set of parentheses then inside of this,parenthesis you will have to supply an,argument so for example let's use the,first now shopify provides a very good,documentation about queries and,arguments so if you'd like to learn more,about it i highly recommend you check it,out i'll put its link in the video,description below or you can check out,the,no you can just check out the,description below though as soon as you,open the parenthesis the graphical app,will also provide you suggestions like,so and if you hover a field or an,argument it will give you the following,card where you can click the same field,or argument and it will open this,documentation this this documentation on,the right side all right so going back,here the first argument asks for an,integer value so if you hover this,argument as you can see like i said it,gives you the following card it's asking,for an integer so we can just give this,um like a number like five,so what does this mean this mean that we,want to get the first five products from,our shopify store,that's the first argument if you want to,use the last you will give it the last,five products from your shopify store,okay so like i said you can check out,the documentation of this argument i'll,put its link in the video description,below now since we're using a plural,field products that's plural we're going,to need to use the edges and then node,for pagination i'm going to create a,separate video for pagination because,that will just make this video very long,it's already long anyway so inside of,this we can set up the edges and nodes,and then inside of the node field we can,start getting the data or the yeah the,data that we need so say for example um,let's use the id and then the title of,the product,now if we execute this query we should,have the following json response and,that's pretty much it this is the first,video for graphql series in the next,video we will learn about graphql,mutations and we will also learn how to,install the graphical app to a,development store if you enjoyed this,lesson let me know by hitting the like,button and sub to the channel so you,won't miss our future uploads now once,again thank you for watching and i will,see you next time,bye

Congratulation! You bave finally finished reading how to use shopify graphql and believe you bave enougb understending how to use shopify graphql

Come on and read the rest of the article!

Browse More Content