How to Fetch and Display Product Images in Shopify Hydrogen Using GraphQL
Shopify Hydrogen is a React-based framework used to build custom headless Shopify storefronts. When developing a Hydrogen storefront, displaying product images is an important part of creating a professional shopping experience.
In this tutorial, we will learn how to fetch product images from Shopify using GraphQL and the Shopify Storefront API. We will then display those images using React and style them with responsive CSS.
This is a practical, step-by-step tutorial. You can follow each step, copy the code, and implement it directly in your Hydrogen project.
What You Will Learn
How to create a product images route in Shopify Hydrogen.
How to write a GraphQL query to fetch products and images.
How to retrieve Shopify product data using a loader.
How to display product images using React.
How to create a responsive product image grid using CSS.
How to handle products without images.
Project Folder Structure
Before writing the code, let’s understand which files we need to create.
Inside your Hydrogen project, create the following files:
your-hydrogen-project/
│
├── app/
│ ├── routes/
│ │ └── product-images.jsx
│ │
│ └── styles/
│ └── product-images.css
│
└── ...File responsibilities:
product-images.jsx– Contains the GraphQL query, loader function, and React UI.product-images.css– Contains the styling for the product image grid.
Step 1: Create a Product Images Route
First, we need to create a new route where our product images will be displayed.
Open your Hydrogen project and navigate to:
app/routes/
Create a new file named:
product-images.jsx
This file will contain all the code required to fetch and display product images.
At this stage, we are only creating a new file. No Shopify products are fetched yet.
Step 2: Write a GraphQL Query to Fetch Product Images
Now we need to tell Shopify which product information we want to retrieve.
We will use the Shopify Storefront API and GraphQL to request:
Product ID
Product title
Product handle
Product image URL
Image alt text
Image width and height
Open app/routes/product-images.jsx and add the following code at the top:
const PRODUCT_IMAGES_QUERY = `#graphql
query ProductImages {
products(first: 12) {
nodes {
id
title
handle
images(first: 4) {
nodes {
id
url
altText
width
height
}
}
}
}
}
`;How does this code work?
products(first: 12)requests up to 12 products.nodescontains the returned product records.titleretrieves the product name.images(first: 4)requests up to four images per product.urlprovides the image URL.altTextprovides the image description.widthandheightprovide the image dimensions.
The query requests up to four images, but a product may have fewer images.
Step 3: Fetch Product Data Using the Hydrogen Loader
Writing a GraphQL query is not enough. We also need to execute that query and retrieve the response from Shopify.
In Hydrogen, we can use a loader function to fetch data on the server.
Add the following code below the GraphQL query in the same file:
File: app/routes/product-images.jsx
export async function loader({context}) {
const response = await context.storefront.query(
PRODUCT_IMAGES_QUERY,
);
return {
products: response.products.nodes,
};
}How does this code work?
loader()runs when the route needs its data.contextprovides access to Hydrogen’s server-side utilities.context.storefront.query()executes the GraphQL query.response.products.nodescontains the products returned by Shopify.returnmakes the product data available to the route component.
After this step, the route has the product data needed to display the images.
Step 4: Display Product Images Using React
Now that we have fetched the product data, we need to display it on the webpage.
We will use React’s useLoaderData() hook to access the data returned by the loader.
Add the following import at the top of product-images.jsx:
import {useLoaderData} from 'react-router';Now add this React component below the loader function:
export default function ProductImages() {
const {products} = useLoaderData();
return (
<div className="product-images-page">
<div className="product-images-header">
<h1>Shopify Product Images</h1>
<p>
Products and their images fetched using
Shopify Storefront API.
</p>
</div>
<div className="product-images-grid">
{products.map((product) => {
const images = product.images.nodes;
return (
<div className="product-image-card" key={product.id}>
<div className="product-main-image">
{images.length > 0 ? (
<img
src={images[0].url}
alt={images[0].altText || product.title}
/>
) : (
<div className="no-product-image">
No Image Available
</div>
)}
</div>
<div className="product-image-details">
<h2>{product.title}</h2>
<div className="product-thumbnails">
{images.slice(1).map((image) => (
<img
key={image.id}
src={image.url}
alt={image.altText || product.title}
/>
))}
</div>
</div>
</div>
);
})}
</div>
</div>
);
}How does this code work?
useLoaderData()retrieves the products returned by the loader.products.map()loops through all fetched products.product.images.nodesgets the images of the current product.images[0]displays the first image as the main image.images.slice(1)displays the remaining images as thumbnails.The conditional check displays a fallback message if no image exists.
Step 5: Import the CSS File
We now need to connect our CSS file to the React route.
At the top of product-images.jsx, add:
import '../styles/product-images.css';This imports the CSS styles that we will create in the next step.
Step 6: Design the Product Image Grid Using CSS
Now let’s make the product display look professional.
Navigate to:
app/styles/
Create a new file named:
product-images.css
Add the following CSS:
.product-images-page {
max-width: 1400px;
margin: 0 auto;
padding: 50px 24px;
}
.product-images-header {
text-align: center;
margin-bottom: 40px;
}
.product-images-header h1 {
font-size: 34px;
font-weight: 700;
margin-bottom: 10px;
}
.product-images-header p {
color: #777;
font-size: 15px;
}
.product-images-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 24px;
}
.product-image-card {
overflow: hidden;
border: 1px solid #e8e8e8;
border-radius: 12px;
background: #fff;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.product-image-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
}
.product-main-image {
height: 260px;
padding: 15px;
background: #f8f8f8;
display: flex;
align-items: center;
justify-content: center;
}
.product-main-image img {
width: 100%;
height: 100%;
object-fit: contain;
}
.no-product-image {
color: #777;
font-size: 14px;
}
.product-image-details {
padding: 16px;
}
.product-image-details h2 {
font-size: 16px;
font-weight: 600;
margin: 0 0 14px;
}
.product-thumbnails {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.product-thumbnails img {
width: 55px;
height: 55px;
padding: 4px;
object-fit: contain;
border: 1px solid #e5e5e5;
border-radius: 6px;
}
@media (max-width: 1024px) {
.product-images-grid {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
@media (max-width: 768px) {
.product-images-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
}
.product-images-page {
padding: 30px 15px;
}
.product-main-image {
height: 200px;
}
}
@media (max-width: 480px) {
.product-images-grid {
grid-template-columns: 1fr;
}
.product-images-header h1 {
font-size: 26px;
}
}What does this CSS do?
Creates a four-column product grid on desktop.
Adjusts the grid for tablet and mobile screens.
Adds borders and rounded corners to product cards.
Adds a hover animation.
Uses
object-fit: containto display the complete product image without unwanted cropping.
Step 7: Register and Open Your Route
If your Hydrogen project uses file-based route discovery, the route may be available automatically.
If you use manual routing, register the new route in your existing app/routes.ts file according to your project’s current routing setup. Do not replace your existing routes.
Start your development server:
npm run devNow open the following route on your local development server:
http://localhost:3000/product-imagesReplace 3000 with the port shown in your terminal if your development server uses a different port.
You should now see your Shopify products displayed in a responsive image grid.
Common Issues and Solutions
Product images are not displaying
Check whether the GraphQL response contains image URLs. Also verify that the product has images assigned in Shopify Admin.
GraphQL query is returning an error
Check your Storefront API configuration and make sure the query is supported by your Shopify API version.
Images are appearing stretched
Use object-fit: contain in your CSS to preserve the complete image.
The route is showing a 404 error
Check your route file name and your project’s route registration configuration.
Frequently Asked Questions
How do I fetch product images in Shopify Hydrogen?
You can use a GraphQL query through the Shopify Storefront API. Request the images field inside the products query to retrieve product image URLs.
Can I fetch multiple images for a Shopify product?
Yes. You can request multiple images using images(first: 4). This example requests up to four images for each product.
Which API is used to fetch products in Shopify Hydrogen?
Shopify Hydrogen uses the Shopify Storefront API to retrieve product information through GraphQL.
Can I display Shopify product images using React?
Yes. You can use the image URLs returned by the Storefront API inside React’s img element.
Is Shopify Hydrogen suitable for creating custom product galleries?
Yes. Hydrogen gives developers control over the storefront, allowing them to create custom product galleries, sliders, thumbnails, and responsive layouts.
Conclusion
In this tutorial, we learned how to fetch and display Shopify product images using GraphQL and the Shopify Storefront API.
We created a new Hydrogen route, wrote a GraphQL query, fetched product data using a loader, displayed images with React, and styled the product grid using responsive CSS.
You can now use this approach as a foundation for building more advanced product galleries and custom Shopify storefront experiences.
Explore more practical Shopify Hydrogen tutorials to improve your headless Shopify development skills.

Comments & Reviews
Share your experience, ask a question, or help other readers.
Start the conversation
Be the first to share feedback on this tutorial.