Skip to content

Conversation

@blittle
Copy link
Contributor

@blittle blittle commented Jun 18, 2025

Add PoC support for @defer in GraphQL queries. Hydrogen doesn't use @shopify/graphql-client, because we have a lot of custom caching and request tracking. @defer support was implemented in @shopify/graphql-client, and it's not trivial. This PoC adds support by conditionally calling the @shopify/graphql-client when we detect @defer is used. It's awkward though, because that client doesn't expose the raw Response object, so I have it overriding fetch just to get at the Response object.

Test it out by adding the following route:

import {LoaderFunctionArgs, useLoaderData} from 'react-router';

export async function loader({context}: LoaderFunctionArgs) {
  const data = await context.storefront.query(
    `
  query ProductQuery($handle: String) {
    product(handle: $handle) {
      id
      handle
      ... @defer(label: "deferredFields") {
        title
        description
      }
    }
  }
`,
    {
      variables: {
        handle: 'v2-snowboard',
      },
    },
  );
  return data;
}

export default function Test() {
  const data = useLoaderData<typeof loader>();
  return <div>{data?.product?.title}</div>;
}

EDIT

You can test out the new network discount codes, that require defer, by loading the demo store, adding something to the cart, and trying to apply the discount code: NETWORK_CALL_DISCOUNT. It should take 1-2 seconds, and this discount code won't work on the main branch.

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

@shopify
Copy link
Contributor

shopify bot commented Jun 18, 2025

Oxygen deployed a preview of your bl-defer-support branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2025 5:29 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2025 5:29 PM
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2025 5:29 PM
metaobjects ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2025 5:29 PM

Learn more about Hydrogen's GitHub integration.

Add defer support by just checking if the query includes the `@defer` directive.
If it does, use the shopify graphql client. Using this client is awkward though,
because it doesn't expose the raw Resposne object. Perhaps we could refactor
everything to use it?

Also change the default `updateDiscountCodes` fragment to use the `@defer` fragment
@blittle blittle force-pushed the bl-defer-support branch from fe342d4 to 75a5dd4 Compare June 23, 2025 14:48
@juanpprieto juanpprieto marked this pull request as ready for review July 9, 2025 17:23
@github-actions

This comment has been minimized.

@juanpprieto juanpprieto changed the title Add defer support Add GraphQL @defer directive support Jul 9, 2025
@juanpprieto juanpprieto changed the title Add GraphQL @defer directive support Add GraphQL @defer directive support to storefront client Jul 9, 2025
Copy link
Contributor

@juanpprieto juanpprieto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a good compromise, thanks!

@juanpprieto juanpprieto merged commit e38273f into main Jul 9, 2025
12 checks passed
@juanpprieto juanpprieto deleted the bl-defer-support branch July 9, 2025 17:35
juanpprieto added a commit that referenced this pull request Sep 17, 2025
* Add defer support

Add defer support by just checking if the query includes the `@defer` directive.
If it does, use the shopify graphql client. Using this client is awkward though,
because it doesn't expose the raw Resposne object. Perhaps we could refactor
everything to use it?

Also change the default `updateDiscountCodes` fragment to use the `@defer` fragment

* Delete defer test route

* Add changeset

---------

Co-authored-by: Juan P. Prieto <jp@calltheguys.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants