magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.43k stars 9.29k forks source link

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

Open mandarzope opened 2 years ago

mandarzope commented 2 years ago

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

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']

m2-assistant[bot] commented 2 years ago

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

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.


: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

mandarzope commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

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

magento-deployment-service[bot] commented 2 years ago

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

m2-assistant[bot] commented 2 years ago

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:


engcom-Echo commented 2 years ago

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

github-jira-sync-bot commented 2 years ago

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

m2-assistant[bot] commented 2 years ago

: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.

aligent-lturner commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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).

rostilos commented 1 month ago

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

mandarzope commented 1 month ago

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