How to display data in front using GraphQL

We can fetch data through GraphQL Schema in 2 ways:

  • Apollo Client Library
  • AJAX

These both are alternate solutions. You can review Apollo Client Document for how to retrieve data using the Apollo Client. Moreover, we prefer to install the Apollo-Boost AMD module which is free open-source, you can get its GitHub repo from here.

In this blog, we would get an idea of fetching data using GraphQL with AJAX and display retrieved data in front of the Magento 2 website.

Step 1: Create GraphQL Schema
Please create GraphQL schema as described in the following blogs:

Step 2: Create XML Layout file
File Path: Mage2/Person/view/frontend/layout/catalog_product_view.xml
We have taken an example in which we are displaying a person’s information on the product view page.

<referenceContainer name="content">
    <referenceContainer name="">
        <block class="Mage2\Person\Block\Info"

Step 3: Create Template file
File Path: Mage2/Person/view/frontend/templates/info.phtml

<div data-bind="scope: 'person-graphql'">

    <!-- ko template: getTemplate() --><!-- /ko -->

    <script type="text/x-magento-init">
            "*": {
                "Magento_Ui/js/core/app": {
                    "components": {
                        "person-graphql": {
                            "component": "Mage2_Person/js/info-graphql",
                            "template": "Mage2_Person/person-info"

Step 4: Create Knockout JS file
File Path: Mage2/Person/view/frontend/templates/web/js/info-graphql.js

Step 4:define(['uiComponent', 'jquery', 'ko'], function(Component, $, ko) {
    'use strict';

    return Component.extend({
        defaults: {
            result: ko.observableArray(),
            isEnabled: ko.observable(false)

        initialize: function () {

            var self = this;

            const query = `
query getPersonData($id: Int!) 
  person(person_id: $id) 
            const payload = {
                query: query,
                variables: {
                    person_id: 1

                url: 'graphql',
                contentType: 'application/json',
                dataType: 'json',
                method: 'POST',
                data: JSON.stringify(payload),
                success: (function (response) {

                    if (self.result().person.person_id != null) {
                error: (function (error) {
            return false;

Step 5: Create Knockout JS Template file
File Path: Mage2/Person/view/frontend/templates/web/js/template/person-info.html

<!-- ko if: isEnabled -->
    <h4 data-bind="text: $t('Person Information')"></h4>

    	<span data-bind="text: $t('Person Name')"></span>
    	<span data-bind="text: result()"></span>

    	<span data-bind="text: $t('Person Age')"></span>
    	<span data-bind="text: result().person.age"></span>
<!-- /ko -->

We hope this blog may understandable and useful to you. You can email us at if we missed anything or want to add any suggestions. We will respond to you as soon as possible. Happy to help 🙂