magento2
magento2 copied to clipboard
Magento Graph QL : route query product and cms-page caching issue
Preconditions (*)
- Magento 2.4.3-p1
- Sample Data is imported
Steps to reproduce (*)
- Query using route(url:"<
>")
Expected result (*)
- Response header should contain X-Magento-Tags and Cache-Control to be public
Actual result (*)
- 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']
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.
- Join Magento Community Engineering Slack and ask your questions in #github channel.
: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
@magento give me 2.4-develop instance
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:
-
- Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
Details
If the issue has a valid description, the labelIssue: Format is validwill be added to the issue automatically. Please, edit issue description if needed, until labelIssue: Format is validappears.
- Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
-
- Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add
Issue: Clear Descriptionlabel to the issue by yourself.
- Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add
-
- Add
Component: XXXXXlabel(s) to the ticket, indicating the components it may be related to.
- Add
-
- Verify that the issue is reproducible on
2.4-developbranchDetails
- Add the comment@magento give me 2.4-develop instanceto deploy test instance on Magento infrastructure.
- If the issue is reproducible on2.4-developbranch, please, add the labelReproduced 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!
- Verify that the issue is reproducible on
- Join Magento Community Engineering Slack and ask your questions in #github channel.
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:
- Added sample products for testing if GraphQL caching is working properly.
- Ran GraphQl route and products query multiple times.
- 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.
- But the response headers are showing differently like
x-magento-cache-debugis coming asMISS.cache-controlis showingno-cache. Attached screenshot of same:
Based on the above analysis, confirming the issue.
Thanks
:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-2059 is successfully created for this GitHub issue.
: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.
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:
- Change
CatalogUrlResolverIdentityto useentity_idinstead ofid. This class is also used for categories, but since they also have an entity id, this should still work. Similarly, changeCmsUrlResolverIdentityto usepage_idinstead ofid. - Change the two data providers to additionally set the
idvalue based onentity_id(for products) orpage_id(for CMS pages.
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_idas part of the result array, while for CMS pages, we end up withpage_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
idin 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:
- Change
CatalogUrlResolverIdentityto useentity_idinstead ofid. This class is also used for categories, but since they also have an entity id, this should still work. Similarly, changeCmsUrlResolverIdentityto usepage_idinstead ofid.- Change the two data providers to additionally set the
idvalue based onentity_id(for products) orpage_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)
@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).
Apparently, the reason is that initially graphql requests are POST requests, so cache tags will not be assigned to them because of this condition
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
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.
X-Magento-Tags header is missing and Cache-Control is no-cache.Hence Confirming the issue.
Thanks.
:x: Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.adobe.com/browse/AC-2059