useShopQuery equilavent in v2 docs (when in components) #594
-
I can't seem to find a way to solve this problem in the new Hydrogen v2. I currently use Sanity, and use it to query for modules that include images, text components etc.. For example consider this Image module that takes Sanity data:
And the Module component here:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You'll either need to traverse your sanity data within your loader and enrich it with data from Shopify, or you'll need to make browser requests directly to the storefront API from your React components when they render on the client. If it were me, I'd go with the first option 😄. It requires some legwork on your part, but it means that you do all of your data fetching on the server which keeps your load speeds fast. |
Beta Was this translation helpful? Give feedback.
-
As @davidhousedev pointed out there are two options: Option A.Move the
Option B.Create a resource/api endpoint |
Beta Was this translation helpful? Give feedback.
-
Hi all, this is exactly the situation I am facing. I have seen an example of Option B in the demo store but I am interested in implementing Option A. Would anyone be able to give a basic example of how I could loop through Sanity modules and enrich the data with a Shopify query? |
Beta Was this translation helpful? Give feedback.
As @davidhousedev pointed out there are two options:
Option A.
Move the
useShopQuery
logic from theImageModule
to theloader
(after your sanity query) and return the enriched variants with shopify variants. This will make the variants:await
the Shopify sub-queries (recommended above the fold) ordefer
the shopify sub-queries (recommended if below the fold).Option B.
Create a resource/api endpoint
/app/routes/api/product-from-gid
and implement auseFetcher.load
in theImageModule
to fetch the product/variant based on the id from Sanity. This will be CSR only, so you could render a placeholder during loading.