📚 Introduction to Magento 2 GraphQL API
Magento 2 GraphQL is a query language for your store’s data. Unlike REST API, GraphQL allows you to request exactly the data you need, which makes it a powerful and flexible tool for developers building headless commerce solutions.
In this tutorial, we’ll explore how to use GraphQL to query Magento 2 data. We’ll cover basic queries, arguments, and mutations to modify the store’s data.
🧠 What is GraphQL?
GraphQL is a query language for APIs and a runtime for executing those queries. With GraphQL, you can:
- Fetch multiple resources in a single request
- Request only the data you need
- Mutate store data (add/update/delete items)
🔑 Authentication with GraphQL
To authenticate with GraphQL, you’ll need an access token. The process is similar to REST API, where you obtain a token and use it in the Authorization header.
💡 How to Obtain an Admin Token
You can obtain an admin token by sending a POST request to:
POST /rest/V1/integration/admin/token
With the body containing your admin credentials:
{ "username": "admin", "password": "adminpassword" }
🚀 Making Your First GraphQL Query
Once authenticated, you can start querying data. Here’s an example of a GraphQL query to get a list of products:
🔧 Query: Get Products
POST /graphql Authorization: BearerContent-Type: application/json { "query": "{ products(pageSize: 10) { items { id name sku price } } }" }
📄 Response Example
The response will return a list of products with their ID, name, SKU, and price:
{ "data": { "products": { "items": [ { "id": 1, "name": "Cool T-Shirt", "sku": "t-shirt", "price": 29.99 }, { "id": 2, "name": "Stylish Hat", "sku": "hat", "price": 15.99 } ] } } }
🔎 Using Arguments for Filtering
You can add arguments to filter and paginate data. Here’s an example of how to filter products by SKU:
🔧 Query: Filter Products by SKU
POST /graphql Authorization: BearerContent-Type: application/json { "query": "{ products(sku: \"t-shirt\") { items { id name sku price } } }" }
📄 Response Example
The response will return the filtered product(s):
{ "data": { "products": { "items": [ { "id": 1, "name": "Cool T-Shirt", "sku": "t-shirt", "price": 29.99 } ] } } }
⚙️ GraphQL Mutations
GraphQL mutations allow you to modify data in your Magento store. You can create, update, or delete products, customers, and more.
🔧 Mutation: Create a Product
POST /graphql Authorization: BearerContent-Type: application/json { "query": "mutation { createProduct(input: {sku: \"cool-new-shirt\", name: \"Cool New Shirt\", price: 25.99}) { sku name price } }" }
📄 Response Example
The response will contain the created product’s details:
{ "data": { "createProduct": { "sku": "cool-new-shirt", "name": "Cool New Shirt", "price": 25.99 } } }
❌ Deleting a Product via Mutation
You can also delete a product using a mutation. Here’s how:
🔧 Mutation: Delete Product
POST /graphql Authorization: BearerContent-Type: application/json { "query": "mutation { deleteProduct(sku: \"cool-new-shirt\") { sku } }" }
✅ Conclusion
In this tutorial, we covered the basics of using Magento 2’s GraphQL API to query and mutate store data. With GraphQL, you can efficiently access and modify store data with more flexibility than traditional REST APIs.