Remix

How to Get Products Upto 250 Without Pagination in Shopify Remix App?

In this blog post, we’ll show you how to get products upto 250 without pagination in Shopify Remix app. We have found that getting all products from the store using API is very complex and if possible then pagination is required for it, but we have another way to get it easily!

Prerequisites:

Before proceeding, ensure that the user has the necessary permissions and write access scope to get products in the Shopify store.

How to Get Products Upto 250 Without Pagination in Shopify Remix App?

We need to set code for getting all products without pagination which needs a cursor and GraphQL query to complete it. Here’s how you can do it:

import React from "react";
import { json } from "@remix-run/node";
import {useLoaderData} from "@remix-run/react";
import shopify from "../shopify.server";

export async function loader({ request }) {
    const { admin } = await shopify.authenticate.admin(request);
    const url = new URL(request.url);
    const searchParam = url.searchParams;
    const initialCursor = searchParam.get("cursor");
    const pageSize = 250;
    let hasNextPage = true;
    let products = [];
    const fetchProductPage = async (cursor) => {
    const graphqlString = `
    {
           products(first: ${pageSize}${cursor ? `, after: "${cursor}"` : ""}) {
                  pageInfo {
                         endCursor
                         hasNextPage
                  }
                  nodes {
                           id
                           title
                           status
                           vendor
                           totalInventory
                  }
         }
}`;

const response = await admin.graphql(graphqlString);
const result = await response.json();
     if (result.errors) {
         throw new Error(
             `GraphQL request failed: ${JSON.stringify(result.errors)}`
          );
      }
      products = products.concat(result.data.products.nodes);
      hasNextPage = result.data.products.pageInfo.hasNextPage;

      return result.data.products.pageInfo.endCursor;
};
const cursors = [];
let currentCursor = initialCursor;

while (hasNextPage) {
        currentCursor = await fetchProductPage(currentCursor);
        cursors.push(currentCursor);
}
return json({ products,cursors,});
}

export default function Index() {
     const { products } = useLoaderData();
     console.log(products);
      return (
           <></>
       );
}

 

This code works for getting all products using GraphQL query and now you can use all products at any place you want.

Output:

Here is a screenshot given below that shows received products from GraphQL query.

Conclusion:

Getting products without pagination is a very useful thing to get updated and synchronized with all your products. For that reason, you can handle all your stores using just a single GraphQL query.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Organic Traffic Drop: Sudden Decline Causes and Solutions

A sudden drop in organic traffic can be alarming for any business. Organic traffic is…

14 hours ago

Difference Between: Magento 2 WhatsApp Order Notification vs. Magento 2 SMS Notification

In the fast-paced world of eCommerce, effective communication is key to maintaining customer satisfaction and…

14 hours ago

Understanding useSearchParams vs useParams Hooks in Remix

In the Remix framework, handling URL parameters is a common task when building dynamic web…

1 day ago

How to Implement Frontend Design Customization in Shopify Remix App?

In this blog post, we'll show you how to implement frontend design customization in the…

3 days ago

Your Ultimate Guide to Shopify Headless Commerce

Starting an eCommerce business with Shopify can be easy yet challenging in many different ways,…

5 days ago

Magento 2: How to Show Custom Notice Message Before Payment Step on Checkout

Hello Magento Friends, In Magento 2, you can customize the checkout process to enhance the…

6 days ago