Call hydrogen's storefront.graphql directly, drop the compat shim

Completes the migration started in f8c3ef9. That commit put hydrogen
underneath the existing shopifyFetch wrapper; this one removes the
wrapper so every query uses the package's own client API.

- All 22 call sites now call storefront.graphql(DOCUMENT, { variables })
  (cachedStorefront for shop policies) instead of shopifyFetch, which
  emulated the old client's {query, variables} -> {data} shape.
- shopifyFetch is replaced by unwrapStorefrontResult(result, operation),
  which is only an error policy, not a transport wrapper: it returns
  data and throws when Shopify reports GraphQL errors, since hydrogen
  returns those rather than throwing. Naming the operation means a
  failure points at the call site instead of just at "Shopify".
- Environment reads move to services/shopify/config, so client.ts is
  only the hydrogen clients and shop-pay-button no longer imports from
  a query module just to get the store domain.
- Removes @shopify/storefront-api-client, the superseded client. It had
  no importers.

The SHOPIFY_STOREFRONT_API_URL export is gone too — hydrogen builds the
endpoint from storeDomain and apiVersion, and nothing else used it.

No behaviour change intended: same documents, same env var names, same
2025-07 API pin, same caching split between the two clients.

yarn typecheck passes with 0 errors. Verified against mock.shop: build
prerenders the policy pages, and in the browser product grid, search,
collection filters, cart restore, quantity update and discount-code
apply all work with no console errors. Customer account flows remain
untested — they need real credentials.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JaxgqPbFxLsSuLPZom2kdC
This commit is contained in:
Rami Bitar
2026-08-08 10:38:12 -04:00
co-authored by Claude Opus 5
parent f8c3ef9e0e
commit fe78293312
9 changed files with 204 additions and 202 deletions
+41 -31
View File
@@ -1,7 +1,7 @@
'use client';
import { create } from 'zustand';
import { shopifyFetch } from '@/services/shopify/client';
import { storefront, unwrapStorefrontResult } from '@/services/shopify/client';
import {
CREATE_CART_MUTATION,
ADD_CART_LINES_MUTATION,
@@ -119,69 +119,79 @@ function unwrapCartPayload<T>(
}
async function createCartApi(lines: CartLineInput[] = []): Promise<Cart> {
const response = await shopifyFetch({
query: CREATE_CART_MUTATION,
variables: { lines: lines.length > 0 ? lines : null },
});
const data = unwrapStorefrontResult(
await storefront.graphql(CREATE_CART_MUTATION, {
variables: { lines: lines.length > 0 ? lines : null },
}),
'CreateCart'
);
return unwrapCartPayload(response.data.cartCreate);
return unwrapCartPayload(data.cartCreate);
}
async function addCartLinesApi(
cartId: string,
lines: CartLineInput[]
): Promise<Cart> {
const response = await shopifyFetch({
query: ADD_CART_LINES_MUTATION,
variables: { cartId, lines },
});
const data = unwrapStorefrontResult(
await storefront.graphql(ADD_CART_LINES_MUTATION, {
variables: { cartId, lines },
}),
'AddCartLines'
);
return unwrapCartPayload(response.data.cartLinesAdd);
return unwrapCartPayload(data.cartLinesAdd);
}
async function updateCartLinesApi(
cartId: string,
lines: CartLineUpdateInput[]
): Promise<Cart> {
const response = await shopifyFetch({
query: UPDATE_CART_LINES_MUTATION,
variables: { cartId, lines },
});
const data = unwrapStorefrontResult(
await storefront.graphql(UPDATE_CART_LINES_MUTATION, {
variables: { cartId, lines },
}),
'UpdateCartLines'
);
return unwrapCartPayload(response.data.cartLinesUpdate);
return unwrapCartPayload(data.cartLinesUpdate);
}
async function removeCartLinesApi(
cartId: string,
lineIds: string[]
): Promise<Cart> {
const response = await shopifyFetch({
query: REMOVE_CART_LINES_MUTATION,
variables: { cartId, lineIds },
});
const data = unwrapStorefrontResult(
await storefront.graphql(REMOVE_CART_LINES_MUTATION, {
variables: { cartId, lineIds },
}),
'RemoveCartLines'
);
return unwrapCartPayload(response.data.cartLinesRemove);
return unwrapCartPayload(data.cartLinesRemove);
}
async function updateCartDiscountCodesApi(
cartId: string,
discountCodes: string[]
): Promise<Cart> {
const response = await shopifyFetch({
query: UPDATE_CART_DISCOUNT_CODES_MUTATION,
variables: { cartId, discountCodes },
});
const data = unwrapStorefrontResult(
await storefront.graphql(UPDATE_CART_DISCOUNT_CODES_MUTATION, {
variables: { cartId, discountCodes },
}),
'UpdateCartDiscountCodes'
);
return unwrapCartPayload(response.data.cartDiscountCodesUpdate);
return unwrapCartPayload(data.cartDiscountCodesUpdate);
}
async function getCartApi(cartId: string): Promise<Cart | null> {
const response = await shopifyFetch({
query: GET_CART_QUERY,
variables: { cartId },
});
const data = unwrapStorefrontResult(
await storefront.graphql(GET_CART_QUERY, { variables: { cartId } }),
'GetCart'
);
return response.data.cart;
return data.cart;
}
export function redirectToCheckout(checkoutUrl: string): void {