The response object to validate
Type predicate indicating if the response is a valid ShopifySearchResponse
const response = await fetch(shopifyGraphQLUrl, { method: "POST", body: query });
const json = await response.json();
if (isValidShopifySearchResponse(json)) {
const products = json.data.products.edges.map(e => e.node);
console.log(`Found ${products.length} products`);
}
export function isValidShopifySearchResponse(response: unknown): response is ShopifySearchResponse {
const parsed = shopifySearchResponseSchema.safeParse(response);
if (!parsed.success) {
console.warn("isValidShopifySearchResponse: response is not a valid ShopifySearchResponse", {
response,
parsed,
error: parsed.error,
issues: parsed.error.issues,
});
}
return parsed.success;
}
Type guard to validate if a response from the Shopify GraphQL API is a valid ShopifySearchResponse. Checks for the nested data.products.edges structure and validates all product nodes within.