magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Magento Graph QL : route query product and cms-page caching issue

Open mandarzope opened this issue 3 years ago • 15 comments

Preconditions (*)

  1. Magento 2.4.3-p1
  2. Sample Data is imported

Steps to reproduce (*)

  1. Query using route(url:"<>")

Expected result (*)

  1. Response header should contain X-Magento-Tags and Cache-Control to be public

Actual result (*)

  1. X-Magento-Tags header is missing and Cache-Control is no-cache
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.

After debugging I have found that vendor/magento/module-cms-url-rewrite-graph-ql/Model/Resolver/UrlRewrite/CmsUrlResolverIdentity.php:26 vendor/magento/module-catalog-url-rewrite-graph-ql/Model/Resolver/UrlRewrite/CatalogUrlResolverIdentity.php:36

expects a key 'id' to be present in $resolvedData $ids = [$selectedCacheTag, sprintf('%s_%s', $selectedCacheTag, $resolvedData['id'])];

A workaround solution is to overwrite the files vendor/magento/module-catalog-url-rewrite-graph-ql/Model/DataProvider/UrlRewrite/ProductDataProvider.php:48 $product = $this->productRepository->getById($id, false, $storeId); $result = $product->getData(); $result['id'] = $product->getId();

vendor/magento/module-cms-url-rewrite-graph-ql/Model/DataProvider/UrlRewrite/Page.php:49 $result = $this->pageDataProvider->getDataByPageId((int)$id); $result['type_id'] = $entity_type; $result['id'] = $result['page_id']

mandarzope avatar Jan 01 '22 06:01 mandarzope

Hi @mandarzope. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] avatar Jan 01 '22 06:01 m2-assistant[bot]

@magento give me 2.4-develop instance

mandarzope avatar Jan 01 '22 06:01 mandarzope

Hi @mandarzope. Thank you for your request. I'm working on Magento instance for you.

Hi @mandarzope, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

Hi @engcom-Echo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

    1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
      DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
    1. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.
    1. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
    1. Verify that the issue is reproducible on 2.4-develop branch
      Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
      - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
      - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

m2-assistant[bot] avatar Jan 03 '22 06:01 m2-assistant[bot]

Hi @mandarzope,

Thank you for reporting the issue.

Issue is verified and reproduced on Magento 2.4-develop. Below are the steps I did to reproduce it:

  1. Added sample products for testing if GraphQL caching is working properly.
  2. Ran GraphQl route and products query multiple times.
  3. First time result was loading slow but next calls results were loading from cache so query was fast. Confirmed from var/cache folder as well.
  4. But the response headers are showing differently like x-magento-cache-debug is coming as MISS. cache-control is showing no-cache. Attached screenshot of same:
Screenshot 2022-01-03 at 5 05 43 PM

Based on the above analysis, confirming the issue.

Thanks

engcom-Echo avatar Jan 03 '22 07:01 engcom-Echo

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-2059 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Jan 03 '22 11:01 github-jira-sync-bot

:white_check_mark: Confirmed by @engcom-Echo. Thank you for verifying the issue.
Issue Available: @engcom-Echo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

m2-assistant[bot] avatar Jan 03 '22 11:01 m2-assistant[bot]

The issue here is very similar for both Products and CMS pages.

For products, data is provided by https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CatalogUrlRewriteGraphQl/Model/DataProvider/UrlRewrite/ProductDataProvider.php

For CMS pages, data is provided by https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CmsUrlRewriteGraphQl/Model/DataProvider/UrlRewrite/Page.php

Both of these data providers return data using the relevant repositories, so for products, we end up with entity_id as part of the result array, while for CMS pages, we end up with page_id.

However, when it comes to determining if a query is cacheable, we need to look at https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CatalogUrlResolverIdentity.php and https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CmsUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CmsUrlResolverIdentity.php

We can see in both cases, the code is looking specifically for the presence of id in the resolved data. Since this doesn't exist in either case, no cache tags get added to the array of identities. Subsequently, the query is considered to not be cacheable, and the no-cache headers are added.

https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CatalogUrlResolverIdentity.php#L29-L40

https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CmsUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CmsUrlResolverIdentity.php#L26-L34

My suggestion would be for one of the following two things to happen:

  1. Change CatalogUrlResolverIdentity to use entity_id instead of id. This class is also used for categories, but since they also have an entity id, this should still work. Similarly, change CmsUrlResolverIdentity to use page_id instead of id.
  2. Change the two data providers to additionally set the id value based on entity_id (for products) or page_id (for CMS pages.

aligent-lturner avatar Jun 07 '23 04:06 aligent-lturner

The issue here is very similar for both Products and CMS pages.

For products, data is provided by https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CatalogUrlRewriteGraphQl/Model/DataProvider/UrlRewrite/ProductDataProvider.php

For CMS pages, data is provided by https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CmsUrlRewriteGraphQl/Model/DataProvider/UrlRewrite/Page.php

Both of these data providers return data using the relevant repositories, so for products, we end up with entity_id as part of the result array, while for CMS pages, we end up with page_id.

However, when it comes to determining if a query is cacheable, we need to look at https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CatalogUrlResolverIdentity.php and https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CmsUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CmsUrlResolverIdentity.php

We can see in both cases, the code is looking specifically for the presence of id in the resolved data. Since this doesn't exist in either case, no cache tags get added to the array of identities. Subsequently, the query is considered to not be cacheable, and the no-cache headers are added.

https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CatalogUrlResolverIdentity.php#L29-L40

https://github.com/magento/magento2/blob/cfaeeaef415c2a77009db57995e5eee627350878/app/code/Magento/CmsUrlRewriteGraphQl/Model/Resolver/UrlRewrite/CmsUrlResolverIdentity.php#L26-L34

My suggestion would be for one of the following two things to happen:

  1. Change CatalogUrlResolverIdentity to use entity_id instead of id. This class is also used for categories, but since they also have an entity id, this should still work. Similarly, change CmsUrlResolverIdentity to use page_id instead of id.
  2. Change the two data providers to additionally set the id value based on entity_id (for products) or page_id (for CMS pages.

That will teach me to check an older version of the code - it appears this issue has already been fixed in 2.4-develop (as can be seen in the code snippets above)

aligent-lturner avatar Jun 07 '23 04:06 aligent-lturner

@engcom-Echo can you test this against the latest 2.4-develop code - I believe it should be fixed now (by this commit https://github.com/magento/magento2/commit/967439abee8e9e2e934aaa983748357dd8816b7d).

aligent-lturner avatar Jun 07 '23 04:06 aligent-lturner

Apparently, the reason is that initially graphql requests are POST requests, so cache tags will not be assigned to them because of this condition 2024-07-10_01-11

rostilos avatar Jul 09 '24 22:07 rostilos

I had raised the PR in past (2 years back). However because I am not a contributor, these are not accepted.

https://github.com/magento/magento2/pull/34910

https://github.com/magento/magento2/pull/34909

mandarzope avatar Jul 10 '24 04:07 mandarzope

Hi @mandarzope,

Thanks for your reporting and collaboration.

We have tried to reproduce the issue in Latest 2.4-develop instance and the issue is still reproducible.Kindly refer the screenshots.

Image

X-Magento-Tags header is missing and Cache-Control is no-cache.Hence Confirming the issue.

Thanks.

engcom-Bravo avatar Jun 12 '25 09:06 engcom-Bravo

:x: Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.adobe.com/browse/AC-2059

github-jira-sync-bot avatar Jun 12 '25 09:06 github-jira-sync-bot