1

I've enabled at backoffice the usage of the category inside the product url by setting this option: enter image description here

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.

2 Answers 2

2

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.

0
0

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
          }
        }
    }
}
3
  • In this way I get the product category, not the product url with the category.
    – Pasquale
    Dec 4, 2020 at 12:27
  • Please show what you want to show in response ?
    – Msquare
    Dec 4, 2020 at 12:41
  • Updated the question: I want tires/tire-black as url instead of just tire-black, where tires is the category.
    – Pasquale
    Dec 4, 2020 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.