---
title: "How to get custom module data into GraphQL Magento 2"
url: "https://dolphinwebsolution.com/how-to-get-custom-module-data-in-to-graphql-magento-2"
date: "2020-11-28T10:48:55+05:30"
modified: "2023-08-24T10:14:23+05:30"
author:
  name: "Mahesh Makwana"
categories:
  - "Magento"
word_count: 773
reading_time: "4 min read"
summary: "Magento provides some default queries to retrieve many data. Sometimes you need to get custom data from a custom module. That time you need to need a custom graphql query to get custom data. Today ..."
description: "GraphQL magento 2 is a query language for the API, allowing clients to accurately identify the data needed and the server returns only the data requested."
keywords: "GraphQL Magento 2,graphql schema,GraphQL dependency,graphql schema validator,graphql schema file, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How To Programmatically Assign Products To Multiple Categories In Magento 2"
    url: "https://dolphinwebsolution.com/how-to-programmatically-assign-products-to-multiple-categories-in-magento-2"
  - title: "How to Add Custom Tab in Product Page Magento 2"
    url: "https://dolphinwebsolution.com/how-to-add-custom-tab-in-product-page-magento-2"
  - title: "How to Customize Email Templates in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-customize-email-templates-in-magento-2"
---

# How to get custom module data into GraphQL Magento 2

_Published: November 28, 2020_  
_Author: Mahesh Makwana_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/Graphql-Magento-2-1024x512.jpg)

> Magento provides some default queries to retrieve many data. Sometimes you need to get custom data from a custom module. That time you need to need a custom graphql query to get custom data. Today we will learn how to custom module data from GraphQl in Magento 2. First create a basic module with a custom table and also create models for it. so after complete basic module you need to follow below steps:

[GraphQL Magento 2](https://devdocs.magento.com/guides/v2.4/graphql/) is a query language for the API, allowing clients to accurately identify the data needed and the server returns only the data requested. GraphQL allows clients to upload a datasheet, server actions must return the same information.

The GraphQL server only needs a single endpoint and accurately responds to the data requested by the client.

Magento provides some default queries to retrieve many data. There are various modules of Magneto which use GraphQL, like CatalogGraphQL, CmsGraphQL, CustomerGraphQL, etc. Sometimes you need to get custom data from a custom module. That time you need to need a custom GraphQL query to get custom data.

Today we will learn how to custom module data from GraphQL in Magento 2. First create a [basic module](https://dolphinwebsolution.com/how-to-create-module-in-magento-2) with a custom table and also create models for it. so after complete basic module you need to follow below steps:

**Step-1** **:** Update **module.xml** file at **app/code/VendoreName/ModuleName/etc** and add below code.

```
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendoreName_ModuleName" setup_version="1.0.0"/>
        <sequence>
            <module name="Magento_GraphQl"/>
            <module name="Magento_Backend"/>
        </sequence>
</config>
```

Here we need to give GraphQL dependency to our custom module.

**Step-2 :** Create **schema.graphqls** file at **app/code/VendoreName/ModuleName/etc** and add below code.

Before creating schema.graphqls you need to check your custom table structure. My custom table structure is given below:

```
+--------------+-------------------+------+-----+---------+----------------+
| Field        | Type              | Null | Key | Default | Extra          |
+--------------+-------------------+------+-----+---------+----------------+
| rule_id      | int unsigned      | NO   | PRI | NULL    | auto_increment |
| customer_id  | int               | NO   | UNI | 0       |                |
| shared       | smallint unsigned | NO   | MUL | 0       |                |
| sharing_code | varchar(32)       | YES  |     | NULL    |                |
| updated_at   | timestamp         | YES  |     | NULL    |                |
+--------------+-------------------+------+-----+---------+----------------+
```

Now Create schema.graphqls file you need to add **String** data type to varchar, text, timestamp etc column type and add **Int** data type to int, smallint etc column into this file. And You need to add column description into **@doc(“desc”)**.

```
type Query {
    getCustomData : [customdata] @resolver( class: "VendoreName\\ModuleName\\Model\\Resolver\\CustomData") @doc(description: "Get All  Banners")
}

type customdata {
    rule_id : Int  @doc(description: "Primary Id"),
    customer_id : Int  @doc(description: "Customer Id"),
    shared : Int  @doc(description: "Share Code Number"),
    sharing_code : String  @doc(description: "Share Code String"),
    updated_at : String  @doc(description: "Update Date")
}
```

Each module that adds to or extends from a GraphQL schema can do so by placing a schema.graphqls file in its etc directory. we have create GraphQL query.

**Step-3 :** Create **CustomData.php** file at **app/code/VendoreName/ModuleName/Model/Resolver** and add below code.

```
<?php

namespace VendoreName\ModuleName\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class CustomData implements ResolverInterface
{
    private $customDataProvider;

    public function __construct(
        \VendoreName\ModuleName\Model\Resolver\DataProvider\CustomData $customDataProvider
    ) {
        $this->customDataProvider = $customDataProvider;
    }

    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $customData = $this->customDataProvider->getCustomData();
        return $customData;
    }
}
```

After that we need to create a DataProvider file which is defined in above \_\_construct().

**Step-4 :** Create **CustomData.php** file at **app/code/VendoreName/ModuleName/Model/Resolver/DataProvider** and add below code.

```
<?php

namespace VendoreName\ModuleName\Model\Resolver\DataProvider;

class CustomData
{
    protected $modelFactory;

    public function __construct(
        \VendoreName\ModuleName\Model\ModelNameFactory $modelFactory
        )
    {
        $this->modelFactory  = $modelFactory;
    }

    public function getCustomData( )
    {
        try {
            $collection = $this->modelFactory->create()->getCollection();
            $CustomData = $collection->getData();

        } catch (NoSuchEntityException $e) {
            throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
        }
        return $CustomData;
    }
}
```

In the above file the **getCustomData()** function is responsible to get data from a custom table And you need to replace your model name into \_\_construct().

**Step-5 :** Run Magento upgrade command after creating above file

```
bin/magento setup:upgrade
bin/magento c:f
```

**Step-6 :** To Run GraphQL You can use some extensions like [ChromeiQL](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en) or [Altair GraphQL](https://chrome.google.com/webstore/detail/altair-graphql-client/flnheeellpciglgpaodhkhmapeljopja?hl=en) addon. After that To check our custom query we need to **set up the endpoint**.

It is usually coming like **<magento\_root\_url>/graphql**. first must be add your **base url** and after that add **/graphql** like :- **http://localhost/graphql.** then add our custom query to the left side panel.

```
query customdata {
  customdata {
    rule_id
    customer_id
    shared
    sharing_code
    updated_at
  }
}
```

Then run and you can see your table data into the right side panel. With the above files we get a collection of our Custom module table.

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.


---

_View the original post at: [https://dolphinwebsolution.com/how-to-get-custom-module-data-in-to-graphql-magento-2](https://dolphinwebsolution.com/how-to-get-custom-module-data-in-to-graphql-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:54:09 UTC_  
