Skip to content

fix: make visitorConsent conditional in cart operations#3720

Merged
itsjustriley merged 4 commits intomainfrom
fix/visitor-consent-conditional
Apr 15, 2026
Merged

fix: make visitorConsent conditional in cart operations#3720
itsjustriley merged 4 commits intomainfrom
fix/visitor-consent-conditional

Conversation

@itsjustriley
Copy link
Copy Markdown
Contributor

Summary

Fixes #3708

  • Cart operations no longer unconditionally include $visitorConsent: VisitorConsent in GraphQL operations
  • The parameter is now only included when explicitly provided
  • Fixes compatibility with stores whose Storefront API schema doesn't include the VisitorConsent type

Changes

@shopify/hydrogen (commit 1):

  • Add cart-query-helpers.ts with getInContextVariables() and getInContextDirective() helpers
  • Update 16 cart mutation files to use conditional inclusion
  • Add tests verifying conditional behavior

@shopify/hydrogen-react (commit 2):

  • Update 9 cart query functions to accept optional { includeVisitorConsent?: boolean } parameter
  • Add comprehensive tests for all query functions

Test plan

  • All 510 hydrogen cart tests pass
  • All 27 new hydrogen-react cart-queries tests pass
  • Verified mutations exclude visitorConsent by default
  • Verified mutations include visitorConsent when explicitly enabled

🤖 Generated with Claude Code

@shopify
Copy link
Copy Markdown
Contributor

shopify bot commented Apr 14, 2026

Oxygen deployed a preview of your fix/visitor-consent-conditional branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment April 15, 2026 1:55 PM

Learn more about Hydrogen's GitHub integration.

@@ -1,17 +1,41 @@
export const CartLineAdd = (cartFragment: string): string => /* GraphQL */ `
type CartQueryOptions = {
includeVisitorConsent?: boolean;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what was the previous behaviour before this param existed? we should default to whatever was before, right?

is there ever a world where we would want people not to include visitor consent? and should we even allow that?

these are genuine questions, im wondering if we could just hardcode it as true and include it every time

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The default behaviour was that it wasn't included. We added the feature to include it unconditionally based on an API change (#3408). That PR states: "Most Hydrogen storefronts do NOT need this feature. If you're using Hydrogen's analytics provider, or Shopify's Customer Privacy API, then consent is already handled automatically." So we're defaulting to the original behaviour in this PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

excellent! thanks for the comprehensive reply

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thank you for prompting me to double check!

@fredericoo
Copy link
Copy Markdown
Contributor

is it worth adding an e2e test for metafields? this could have been caught by it

@itsjustriley
Copy link
Copy Markdown
Contributor Author

itsjustriley commented Apr 14, 2026

is it worth adding an e2e test for metafields? this could have been caught by it

definitely! https://github.com/Shopify/developer-tools-team/issues/1199

@itsjustriley itsjustriley marked this pull request as ready for review April 15, 2026 11:44
@itsjustriley itsjustriley requested a review from a team as a code owner April 15, 2026 11:44
Copy link
Copy Markdown
Contributor

@fredericoo fredericoo left a comment

Choose a reason for hiding this comment

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

just some small comments that are not necessarily needed to be addressed

also if you rebase or merge main in all the tests should pass 🥳

Comment thread packages/hydrogen/src/cart/queries/cartAttributesUpdateDefault.ts Outdated
Comment thread packages/hydrogen/src/cart/queries/cart-query-helpers.ts Outdated
itsjustriley and others added 4 commits April 15, 2026 10:44
Fixes #3708

Cart operations were unconditionally declaring `$visitorConsent: VisitorConsent`
in GraphQL mutations, causing schema validation failures on stores without the
VisitorConsent type (older API versions or certain store configurations).

The visitorConsent parameter is now only included when explicitly provided via
`optionalParams.visitorConsent`. This restores compatibility while preserving
the feature for users who need it.

Changes:
- Add cart-query-helpers.ts with getInContextVariables/getInContextDirective
- Update 16 cart mutation files to use conditional inclusion
- Add tests verifying conditional behavior

Co-Authored-By: Claude Opus 4.5 (1M context) <noreply@anthropic.com>
Same root cause as the hydrogen fix - cart queries were unconditionally
declaring `$visitorConsent: VisitorConsent`, causing schema validation
failures on stores without that type.

Cart query functions now accept an optional second parameter:
`{ includeVisitorConsent?: boolean }`. The visitorConsent variable and
directive argument are only included when explicitly enabled.

Co-Authored-By: Claude Opus 4.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 (1M context) <noreply@anthropic.com>
… into helpers

Address review feedback on PR #3720. The visitorConsent helpers and type
were duplicated across 17 mutation/query files with 3 inconsistent names
(CartMutationBuilderOptions, CartMutationOptions, CartQueryBuilderOptions).
Callers also repeated the derivation logic and forced ?? false at every call site.

- Export CartBuilderOptions from cart-query-helpers.ts as the single source of truth
- Add shouldIncludeVisitorConsent() to centralize the visitorConsent detection logic
- Make getInContextVariables/getInContextDirective params optional (default false)
  so callers no longer write options.includeVisitorConsent ?? false at 34 call sites

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@itsjustriley itsjustriley force-pushed the fix/visitor-consent-conditional branch from aeac91d to 45c3550 Compare April 15, 2026 13:53
@itsjustriley itsjustriley requested a review from fredericoo April 15, 2026 14:06
@itsjustriley itsjustriley merged commit f84ab40 into main Apr 15, 2026
21 checks passed
@itsjustriley itsjustriley deleted the fix/visitor-consent-conditional branch April 15, 2026 14:16
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.

VisitorConsent isn't a defined input type (on $visitorConsent) while calling cart.setMetafields

2 participants