๐ฆ FetchSearchResult
TypeScript interface defining the return value structure of the getSearchResult()
function.
๐ Interface Definitionโ
interface FetchSearchResult {
products: Product[];
pageInfo: ProductsPageInfo | null;
availableFilters?: FilterGroup[];
totalCount?: number;
searchTerm: string;
error: string | null;
}
๐ Propertiesโ
products
โ
- Type:
Product[]
- Description: Array of products matching the search criteria
- Always present: Yes (empty array if no results)
pageInfo
โ
- Type:
ProductsPageInfo | null
- Description: Pagination information for cursor-based navigation
- Null when: No results or pagination not applicable
availableFilters
โ
- Type:
FilterGroup[]
- Description: Dynamic filters available based on current search results
- Optional: Yes (may not be present in all responses)
totalCount
โ
- Type:
number
- Description: Total number of products matching the search
- Optional: Yes (may not be present in all responses)
searchTerm
โ
- Type:
string
- Description: The search query that was executed
- Always present: Yes
error
โ
- Type:
string | null
- Description: Error message if the search failed
- Null when: Search was successful
๐ Related Typesโ
Product
โ
interface Product {
id: string;
title: string;
handle: string;
descriptionHtml: string;
featuredImage: Image | null;
images: Image[];
variants: Variant[];
price: {
amount: number;
currencyCode: string;
};
compareAtPrice: {
amount: number;
currencyCode: string;
} | null;
metafields: Record<string, any>;
}
ProductsPageInfo
โ
interface ProductsPageInfo {
hasNextPage: boolean;
hasPreviousPage: boolean;
startCursor: string | null;
endCursor: string | null;
}
FilterGroup
โ
interface FilterGroup {
id: string;
label: string;
type: "LIST" | "PRICE_RANGE" | "BOOLEAN";
values: FilterValue[];
}
interface FilterValue {
id: string;
label: string;
count: number;
input: any; // Filter input object for applying this filter
}
Variant
โ
interface Variant {
id: string;
productTitle: string;
variantTitle: string;
price: {
amount: number;
currencyCode: string;
};
compareAtPrice: {
amount: number;
currencyCode: string;
} | null;
metafields: Record<string, any>;
}
Image
โ
interface Image {
id: string;
url: string;
altText: string | null;
width: number;
height: number;
}
๐งช Usage Examplesโ
Basic Result Handlingโ
const { data, error } = await getSearchResult({
query: "solar panels",
limit: 12,
});
if (error) {
console.error("Search failed:", error);
return;
}
// data is of type FetchSearchResult
console.log(`Found ${data.totalCount} results for "${data.searchTerm}"`);
console.log(`Showing ${data.products.length} products`);
data.products.forEach((product) => {
console.log(`${product.title} - $${product.price.amount}`);
});
Pagination Handlingโ
const result = await getSearchResult({
query: "solar kit",
limit: 10,
});
if (result.pageInfo?.hasNextPage) {
// Fetch next page
const nextPage = await getSearchResult({
query: "solar kit",
limit: 10,
cursor: result.pageInfo.endCursor,
});
}
Filter Processingโ
const result = await getSearchResult({
query: "renewable energy",
limit: 20,
});
// Process available filters for UI
result.availableFilters?.forEach((filterGroup) => {
console.log(`Filter: ${filterGroup.label}`);
filterGroup.values.forEach((value) => {
console.log(` ${value.label} (${value.count})`);
});
});
Error Handlingโ
const result = await getSearchResult({
query: "", // Invalid empty query
});
if (result.error) {
// Handle specific error cases
switch (result.error) {
case "Search query cannot be empty":
// Show validation message
break;
case "Search failed":
// Show generic error
break;
default:
// Show unexpected error
break;
}
}
Metafield Accessโ
const result = await getSearchResult({
query: "solar panels",
productMetafields: [
{ field: "custom.warranty_years", type: "number_integer" },
{ field: "custom.efficiency", type: "number_decimal" },
],
options: {
camelizeKeys: true,
},
});
result.products.forEach((product) => {
// Access metafields (camelized keys)
const warranty = product.metafields.customWarrantyYears;
const efficiency = product.metafields.customEfficiency;
console.log(
`${product.title}: ${warranty} year warranty, ${efficiency}% efficiency`
);
});
๐จ Error Statesโ
Common error messages you might encounter:
"Search query cannot be empty"
- Query parameter is empty or whitespace"Search failed"
- General search failure from Shopify API"Unexpected error"
- Unexpected runtime error during search- Network-related errors from the underlying fetch operation
โ Back to: Search Overview โ
Description
TypeScript interface for getSearchResult return value.