-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Okay, I've figured by myself. const PRODUCT_IMAGE_FRAGMENT = `#graphql
fragment ProductMediaImage on MediaImage {
__typename
mediaContentType
image {
__typename
id
url
altText
width
height
}
}
` as const;
const PRODUCT_EXTERNAL_VIDEO_FRAGMENT = `#graphql
fragment ProductExternalVideo on ExternalVideo {
mediaContentType
id
embedUrl
host
originUrl
presentation {
id
}
previewImage {
id
url
width
height
altText
}
}
` as const;
const PRODUCT_VIDEO_FRAGMENT = `#graphql
fragment ProductVideo on Video {
id
mediaContentType
previewImage {
id
url
width
height
altText
}
}
` as const;
const PRODUCT_FRAGMENT = `#graphql
fragment Product on Product {
id
title
vendor
handle
descriptionHtml
description
options {
name
values
}
media(first: 10) {
nodes {
...ProductMediaImage
...ProductExternalVideo
...ProductVideo
}
}
selectedVariant: variantBySelectedOptions(selectedOptions: $selectedOptions, ignoreUnknownOptions: true, caseInsensitiveMatch: true) {
...ProductVariant
}
variants(first: 1) {
nodes {
...ProductVariant
}
}
seo {
description
title
}
}
${PRODUCT_VARIANT_FRAGMENT}
${PRODUCT_IMAGE_FRAGMENT}
${PRODUCT_EXTERNAL_VIDEO_FRAGMENT}
${PRODUCT_VIDEO_FRAGMENT}
` as const;Initially I was struggling to make this work because:
So either Shopify documentation or Hydrogen implementation is wrong. If you look at this object on Shopify Docs: You will see it has a If you open node_modules, you can see the Image type was defined with a Technically, it would not be a problem, since __typename is a technical field from GraphQL, and should be automatically added to every graphql object, but for some reason in Hydrogen this is not as seemeless as one could think, so I had to add it manually. I've fixed the problem by adding |
Beta Was this translation helpful? Give feedback.
-
|
I feel your pain I spent an entire weekend fighting with the ProductFragment and Storefront.query types when I first started. The trick is usually in how the codegen handles those fragments. If you've updated the Fragment but your IDE isn't seeing the new fields (like images), make sure you've re-run npm run graphql-codegen so the TypeScript definitions actually match your new query. On a side note, if you're pulling in images to build things like custom color swatches or dynamic product galleries, handling those types in a headless setup can get incredibly complex fast. I’ve been working on Misk Smart Product Options, which is designed to handle that 'heavy lifting' on the frontend logic. It lets you import and display images/options without having to manually map every single GraphQL fragment and type definition for each new variant. Once you get the codegen synced, it gets a lot smoother hang in there |
Beta Was this translation helpful? Give feedback.




Okay, I've figured by myself.
This is the code that solved the problem: