{"id":1970,"date":"2021-02-12T12:00:00","date_gmt":"2021-02-12T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/what-is-graphql-how-to-use-it\/"},"modified":"2026-07-05T03:25:23","modified_gmt":"2026-07-05T01:25:23","slug":"what-is-graphql-how-to-use-it","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/what-is-graphql-how-to-use-it\/","title":{"rendered":"What is GraphQL? How to Use It with Shopify"},"content":{"rendered":"<p>In this tutorial, you will learn about GraphQL and how to use it. This would be useful to you if you have a basic knowledge of HTTP REST APIs. So, GraphQL is normally described as &#8216;a better REST&#8217;. So basically, you make request to some endpoints and get some data. GraphQL does it better. Let&#8217;s see why.<\/p>\n<ol>\n<li><a href=\"#t1\">What is GraphQL<\/a><\/li>\n<li><a href=\"#t2\">How GraphQL Works<\/a><\/li>\n<li><a href=\"#t3\">A Short GraphQL Demo<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What is GraphQL?<\/strong><\/h4>\n<p>According to Graph website, www.graphql.org, <em>&#8216;GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data&#8217;<\/em>. Let&#8217;s break this down! Let&#8217;s take REST API for example. If you want some data, you make a HTTP request to some endpoint, say, http:\/\/myserver\/orders, these are the steps:<\/p>\n<ul>\n<li>send the api request to the endpoint<\/li>\n<li>the endpoint executes a function that connect to the database and fetches the data<\/li>\n<li>the data is sent back to you as a json response<\/li>\n<\/ul>\n<p>For example, the below figure shows the response from a Shopify orders API.<\/p>\n<figure id=\"attachment_14574\" aria-describedby=\"caption-attachment-14574\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-16.05.28.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14574 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-16.05.28-1024x538.png\" alt=\"Problem of overfetching with REST APIs\" width=\"1024\" height=\"538\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-16.05.28-1024x538.png 1024w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-16.05.28-300x158.png 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-16.05.28-768x404.png 768w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-16.05.28-600x315.png 600w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-16.05.28.png 1368w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-14574\" class=\"wp-caption-text\">Problem of overfetching with REST APIs<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Do yo know that what is returned is just\u00a0 a single order data! This is the problem of <em>overfetching<\/em> with REST APIs. You see that mode of the information returned are not needed. GraphQL solves this problem by allowing you to fetch only the\u00a0 data you need, nothing more, nothing less.<\/p>\n<p>We also have the problem of <em>underfetching<\/em>. In this case, REST API response returns field including id field. These id fields relates to other entities. For instance, productId may be returned where you actually need the description and price. GraphQL also solves this problem of <em>underfetching<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">4. How GraphQL Works<\/strong><\/h4>\n<p>As mentioned previously, GraphQL is a query language and runtime. So the queries has to be defined first. Then the runtime has to be setup on a server. Normally, you will find some library or service to achieve this. Here are the steps of getting data from GraphQL<\/p>\n<ul>\n<li>send a query to the GraphQL service<\/li>\n<li>the service validates and executes the query<\/li>\n<li>the query is executed and the data is fetched<\/li>\n<li>the result is sent back to the client as a linked list(or graph)<\/li>\n<\/ul>\n<p>Let me now show you an equivalent of the GraphQL equivalent of the orders API which returns only the data we need.<\/p>\n<p>Query<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">query{\r\n  orders(first<span style=\"color: #333333;\">:<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>){\r\n    edges{\r\n      node{\r\n        id\r\n        name\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Response<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">{\r\n  <span style=\"background-color: #fff0f0;\">\"data\"<\/span><span style=\"color: #333333;\">:<\/span> {\r\n    <span style=\"background-color: #fff0f0;\">\"orders\"<\/span><span style=\"color: #333333;\">:<\/span> {\r\n      <span style=\"background-color: #fff0f0;\">\"edges\"<\/span><span style=\"color: #333333;\">:<\/span> [\r\n        {\r\n          <span style=\"background-color: #fff0f0;\">\"node\"<\/span><span style=\"color: #333333;\">:<\/span> {\r\n            <span style=\"background-color: #fff0f0;\">\"id\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"gid:\/\/shopify\/Order\/3217147494577\"<\/span>,\r\n            <span style=\"background-color: #fff0f0;\">\"name\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"#1001\"<\/span>\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. GraphQL Demo<\/strong><\/h4>\n<p>Let&#8217;s now take an example of working with GraphQL. At this point, I would recommend you also watch the video the see how it works. This demo would be based on a Shopify app that return a list of products and displays them on the console. The products would be displayed with the click of a button.<\/p>\n<p><strong>Prerequisite<\/strong>: You need to have a basic Shopify App. I&#8217;ll update the step by step on this.<\/p>\n<p>Follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create functional component. I call it <em>GetProducts.js<\/em><\/p>\n<p><strong>Step 2<\/strong> &#8211; Import the <em>ggl<\/em> library as well as the usQuery library like so:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> gql from <span style=\"background-color: #fff0f0;\">'graphql-tag'<\/span>;\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> {useQuery } from <span style=\"background-color: #fff0f0;\">'react-apollo'<\/span>;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> &#8211; Add a button to the component and a clickHandler. This function is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">function<\/span> GetProducts() {\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> (\r\n        <span style=\"color: #333333;\">&lt;<\/span>div<span style=\"color: #333333;\">&gt;<\/span>\r\n            <span style=\"color: #333333;\">&lt;<\/span>button onClick<span style=\"color: #333333;\">=<\/span>{showProduts}<span style=\"color: #333333;\">&gt;<\/span>Show Products<span style=\"color: #333333;\">&lt;<\/span>\/button&gt;\r\n        <span style=\"color: #333333;\">&lt;<\/span>\/div&gt;\r\n    )\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Define the following variables in the component<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> GET_PRODUCTS <span style=\"color: #333333;\">=<\/span> gql`\r\n{\r\nproducts(first<span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>) {\r\n    edges{\r\n    node{\r\n        id\r\n        title\r\n    }\r\n    }\r\n}\r\n}\r\n`;\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">let<\/span> data <span style=\"color: #333333;\">=<\/span> useQuery(GET_PRODUCTS)\r\n<\/pre>\n<p>The first variable GET_PRODUCTS is the GraphQL query while the second variable data would hold the response data returned.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 5<\/strong> &#8211; Now write the function to simply log the data to the console like so:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">function<\/span> showProducts(){\r\n   console.log(data)\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong> &#8211; Finally, include the GetProducts component in the index.js component. Then run shopify server. Go to the App page and click on the button. Open the console and see that a list of products is returned. This is shown below:<\/p>\n<figure id=\"attachment_14576\" aria-describedby=\"caption-attachment-14576\" style=\"width: 891px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-18.10.51.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14576 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-18.10.51.png\" alt=\"Response from GraphQL Query\" width=\"891\" height=\"556\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-18.10.51.png 891w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-18.10.51-300x187.png 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-18.10.51-768x479.png 768w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-18.10.51-400x250.png 400w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-12-at-18.10.51-600x374.png 600w\" sizes=\"auto, (max-width: 891px) 100vw, 891px\" \/><\/a><figcaption id=\"caption-attachment-14576\" class=\"wp-caption-text\">Response from GraphQL Query<\/figcaption><\/figure>\n<p>I recommend you watch the video as it makes it very clear.<\/p>\n<p>I would be making a more comprehensive beginner tutorial on GraphQL in the coming days.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about GraphQL and how to use it. This would be useful to you if you have a basic knowledge &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1970","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1970"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1970\/revisions"}],"predecessor-version":[{"id":2138,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1970\/revisions\/2138"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}