Move Storefront queries onto @shopify/hydrogen preview client

Replaces the hand-rolled shopifyFetch wrapper and raw query strings with
the hydrogen preview package (0.0.0-preview-116d5d7-20260730141607), the
same build pinned in hydrogen-preview/.

- services/shopify/client: createStorefrontClient({type: "public"}) with a
  static request context. Two module-scoped clients — uncached for carts,
  products and customers; revalidate-3600 for shop policies — using the
  client's custom fetch option to carry Next's caching hints.
- graphql/*: every document wrapped in gql(), fragments composed via the
  second argument instead of string interpolation, and $country/$language
  declared with @inContext so hydrogen injects them.
- shopifyFetch keeps its {query, variables} call shape so call sites are
  unchanged, but is now generic over the document, so data is inferred. It
  re-raises GraphQL errors, which hydrogen returns rather than throws.

Env var names, the 2025-07 API version pin, and the client-side fetching
architecture are unchanged.

Turning on type coverage surfaced real defects, not just annotations:

- hydrogen gql check caught $discountCodes: [String!] used where the field
  requires [String!]!.
- Cart and customer mutation payloads are nullable and were dereferenced
  unconditionally, so a failed mutation threw a TypeError. Adds a shared
  unwrapCartPayload helper (collapsing five copies of the same userErrors
  check) and explicit null handling in the customer service, so a null
  payload reads as an error rather than success with no errors.
- Search results are a Product | Page | Article union; adds __typename to
  the queries and narrows on it.
- Widens nullable fields (altText, image, customer.email, totalTaxAmount)
  in the domain interfaces and in the structural duplicates some
  components declare locally.

Adds a typecheck script (tsc --noEmit && hydrogen gql check); it passes
with 0 errors. Two deprecation warnings are left alone as acting on them
would change behaviour: ProductOption.values and CartCost.totalTaxAmount.

Verified against mock.shop: build prerenders the policy pages through the
cached client, and in the browser the product grid, search with facets,
collection filter round-trip, add-to-cart and quantity update all work.
Customer account flows are 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:24:38 -04:00
co-authored by Claude Opus 5
parent 95b191eeb8
commit f8c3ef9e0e
21 changed files with 468 additions and 215 deletions
+46 -26
View File
@@ -1,5 +1,7 @@
import { gql } from '@shopify/hydrogen';
// Cart Fragment for consistent cart data
const CartFragment = `
const CartFragment = gql(`
fragment CartFragment on Cart {
id
checkoutUrl
@@ -64,12 +66,13 @@ const CartFragment = `
}
}
}
`;
`);
// Create a new cart
export const CREATE_CART_MUTATION = `
${CartFragment}
mutation CreateCart($lines: [CartLineInput!]) {
export const CREATE_CART_MUTATION = gql(
`
mutation CreateCart($lines: [CartLineInput!], $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
cartCreate(input: { lines: $lines }) {
cart {
...CartFragment
@@ -80,12 +83,15 @@ export const CREATE_CART_MUTATION = `
}
}
}
`;
`,
[CartFragment]
);
// Add lines to cart
export const ADD_CART_LINES_MUTATION = `
${CartFragment}
mutation AddCartLines($cartId: ID!, $lines: [CartLineInput!]!) {
export const ADD_CART_LINES_MUTATION = gql(
`
mutation AddCartLines($cartId: ID!, $lines: [CartLineInput!]!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
cartLinesAdd(cartId: $cartId, lines: $lines) {
cart {
...CartFragment
@@ -96,12 +102,15 @@ export const ADD_CART_LINES_MUTATION = `
}
}
}
`;
`,
[CartFragment]
);
// Update cart lines
export const UPDATE_CART_LINES_MUTATION = `
${CartFragment}
mutation UpdateCartLines($cartId: ID!, $lines: [CartLineUpdateInput!]!) {
export const UPDATE_CART_LINES_MUTATION = gql(
`
mutation UpdateCartLines($cartId: ID!, $lines: [CartLineUpdateInput!]!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
cartLinesUpdate(cartId: $cartId, lines: $lines) {
cart {
...CartFragment
@@ -112,12 +121,15 @@ export const UPDATE_CART_LINES_MUTATION = `
}
}
}
`;
`,
[CartFragment]
);
// Remove lines from cart
export const REMOVE_CART_LINES_MUTATION = `
${CartFragment}
mutation RemoveCartLines($cartId: ID!, $lineIds: [ID!]!) {
export const REMOVE_CART_LINES_MUTATION = gql(
`
mutation RemoveCartLines($cartId: ID!, $lineIds: [ID!]!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
cartLinesRemove(cartId: $cartId, lineIds: $lineIds) {
cart {
...CartFragment
@@ -128,12 +140,15 @@ export const REMOVE_CART_LINES_MUTATION = `
}
}
}
`;
`,
[CartFragment]
);
// Apply (or clear) discount codes on the cart
export const UPDATE_CART_DISCOUNT_CODES_MUTATION = `
${CartFragment}
mutation UpdateCartDiscountCodes($cartId: ID!, $discountCodes: [String!]) {
export const UPDATE_CART_DISCOUNT_CODES_MUTATION = gql(
`
mutation UpdateCartDiscountCodes($cartId: ID!, $discountCodes: [String!]!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
cartDiscountCodesUpdate(cartId: $cartId, discountCodes: $discountCodes) {
cart {
...CartFragment
@@ -144,14 +159,19 @@ export const UPDATE_CART_DISCOUNT_CODES_MUTATION = `
}
}
}
`;
`,
[CartFragment]
);
// Get cart by ID
export const GET_CART_QUERY = `
${CartFragment}
query GetCart($cartId: ID!) {
export const GET_CART_QUERY = gql(
`
query GetCart($cartId: ID!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
cart(id: $cartId) {
...CartFragment
}
}
`;
`,
[CartFragment]
);
@@ -1,8 +1,10 @@
import { gql } from '@shopify/hydrogen';
import { ProductFragment } from '@/graphql/products';
// Get all collections
export const GET_COLLECTIONS_QUERY = `
query GetCollections($first: Int!) {
export const GET_COLLECTIONS_QUERY = gql(`
query GetCollections($first: Int!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
collections(first: $first) {
edges {
node {
@@ -26,11 +28,11 @@ export const GET_COLLECTIONS_QUERY = `
}
}
}
`;
`);
// Get products in a collection
export const GET_COLLECTION_PRODUCTS_QUERY = `
${ProductFragment}
export const GET_COLLECTION_PRODUCTS_QUERY = gql(
`
query GetCollectionProducts(
$handle: String!
$first: Int!
@@ -38,7 +40,9 @@ export const GET_COLLECTION_PRODUCTS_QUERY = `
$sortKey: ProductCollectionSortKeys
$reverse: Boolean
$filters: [ProductFilter!]
) {
$country: CountryCode
$language: LanguageCode
) @inContext(country: $country, language: $language) {
collection(handle: $handle) {
id
title
@@ -82,4 +86,6 @@ export const GET_COLLECTION_PRODUCTS_QUERY = `
}
}
}
`;
`,
[ProductFragment]
);
+25 -20
View File
@@ -1,7 +1,8 @@
// Customer account operations (classic Storefront customer accounts).
// https://shopify.dev/docs/api/storefront/latest/objects/Customer
import { gql } from '@shopify/hydrogen';
const CustomerFragment = `
const CustomerFragment = gql(`
fragment CustomerFragment on Customer {
id
email
@@ -24,10 +25,10 @@ const CustomerFragment = `
phone
}
}
`;
`);
export const CUSTOMER_QUERY = `
${CustomerFragment}
export const CUSTOMER_QUERY = gql(
`
query GetCustomer($customerAccessToken: String!, $orderCount: Int!) {
customer(customerAccessToken: $customerAccessToken) {
...CustomerFragment
@@ -63,9 +64,11 @@ export const CUSTOMER_QUERY = `
}
}
}
`;
`,
[CustomerFragment]
);
export const CUSTOMER_CREATE_MUTATION = `
export const CUSTOMER_CREATE_MUTATION = gql(`
mutation CustomerCreate($input: CustomerCreateInput!) {
customerCreate(input: $input) {
customer {
@@ -79,9 +82,9 @@ export const CUSTOMER_CREATE_MUTATION = `
}
}
}
`;
`);
export const CUSTOMER_ACCESS_TOKEN_CREATE_MUTATION = `
export const CUSTOMER_ACCESS_TOKEN_CREATE_MUTATION = gql(`
mutation CustomerAccessTokenCreate($input: CustomerAccessTokenCreateInput!) {
customerAccessTokenCreate(input: $input) {
customerAccessToken {
@@ -95,9 +98,9 @@ export const CUSTOMER_ACCESS_TOKEN_CREATE_MUTATION = `
}
}
}
`;
`);
export const CUSTOMER_ACCESS_TOKEN_DELETE_MUTATION = `
export const CUSTOMER_ACCESS_TOKEN_DELETE_MUTATION = gql(`
mutation CustomerAccessTokenDelete($customerAccessToken: String!) {
customerAccessTokenDelete(customerAccessToken: $customerAccessToken) {
deletedAccessToken
@@ -107,10 +110,10 @@ export const CUSTOMER_ACCESS_TOKEN_DELETE_MUTATION = `
}
}
}
`;
`);
// Sends the "reset your password" email.
export const CUSTOMER_RECOVER_MUTATION = `
export const CUSTOMER_RECOVER_MUTATION = gql(`
mutation CustomerRecover($email: String!) {
customerRecover(email: $email) {
customerUserErrors {
@@ -120,10 +123,10 @@ export const CUSTOMER_RECOVER_MUTATION = `
}
}
}
`;
`);
// Completes the reset using the id + token from the emailed link.
export const CUSTOMER_RESET_MUTATION = `
export const CUSTOMER_RESET_MUTATION = gql(`
mutation CustomerReset($id: ID!, $input: CustomerResetInput!) {
customerReset(id: $id, input: $input) {
customerAccessToken {
@@ -137,10 +140,10 @@ export const CUSTOMER_RESET_MUTATION = `
}
}
}
`;
`);
// Activation link sent to customers created by the merchant.
export const CUSTOMER_ACTIVATE_MUTATION = `
export const CUSTOMER_ACTIVATE_MUTATION = gql(`
mutation CustomerActivate($id: ID!, $input: CustomerActivateInput!) {
customerActivate(id: $id, input: $input) {
customerAccessToken {
@@ -154,10 +157,10 @@ export const CUSTOMER_ACTIVATE_MUTATION = `
}
}
}
`;
`);
export const CUSTOMER_UPDATE_MUTATION = `
${CustomerFragment}
export const CUSTOMER_UPDATE_MUTATION = gql(
`
mutation CustomerUpdate($customerAccessToken: String!, $customer: CustomerUpdateInput!) {
customerUpdate(customerAccessToken: $customerAccessToken, customer: $customer) {
customer {
@@ -170,4 +173,6 @@ export const CUSTOMER_UPDATE_MUTATION = `
}
}
}
`;
`,
[CustomerFragment]
);
+4 -2
View File
@@ -1,6 +1,8 @@
import { gql } from '@shopify/hydrogen';
// Shop policies are exposed on the `shop` object of the Storefront API.
// There is no lookup-by-handle field, so we fetch all of them and match.
export const GET_SHOP_POLICIES_QUERY = `
export const GET_SHOP_POLICIES_QUERY = gql(`
query GetShopPolicies {
shop {
privacyPolicy {
@@ -40,4 +42,4 @@ export const GET_SHOP_POLICIES_QUERY = `
}
}
}
`;
`);
+25 -14
View File
@@ -1,5 +1,7 @@
import { gql } from '@shopify/hydrogen';
// Product Fragment for consistent product data
export const ProductFragment = `
export const ProductFragment = gql(`
fragment ProductFragment on Product {
id
title
@@ -98,12 +100,13 @@ export const ProductFragment = `
}
}
}
`;
`);
// Get multiple products
export const GET_PRODUCTS_QUERY = `
${ProductFragment}
query GetProducts($first: Int!, $after: String, $query: String, $sortKey: ProductSortKeys, $reverse: Boolean) {
export const GET_PRODUCTS_QUERY = gql(
`
query GetProducts($first: Int!, $after: String, $query: String, $sortKey: ProductSortKeys, $reverse: Boolean, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
products(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) {
edges {
node {
@@ -116,24 +119,32 @@ export const GET_PRODUCTS_QUERY = `
}
}
}
`;
`,
[ProductFragment]
);
// Get a single product by handle
export const GET_PRODUCT_QUERY = `
${ProductFragment}
query GetProduct($handle: String!) {
export const GET_PRODUCT_QUERY = gql(
`
query GetProduct($handle: String!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
product(handle: $handle) {
...ProductFragment
}
}
`;
`,
[ProductFragment]
);
// Get product recommendations
export const QUERY_PRODUCT_RECOMMENDATIONS = `
${ProductFragment}
query GetProductRecommendations($productId: ID!) {
export const QUERY_PRODUCT_RECOMMENDATIONS = gql(
`
query GetProductRecommendations($productId: ID!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
productRecommendations(productId: $productId) {
...ProductFragment
}
}
`;
`,
[ProductFragment]
);
+17 -7
View File
@@ -1,10 +1,11 @@
import { gql } from '@shopify/hydrogen';
import { ProductFragment } from '@/graphql/products';
// Storefront search over products. `productFilters` accepts the raw `input`
// values returned in `productFilters[].values[].input`, so facets round-trip
// without the client needing to know each filter's shape.
export const SEARCH_PRODUCTS_QUERY = `
${ProductFragment}
export const SEARCH_PRODUCTS_QUERY = gql(
`
query SearchProducts(
$query: String!
$first: Int!
@@ -12,7 +13,9 @@ export const SEARCH_PRODUCTS_QUERY = `
$sortKey: SearchSortKeys
$reverse: Boolean
$productFilters: [ProductFilter!]
) {
$country: CountryCode
$language: LanguageCode
) @inContext(country: $country, language: $language) {
search(
query: $query
first: $first
@@ -40,6 +43,9 @@ export const SEARCH_PRODUCTS_QUERY = `
}
edges {
node {
# types: PRODUCT already narrows the results, but the schema still
# types nodes as a union __typename lets callers narrow too.
__typename
... on Product {
...ProductFragment
}
@@ -47,16 +53,20 @@ export const SEARCH_PRODUCTS_QUERY = `
}
}
}
`;
`,
[ProductFragment]
);
// Lightweight variant for the autocomplete dropdown — just enough to render a
// row, so the dialog stays responsive while typing.
export const SEARCH_SUGGESTIONS_QUERY = `
query SearchSuggestions($query: String!, $first: Int!) {
export const SEARCH_SUGGESTIONS_QUERY = gql(`
query SearchSuggestions($query: String!, $first: Int!, $country: CountryCode, $language: LanguageCode)
@inContext(country: $country, language: $language) {
search(query: $query, first: $first, types: PRODUCT) {
totalCount
edges {
node {
__typename
... on Product {
id
title
@@ -76,4 +86,4 @@ export const SEARCH_SUGGESTIONS_QUERY = `
}
}
}
`;
`);