Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pieroenrico/tune-me-in/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheuseSanityQuery hook streamlines the process of fetching data from Sanity CMS while automatically resolving any referenced Shopify products. It queries your Sanity dataset using GROQ, identifies Shopify product references, and fetches fresh product data from the Shopify Storefront API.
This hook is provided by the hydrogen-plugin-sanity package.
Import
Basic Usage
src/pages/Index.server.jsx
Parameters
A GROQ query string to fetch data from Sanity. The query should return any referenced Shopify product IDs (keyed to either
_ref or _id).Parameters to pass to the GROQ query. These are used for variable substitution in your query.
Custom callback to control which products to fetch and what data to retrieve for each product.Function signature:Return values:Example - Skip Shopify queries:
true- Fetch default product data usingProductProviderFragmentfalse- Skip fetching data for this productstring- Custom GraphQL fragment for this product
Return Value
Returns an object with two properties:The complete payload from your Sanity query, with all data structures preserved exactly as queried.
A normalized object containing all Shopify products fetched from the Storefront API, keyed by their Sanity product ID.
Usage Examples
Fetching Collection Products
src/pages/collections/[handle].server.jsx
Custom Product Fragment
src/pages/Index.server.jsx
Sanity-Only Query (No Shopify Products)
src/pages/about.server.jsx
Using with ProductsProvider
For editorial pages with inline product references, combineuseSanityQuery with ProductsProvider to make Shopify products available throughout your component tree:
src/pages/editorial/[handle].server.jsx
How It Works
- Query Sanity: The hook executes your GROQ query against your Sanity dataset
- Extract Product IDs: It scans the response for Shopify product references (IDs starting with
shopifyProduct-) - Fetch from Shopify: For each referenced product, it queries the Shopify Storefront API
- Return Combined Data: You receive both the original Sanity data and a normalized object of Shopify products
Performance Considerations
- By default, uses
ProductProviderFragmentwhich fetches comprehensive product data - For pages with many products, use
getProductGraphQLFragmentto fetch only necessary fields - The hook won’t make Storefront API requests if no product references are found
- Consider implementing caching strategies for production use
Related
- useProductsContext - Access Shopify products in nested components
- ProductsProvider - Context provider for Shopify products