@@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO
2323
2424import { gql , useLazyQuery , useMutation } from '@apollo/client' ;
2525import type { Edges , ShopifyProduct , ShopifyCart } from '../../@types' ;
26+ import { getLocale } from '../utils' ;
2627
2728const moneyFragment = gql `
2829 fragment Price on MoneyV2 {
@@ -66,7 +67,8 @@ const cartCostFragment = gql`
6667` ;
6768
6869const PRODUCTS_QUERY = gql `
69- query FetchProducts {
70+ query FetchProducts($country: CountryCode = CA)
71+ @inContext(country: $country) {
7072 products(first: 10) {
7173 edges {
7274 node {
@@ -77,6 +79,9 @@ const PRODUCTS_QUERY = gql`
7779 edges {
7880 node {
7981 id
82+ unitPrice {
83+ ...Price
84+ }
8085 price {
8186 ...Price
8287 }
@@ -102,7 +107,8 @@ const PRODUCTS_QUERY = gql`
102107` ;
103108
104109const CART_QUERY = gql `
105- query FetchCart($cartId: ID!) {
110+ query FetchCart($cartId: ID!, $country: CountryCode = CA)
111+ @inContext(country: $country) {
106112 cart(id: $cartId) {
107113 id
108114 totalQuantity
@@ -134,7 +140,8 @@ const CART_QUERY = gql`
134140` ;
135141
136142const CREATE_CART_MUTATION = gql `
137- mutation CreateCart($input: CartInput) {
143+ mutation CreateCart($input: CartInput, $country: CountryCode = CA)
144+ @inContext(country: $country) {
138145 cartCreate(input: $input) {
139146 cart {
140147 id
@@ -145,7 +152,11 @@ const CREATE_CART_MUTATION = gql`
145152` ;
146153
147154const ADD_TO_CART_MUTATION = gql `
148- mutation AddToCart($cartId: ID!, $lines: [CartLineInput!]!) {
155+ mutation AddToCart(
156+ $cartId: ID!
157+ $lines: [CartLineInput!]!
158+ $country: CountryCode = CA
159+ ) @inContext(country: $country) {
149160 cartLinesAdd(cartId: $cartId, lines: $lines) {
150161 cart {
151162 id
@@ -157,7 +168,11 @@ const ADD_TO_CART_MUTATION = gql`
157168` ;
158169
159170const REMOVE_FROM_CART_MUTATION = gql `
160- mutation RemoveFromCart($cartId: ID!, $lineIds: [ID!]!) {
171+ mutation RemoveFromCart(
172+ $cartId: ID!
173+ $lineIds: [ID!]!
174+ $country: CountryCode = CA
175+ ) @inContext(country: $country) {
161176 cartLinesRemove(cartId: $cartId, lineIds: $lineIds) {
162177 cart {
163178 id
@@ -169,15 +184,26 @@ const REMOVE_FROM_CART_MUTATION = gql`
169184` ;
170185
171186function useShopify ( ) {
187+ const [ , country ] = getLocale ( ) . split ( '_' ) ;
188+ const includeCountry = {
189+ variables : {
190+ country,
191+ } ,
192+ } ;
172193 const products = useLazyQuery < { products : Edges < ShopifyProduct > } > (
173194 PRODUCTS_QUERY ,
195+ includeCountry ,
174196 ) ;
175197 const cart = useLazyQuery < { cart : ShopifyCart } > ( CART_QUERY , {
176198 fetchPolicy : 'network-only' ,
199+ ...includeCountry ,
177200 } ) ;
178- const cartCreate = useMutation ( CREATE_CART_MUTATION ) ;
179- const cartLinesAdd = useMutation ( ADD_TO_CART_MUTATION ) ;
180- const cartLinesRemove = useMutation ( REMOVE_FROM_CART_MUTATION ) ;
201+ const cartCreate = useMutation ( CREATE_CART_MUTATION , includeCountry ) ;
202+ const cartLinesAdd = useMutation ( ADD_TO_CART_MUTATION , includeCountry ) ;
203+ const cartLinesRemove = useMutation (
204+ REMOVE_FROM_CART_MUTATION ,
205+ includeCountry ,
206+ ) ;
181207
182208 return {
183209 queries : {
0 commit comments