Skip to content
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

Open
wants to merge 7 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

serhiyzhovnir
Copy link

@serhiyzhovnir serhiyzhovnir commented May 21, 2020

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)

  1. Fixes New GraphQL endpoint for single product retrieval catalog-storefront#12: New GraphQL endpoint for single product retrieval

Manual testing scenarios (*)

  1. Create a product
  2. Use a similar query to retrieve the products data by IDs
{
  productsByID(ids: [1, 2]) {
    id
    name
    sku
    categories {
      id
      name
    }
    media_gallery {
      url
    }
  }
}

The result should be like on the following screenshot:
Screenshot 2020-06-24 at 09 30 37

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented May 21, 2020

Hi @serhiyzhovnir. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

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.

@magento-engcom-team magento-engcom-team added Component: CatalogGraphQl Release Line: 2.4 Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner labels May 21, 2020
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")

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.

Copy link
Author

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)
Copy link
Contributor

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.

Copy link
Author

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!

@ghost ghost assigned rogyar May 21, 2020
@okorshenko
Copy link
Contributor

@magento run Unit Tests

@serhiyzhovnir serhiyzhovnir requested a review from rogyar May 26, 2020 05:21
}

/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productDataProvider->getProductById($productId, $fields);
Copy link
Contributor

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 😎

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tried this scenario before.
I've just added the additional validation rule for throwing the exception if the ID argument is negative.
Screenshot 2020-06-09 at 16 15 14
Thank you!

@rogyar rogyar added Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests Award: category of expertise labels May 28, 2020
@serhiyzhovnir serhiyzhovnir requested a review from rogyar June 9, 2020 13:23
@nrkapoor nrkapoor added this to the 2.4.1 milestone Jun 10, 2020
@@ -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.")
Copy link
Contributor

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.

Suggested change
id: Int @doc(description: "Id of the product.")
id: ID! @doc(description: "Id of the product.")

@ghost ghost assigned paliarush Jun 10, 2020
@@ -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 (
Copy link
Contributor

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?

Copy link

@DrewML DrewML Jun 15, 2020

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:

image

In other words, a list type will work fine for both use-cases, with some nice syntax sugar from GraphQL.

Copy link

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.

@nrkapoor nrkapoor removed request for akaplya, rogyar and DrewML July 14, 2020 14:32
@nrkapoor nrkapoor modified the milestones: 2.4.1, 2.4.2 Aug 15, 2020
@nrkapoor
Copy link

@magento run all tests

@cpartica
Copy link
Contributor

cpartica commented Nov 4, 2020

hey guys, why not do this?
#28578
This is introducing a new query, and it's not good...

@DrewML
Copy link

DrewML commented Nov 17, 2020

@cpartica we had a discussion recently about this one offline. I think your preference was to go the route of the Node interface we discussed instead, right?

IOW, ProductInterface and other entities implement this (rough) interface

interface Node {
   uid: ID!
}

and then we add a new root field:

type Query {
   node(id: ID!): Node
}

Is that right?

@gabrieldagama gabrieldagama added the Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. label Mar 23, 2021
@cpartica
Copy link
Contributor

cpartica commented Sep 7, 2021

we actually want this: magento/architecture#477

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests Award: category of expertise Component: CatalogGraphQl Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: review Release Line: 2.4
Projects
Status: Review in Progress