Hyvä Theme is optimized for performance and simplifies Magento’s frontend, but it still supports GraphQL in Hyvä for efficient data fetching, particularly in cases where traditional REST APIs or direct database queries may not be ideal. Hyvä theme GraphQL integration allows for flexible, performant, and structured data retrieval, making it a great choice for modern Magento development.
Unlike Magento’s traditional REST or SOAP APIs, Hyvä data fetching using GraphQL allows you to:
Feature | GraphQL | REST API |
Data Fetching | Fetch specific fields | Fetch full objects |
API Calls | Single request for multiple data types | Multiple requests needed |
Performance | Efficient with reduced payload | Can be slower |
Frontend Flexibility | Easier to structure UI components | Harder to manage |
Magento comes with GraphQL enabled by default. Ensure that the Magento GraphQL endpoint is accessible:
https://yourdomain.com/graphql
If GraphQL is not working, make sure your store is running in developer or production mode and the GraphQL schema is properly generated.
Run:
php bin/magento setup:upgrade
php bin/magento cache:flush
Hyvä doesn’t rely on KnockoutJS or Magento UI Components, so we need a native Hyvä theme GraphQL integration using:
fetch('/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
query {
products(filter: { sku: { eq: "24-MB01" } }) {
items {
name
price {
regularPrice {
amount {
value
currency
}
}
}
}
}
}
`
})
})
.then(response => response.json())
.then(data => console.log(data));
<div x-data="{ product: {} }" x-init="fetch('/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: `query { products(filter: { sku: { eq: \"24-MB01\" } }) { items { name } } }` })
}).then(response => response.json())
.then(data => product = data.data.products.items[0])">
<h1 x-text="product.name"></h1>
</div>
This binds product data dynamically using Alpine.js.
Unlike REST, Magento 2 GraphQL with Hyvä allows multi-resource requests in a single call.
const query = `
query {
categories(filters: { name: { match: "Men" } }) {
items { name id }
}
products(search: "T-Shirt") {
items { name sku }
}
}
`;
fetch('/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query }) })
.then(res => res.json())
.then(data => console.log(data));
const mutation = `
mutation {
addSimpleProductsToCart(input: {
cart_id: "123456789",
cart_items: [{ sku: "24-MB01", quantity: 1 }]
}) {
cart { items { product { name } } }
}
}
`;
fetch('/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: mutation }) })
.then(res => res.json())
.then(data => console.log(data));
To optimize Hyvä theme with GraphQL, follow these best practices:
Optimize your Magento 2 store with VDC Store's Hyvä Theme development services. By optimizing the Hyvä theme with GraphQL, we ensure improved performance and seamless data fetching, enhancing your store's overall efficiency.