In this blog, you can see how to get all the product collections using GraphQL in Magento 2.
Step 1:
First of all, we need to create a registration.php file in app/code/Webi/GraphQL/ directory to register our module by adding the following code.
1 2 3 4 5 6 7 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Webi_GraphQL', __DIR__ ); |
Step 2:
Now, we need to create a module.xml file in app/code/Webi/GraphQL/etc/ directory and add the following code.
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webi_GraphQL"> <sequence> <module name="Magento_Sales"/> <module name="Magento_GraphQl"/> </sequence> </module> </config> |
Step 3:
In this step is now to create schema.graphqls file in app/code/Webi/GraphQL/etc/ directory and add the following code.
app/code/Webi/GraphQL/etc/ directory and add the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
type Query { productCollection: ProductCollection @resolver(class: "Webi\\GraphQL\\Model\\Resolver\\ProductsResolver") @doc(description: "Get Product collection of a store") } type ProductCollection @doc(description: "product collection") { allProducts: [ProductRecord] @doc(description: "Product records with sku,name and price") } type ProductRecord { id: Int @doc(description: "To get Product Id") sku: String @doc(description: "To get Product sku") name: String @doc(description: "To get Product name") price: Float @doc(description: "To get Product price") } |
Step 4:
Lastly, we need to create ProductsResolver.php as a resolver file in app/code/Webi/GraphQL/Model/Resolver/ and add the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php namespace Webi\GraphQL\Model\Resolver; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; class ProductsResolver implements ResolverInterface { public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder ) { $this->productRepository = $productRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; } public function resolve( Field $field, $context, ResolveInfo $info, array $value = null, array $args = null ) { $productsData = $this->getProductsData(); return $productsData; } private function getProductsData() { try { /* filter for all the pages */ $searchCriteria = $this->searchCriteriaBuilder->addFilter('entity_id', 1,'gteq')->create(); $products = $this->productRepository->getList($searchCriteria)->getItems(); $productRecord['allProducts'] = []; foreach($products as $product) { $productId = $product->getId(); $productRecord['allProducts'][$productId]['id'] = $product->getId(); $productRecord['allProducts'][$productId]['sku'] = $product->getSku(); $productRecord['allProducts'][$productId]['name'] = $product->getName(); $productRecord['allProducts'][$productId]['price'] = $product->getPrice(); } } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } return $productRecord; } } |
Now, execute the following commands:
1 2 3 4 5 6 |
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f en_US php bin/magento cache:flush php bin/magento cache:clean php bin/magento indexer:reindex |
Now, you need to pass the query required data in the Request Body.
1 2 3 4 5 6 7 8 9 10 |
{ productCollection{ allProducts{ id name price sku } } } |
We hope this post is very helpful to you.