Retrieve product url with category in the graphQL
Question
I've enabled at backoffice the usage of the category inside the product url by setting this option:
When I access the magento frontend, I can now access the product with the url <category>/<product>
. However, when I call the graphQL API:
query {
products(filter: {
sku: {
eq: "1"
}
}) {
items {
sku
canonical_url
url_path
url_key
}
}
}
I get this response:
{
"data": {
"products": {
"items": [
{
"sku": "1",
"canonical_url": "tire-black",
"url_path": null,
"url_key": "tire-black"
}
]
}
}
}
In other words, I get the path url with the product name only,(i.e. without the category). In the example, the product tire-black
belongs to the category tires
. In the url_rewrite
table the associated url with the category is: tires/tire-black
, which is what I want to retrieve using the graphQL.
Do you know if there's another field I should use to get the url comprehensive of the category (i.e. tires/tire-black
)?
If not, what should I do to get it in the graphql response?
Thank you.
Solution
Set this below request in your GraphQL :
Query Request :
query {
products(filter: {
sku: {
eq: "24-MB01"
}
}) {
items {
sku
canonical_url
url_path
url_key
url_rewrites {
url
parameters {
name
value
}
}
}
}
}
Response :
{
"data": {
"products": {
"items": [
{
"sku": "24-MB01",
"canonical_url": null,
"url_path": null,
"url_key": "joust-duffle-bag",
"url_rewrites": [
{
"url": "joust-duffle-bag.html",
"parameters": [
{
"name": "id",
"value": "1"
}
]
},
{
"url": "gear/joust-duffle-bag.html",
"parameters": [
{
"name": "id",
"value": "1"
},
{
"name": "category",
"value": "3"
}
]
},
{
"url": "gear/bags/joust-duffle-bag.html",
"parameters": [
{
"name": "id",
"value": "1"
},
{
"name": "category",
"value": "4"
}
]
}
]
}
]
}
}
}
Hope, It will useful for you.
OTHER TIPS
try this
query {
products(filter: {
sku: {
eq: "1"
}
}) {
items {
categories {
id
name
url_key
url_path
canonical_url
include_in_menu
}
name
sku
canonical_url
url_path
url_key
image{
label
url
}
}
}
}