How to Create a Custom GraphQL Endpoint in Magento 2 [Step-by-Step Guide].
Magento 2’s GraphQL support makes it ideal for modern eCommerce frontends. But what happens when you need to expose custom data that isn’t available through Magento’s default API? That’s where creating your own GraphQL endpoint comes in.
In this tutorial, we’ll show you how to build a custom GraphQL endpoint in Magento 2 using a module called SbDevBlog_CustomGraphQL
. Whether you’re working with a custom table or just experimenting, this guide will get you started — the right way.
How to Create a Custom GraphQL Endpoint in Magento 2
🔧 Why Create Custom GraphQL Endpoints?
Magento’s core GraphQL schema is robust, but not every use case fits into its structure. Some reasons you might need custom endpoints:
- Display data from a custom entity (e.g., testimonials, reviews)
- Send user-submitted data (e.g., feedback, contact forms)
- Integrate third-party systems
With a custom endpoint, you stay flexible while keeping your frontend sleek and optimized.
🚀 Let’s Build: A Simple GraphQL Module
We’ll create a query that returns a list of records with id
, title
, and description
.
✅ Step 1: Create the Module Directory
app/code/SbDevBlog/CustomGraphQL
✅ Step 2: Register the Module
registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'SbDevBlog_CustomGraphQL',
__DIR__
);
etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SbDevBlog_CustomGraphQL" setup_version="1.0.0" />
</config>
✅ Step 3: Define the GraphQL Schema
etc/schema.graphqls
type Query {
getCustomRecords(filter: CustomFilterInput): [CustomRecord] @resolver(class: "SbDevBlog\\CustomGraphQL\\Model\\Resolver\\GetRecords")
}
input CustomFilterInput {
id: Int
title: String
}
type CustomRecord {
id: Int
title: String
description: String
}
✅ Step 4: Create the Resolver
Model/Resolver/GetRecords.php
namespace SbDevBlog\CustomGraphQL\Model\Resolver;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
class GetRecords implements ResolverInterface
{
public function resolve(
$field,
$context,
ResolveInfo $info,
array $value = null,
$args = null
) {
$data = [
['id' => 1, 'title' => 'Record One', 'description' => 'Description for record one.'],
['id' => 2, 'title' => 'Record Two', 'description' => 'Description for record two.']
];
if (!empty($args['filter']['id'])) {
return array_filter($data, fn($item) => $item['id'] == $args['filter']['id']);
}
return $data;
}
}
✅ Step 5: Enable and Deploy
php bin/magento setup:upgrade
php bin/magento cache:flush
🔍 Step 6: Test It with GraphQL
https://your-magento-site.com/graphql
{
getCustomRecords {
id
title
description
}
}
Filtered Query:
{
getCustomRecords(filter: { id: 1 }) {
id
title
description
}
}
💡 Best Practices
- Keep your resolver logic simple; delegate heavy lifting to models or services.
- Consider adding ACL rules for sensitive data.
- Organize schema files cleanly for long-term maintainability.
How to Create a Custom GraphQL Endpoint in Magento 2
🧠 Coming Up Next
In our next post, we’ll show you how to add custom mutations in GraphQL to insert, update, or delete data via a custom module.
Stay connected with SB Dev Blog for advanced Magento 2 GraphQL development tips!