Next.js Discord

Discord Forum

I need help with integrating NextJS with Shopify.

Unanswered
Kurilian Bobtail posted this in #help-forum
Open in Discord
Kurilian BobtailOP
I've been using nextjs for a while now, my website is created with it. But so far, I've only created static websites. I read the docs about data fetching, caching, and rendering, and now I want to put my new skills into action.

I also read the GraphQL docs, but I always get lost when I try to integrate nextjs with shopify. That's why I need a little bit of help to get things started. I tried multiple times with a shopify development account, but I just can't do it.

If you've got sometime, please help me. I'd be incredibly grateful, and I promise you, I'll always try my best to provide you with some value back. TY

10 Replies

Asian black bear
You are welcome to ask concrete and actionable questions in the help forum but this post is pretty much useless as you haven't shared any details at all. Just saying "please somebody help me implement X" won't get you far.
Kurilian BobtailOP
True, true.


If you can point me to a direction other than the docs, that would be amazing.
I'm trying to connect the storefront api with a new next app.
Lemme share some of the code.
/lib/shopify.jsx
import { GraphQLClient } from 'graphql-request';

const client = new GraphQLClient(`https://${process.env.SHOPIFY_STORE_DOMAIN}/api/2023-07/graphql.json`, {
  headers: {
    'X-Shopify-Storefront-Access-Token': process.env.SHOPIFY_STOREFRONT_ACCESS_TOKEN,
  },
});
export async function fetchShopifyData(query, variables = {}) {
    try {
      return await client.request(query, variables);
    } catch (error) {
      console.error('Shopify API request failed:', error);
      throw error;
    }
  }
export default client;
import { fetchShopifyData } from './lib/shopify';
import Image from 'next/image';

const PRODUCTS_QUERY = `
  {
    products(first: 10) {
      edges {
        node {
          id
          title
          description
          handle
          images(first: 1) {
            edges {
              node {
                src
              }
            }
          }
        }
      }
    }
  }
`;

export default async function Home() {
  try {
    const data = await fetchShopifyData(PRODUCTS_QUERY);
    return (
      <div>
        <h1 className="text-3xl font-bold">Products</h1>
        <ul>
          {data.products.edges.map(({ node }) => (
            <li key={node.id} className="m-4 p-4 border rounded">
              <h2>{node.title}</h2>
              <p>{node.description}</p>
              {node.images.edges[0] && (
                <Image
                  src={node.images.edges[0].node.url}
                  alt={node.title}
                  width={500}
                  height={500}
                />
              )}
            </li>
          ))}
        </ul>
      </div>
    );
  } catch (error) {
    console.error('Failed to fetch products: ', error);
    return <p>Error loading products</p>;
  }
}

Page.jex
.jsx*
What's rendered on my screen is the h1 tag that says products.
But yeah, I wanna learn this in details, and the documentation is not cutting it for me. I can't understand it well enough yet.. So.. if you can point me to the right direction, I'll be thankful.