-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new GraphQL query for single product retrieval #28316
base: 2.4-develop
Are you sure you want to change the base?
Add new GraphQL query for single product retrieval #28316
Conversation
Hi @serhiyzhovnir. Thank you for your contribution
❗ Automated tests can be triggered manually with an appropriate comment:
You can find more information about the builds here ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review. For more details, please, review the Magento Contributor Guide documentation. |
product ( | ||
id: Int @doc(description: "Id of the product.") | ||
): ProductInterface | ||
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\ProductId") @doc(description: "The product query searches for product that match the criteria specified in the search and filter attributes.") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductIdIdentity") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"that match the criteria specified in the search and filter attributes" doesn't seem correct if the query only has an attribute to specify the id of the product.
"The product query searches for a product that matches the specified id." feels a bit more closer to what it should be, looking at the attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your suggestion, the description has been adjusted.
* @param array $attributeCodes | ||
* @return ProductInterface|Product | ||
*/ | ||
public function getProductById(int $productId, array $attributeCodes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, maybe this part was agreed previously. But I would use return types hinting since we use strict_types declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't agreed previously, I just missed the strict type here.
I've just added it.
Thanks!
@magento run Unit Tests |
} | ||
|
||
/** @var \Magento\Catalog\Model\Product $product */ | ||
$product = $this->productDataProvider->getProductById($productId, $fields); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried the following scenario?
Pass "-1" as the product ID in the GraphQL query 😎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -10,6 +10,10 @@ type Query { | |||
sort: ProductAttributeSortInput @doc(description: "Specifies which attributes to sort on, and whether to return the results in ascending or descending order.") | |||
): Products | |||
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Products") @doc(description: "The products query searches for products that match the criteria specified in the search and filter attributes.") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\Identity") | |||
product ( | |||
id: Int @doc(description: "Id of the product.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The argument should be required.
Also, @kokoc what if we change the type to ID
, it seems to correspond our strategy. But may cause the need of type casting in strictly typed languages.
id: Int @doc(description: "Id of the product.") | |
id: ID! @doc(description: "Id of the product.") |
@@ -10,6 +10,10 @@ type Query { | |||
sort: ProductAttributeSortInput @doc(description: "Specifies which attributes to sort on, and whether to return the results in ascending or descending order.") | |||
): Products | |||
@resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Products") @doc(description: "The products query searches for products that match the criteria specified in the search and filter attributes.") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\Identity") | |||
product ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this endpoint does not support multiple IDs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seconding this - input should be a non-nullable list type of IDs.
Fun fact: GraphQL spec allows clients to pass a single value when the input is a list type, and it will coerce to a list length of 1 with that value:
In other words, a list type will work fine for both use-cases, with some nice syntax sugar from GraphQL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One other note: if we accept a list type as an input, we'll need a different name for this field that isn't singular.
@magento run all tests |
hey guys, why not do this? |
@cpartica we had a discussion recently about this one offline. I think your preference was to go the route of the IOW, interface Node {
uid: ID!
} and then we add a new root field: type Query {
node(id: ID!): Node
} Is that right? |
we actually want this: magento/architecture#477 |
Description (*)
This PR adds a new GraphQL endpoint for the products fetching by IDs.
The new GraphQL single product retrieval endpoint was requested at magento/catalog-storefront#12 issue.
After the conversations in this PR, there was a request to support the fetching of several products by IDs. Comment Link
Fixed Issues (if relevant)
Manual testing scenarios (*)
The result should be like on the following screenshot:
Contribution checklist (*)