{"id":19527,"date":"2021-08-19T10:13:25","date_gmt":"2021-08-19T10:13:25","guid":{"rendered":"https:\/\/www.cmarix.com\/blog\/?p=19527"},"modified":"2026-04-14T12:00:44","modified_gmt":"2026-04-14T12:00:44","slug":"how-to-implement-custom-graphql-in-magento-2","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/blog\/how-to-implement-custom-graphql-in-magento-2\/","title":{"rendered":"How to implement Custom GraphQL in Magento 2?"},"content":{"rendered":"<p>Hire magento developersand create Custom GraphQL in Magento 2. GraphQL is a query language for API to allow the customers to fetch the required data while the server returns only the required information. GraphQL was introduced in Magento 2.3 to replace REST and SOAP API. GraphQL is used to read data by query and write data to the server through a mutation.<\/p>\n<p>Here we&#8217;ll discuss How to Create Custom GraphQL in Magento 2.<\/p>\n<p>Let&#8217;s get started:<\/p>\n<h2 class=\"wp-block-heading\">Steps to Build Custom GraphQL in Magento 2:<\/h2>\n<h3 class=\"wp-block-heading\">Step 1:<\/h3>\n<p>Magento Development Company create a &#8220;<strong>schema. graphics<\/strong>&#8221; file inside our extension below.<\/p>\n<p><strong>app\\code\\Vendor\\Extension\\etc\\schema.graphqls<\/strong><\/p>\n<p>Now, add the below code<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/ugAOaIfgWFG8vuHbN_Wj6Z9F2rPlr-qffnt0CIDFXrYuJ085hC5yhL-wrrl4KD-BsKNno5oV3UsIiwH-lUgSU6X9FBXCD5Ol4mNRibNxXpla06CzKmLakDg\" alt=\"Now, add the below code\" \/><\/figure>\n<h3 class=\"wp-block-heading\">Step 2:<\/h3>\n<p>After that, we need to create a &#8220;<strong>CustomGraphql.php<\/strong>&#8221; file inside the below folder path of the extension.<\/p>\n<p><strong>app\\code\\Vendor\\Extension\\Model\\Resolver\\CustomGraphql.PHP<\/strong><\/p>\n<p>And add the below code<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/aR9NaM8wZJkKfVBLYjSlf3f0Pz7ZcvlhHEjq-n-83PsOg3OwcrDX27eyDkI9zhEmEaVPm-spNxtEOEeBdRBJ1nKiofCSnXu-dLdU_3lVg3AJHWccXjNHsJY\" alt=\"And add the below code\" \/><\/figure>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/HPY11LlLhS33uMlpWU-EyJ_QlXTIGUV1hTAlfWGf0Q0CpRPS4RBoS1oteGME1XZ_7q5FpXWZfq47uDm0q6K50gPLPeQyfEWVs0QRzRSYh1bt0y5m3mI7npY\" alt=\"And add the below code\" \/><\/figure>\n<p>You need to test GraphQL Query in Altair GraphQL client chrome extension or test in Postman Software.<\/p>\n<h2 class=\"wp-block-heading\">GraphQL Compared to REST API<\/h2>\n<p><a href=\"https:\/\/www.cmarix.com\/blog\/how-to-ensure-rest-api-security\/\">REST API<\/a> characterizes asset data on the worker; the customer settles on the decision as it were. GraphQL permits customers to transfer a datasheet; worker activities should return a similar data.<\/p>\n<p>The GraphQL worker just requires a single endpoint and precisely reacts to the data mentioned by the customer; rather than having multiple endpoints, every endpoint returns a fixed data structure as in REST.<\/p>\n<h2 class=\"wp-block-heading\">What&#8217;s the GraphQL in Magento?<\/h2>\n<ul>\n<li>Graphql was presented with Magento version 2.3 as a choice to REST\/SOAP API for frontend development.<\/li>\n<li>Magento utilizes two sorts of GraphQL operations:<\/li>\n<li>Question: used to understand data<\/li>\n<li>Change: used to compose data to the worker<\/li>\n<li>As of version 2.3.4, the accompanying center modules are utilizing GraphQL inquiry language:<\/li>\n<li>CatalogInventoryGraphQl<\/li>\n<li>CatalogUrlRewriteGraphQl<\/li>\n<li>BundleGraphQl<\/li>\n<li>CatalogGraphQl<\/li>\n<li>ConfigurableProductGraphQl<\/li>\n<li>CustomerGraphQl<\/li>\n<li>DownloadableGraphQl<\/li>\n<li>EavGraphQl<\/li>\n<li>GroupedProductGraphQl<\/li>\n<li>TaxGraphQl<\/li>\n<li>ThemeGraphQl<\/li>\n<li>UrlRewriteGraphQl<\/li>\n<li>WeeeGraphQl<\/li>\n<\/ul>\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.cmarix.com\/inquiry.html\"><img decoding=\"async\" class=\"alignnone\" src=\"https:\/\/lh4.googleusercontent.com\/hwThPWd_ufREF_M1W7Cjg0yNjDNBAYgNPWQ25BHppkQlzqW8NNVjjnvGC_JZ7rPkiNwfUPR1clfYSuzBndWGql2q8YnOqLXVPG9HdaBBVPOVM_VIZ8HnIy0\" alt=\"Hire Magento Developer India\" width=\"951\" height=\"271\" \/><\/a><\/figure>\n<h2 class=\"wp-block-heading\">Model: Getting Customer Data Utilizing GraphQl<\/h2>\n<p>In this model, we&#8217;ll make a simple GraphQL API inquiry to bring customer data by means of their email.<\/p>\n<h3 class=\"wp-block-heading\">Stage 1: Create your Module<\/h3>\n<p>We should call our module Magenest\/GraphQl. Our module should be stacked after GraphQL and Customer module so that the module.xml file would resemble this:<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/uRhk5TSrKmTIB4ahnlHy1E5KH1x-_7WqaPfbpv-l0_uWS9iDaKQyp6fkbMJDicueD9WPftsSg9gPT9UyQK0OeO367pmyMLn0JAo8_fw_aTqYvpY3ZOVpob0\" alt=\"Create your Module\" \/><\/figure>\n<h3 class=\"wp-block-heading\">Step 2: Define Schema<\/h3>\n<p>GraphQL queries are declared under vendor\/module\/etc\/schema.graphqls:<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/XLrOoPGe8GiBK8AKyJeCh8-WRZWzVy6a_lgzMsT4H5LGsjzKNEZ-k_IQgkNf30rk5ltSm0ZzQbGVnlJSiJyzlMYixKL0if8RwjkEuieX2RZnH0KpdIifDL4\" alt=\"Define Schema\" \/><\/figure>\n<ul>\n<li><strong>&#8220;type Query&#8221;:<\/strong> declares Query operations of our module<\/li>\n<li><strong>&#8220;testcustomer&#8221;:<\/strong> name of our query<\/li>\n<li><strong>email: String:<\/strong> declares input name (&#8217;email&#8217;) and type (&#8216;string&#8217;)<\/li>\n<li><strong>Test customer:<\/strong> defines the identity of the query, including resolver (@resolver) class, document (@doc), is the result cacheable (@cache), etc.<\/li>\n<li>Here we have defined<strong> &#8220;cacheable: false&#8221;<\/strong>, meaning the result will not be cached. If the result can be cached, use @cache tag and define a caching class instead.<\/li>\n<li><strong>&#8220;type Test customer&#8221;:<\/strong> define the result object of the query, including their name and type<\/li>\n<\/ul>\n<p><strong>Read More: <\/strong><a href=\"https:\/\/www.cmarix.com\/blog\/best-practices-for-magento-2-custom-theme-development\/\">Best Practices for Magento 2 Custom Theme Development<\/a><\/p>\n<h3 class=\"wp-block-heading\">Step 3: Create Resolver Class for the Schemain Magento 2<\/h3>\n<p><a href=\"https:\/\/www.cmarix.com\/magento-development.html\">Magento development Services<\/a>return data of customers with the requested email.<\/p>\n<p>app\/code\/Magenest\/GraphQl\/Model\/Resolver\/Customer.php<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/660bwppSLd9b1xw6Z5RYfDXYWdHvOszSGpdD2FG6AoE2QgwyxpsBgx9VlWLLoHLMrGO8qpH3ygJynhAHaYtKqFSX-0HrMvHYpy74_Jxy39GIqc-QsAr2tGA\" alt=\"Create Resolver Class for the Schemain Magento 2\" \/><\/figure>\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" style=\"width: 705px; height: 620px;\" src=\"https:\/\/lh5.googleusercontent.com\/ZE2AvEbMxpdPWMUnDcCWFdnG6qBUISl-eJftEtQq-qyY9KNa5ZhV1KshgSUk73hnF8pPVpak6BjPvW4tJRNAYE43MiLwTPkTM9_BQkPK2lYqaGWYlDiIANE\" alt=\"Create Resolver Class for the Schemain Magento 2\" width=\"705\" height=\"620\" \/><\/figure>\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" style=\"width: 705px; height: 620px;\" src=\"https:\/\/lh4.googleusercontent.com\/Zx1i3aaMH7MfLeiknUyRHPDc1-BpMC65JnVHiAT5Sh1wNsHD81ckoQ_PQU2xCPdd9LQzjZccI5i6fyy5ZkZ3pAE5xebIsLp1BGVqrbk1rdlk6hmzGvSWV-Y\" alt=\"Create Resolver Class for the Schemain Magento 2\" width=\"705\" height=\"620\" \/><\/figure>\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" style=\"width: 705px; height: 620px;\" src=\"https:\/\/lh4.googleusercontent.com\/q3pz1iZlQ1tFjRFfXMPJZwyvIeprY0GesS1l89Lg-LWP7T4FAp83lIH-I6UHUfFmtYmegwkOJbfxTrP3xSXBxBetsP4YXX_1t17eAhURkX8PDn9FRNe9jrs\" alt=\"Create Resolver Class for the Schemain Magento 2\" width=\"705\" height=\"620\" \/><\/figure>\n<h4 class=\"wp-block-heading\">Couple of things to note:<\/h4>\n<ul>\n<li>Resolver class must implement Magento\\Framework\\GraphQl\\Query\\ResolverInterface<\/li>\n<li><strong>&#8220;resolve&#8221;<\/strong> is the main method of this class, with $args as the query&#8217;s input<\/li>\n<li><a href=\"https:\/\/www.redhat.com\/en\/topics\/api\/what-is-graphql\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">GraphQL<\/a> has several exception classes, including GraphQlAuthorizationException and GraphQlNoSuchEntityException. These must be used to return the error to the client<\/li>\n<\/ul>\n<h4>Testing<\/h4>\n<p>For testing <a href=\"https:\/\/www.cmarix.com\/hire-magento-developer.html\">hire dedicated magento developers<\/a> or you can check GraphQL query and response by installing a Chrome extension called &#8220;ChromeiQL&#8221;. Then, set the endpoint as &#8220;&lt;your Magento root url&gt;\/graphql&#8221;. After that, input your GraphQL query on the left side of &#8220;ChromeiQL&#8221; interface and click on the <strong>&#8220;Set endpoint&#8221;<\/strong> button.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hire magento developersand create Custom GraphQL in Magento 2. GraphQL is a [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[24],"tags":[],"class_list":["post-19527","post","type-post","status-publish","format-standard","hentry","category-magento-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/posts\/19527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/comments?post=19527"}],"version-history":[{"count":3,"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/posts\/19527\/revisions"}],"predecessor-version":[{"id":49449,"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/posts\/19527\/revisions\/49449"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/media?parent=19527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/categories?post=19527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/blog\/wp-json\/wp\/v2\/tags?post=19527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}