Open sjelfull opened 2 years ago
Since the Buy SDK doesn't work with this at all I ended up rewriting all calls to use raw GraphQL queries.
@sjelfull Were you able to use @inContext directives using the same gql library js-buy-sdk uses? graphql-js-client
?
It seems like they support directives but not on the root query object.....
I ended up using apollographql/graphql-tag/loader
(included in Gatsby) and graphql-request
- think the latter was 1/3 the size of graphql-js-client
according to Bundlephobia.
I copied all the .graphql files, added the @inContext
directive, and used this code to change the country code depending on the language:
function replaceContextCountry(query) {
query.definitions[0].directives[0].arguments[0].value.value = regionIsoCode
return query
}
i modified the structure of the buy sdk a bit and managed to make directives work. I will paste here some screenshots of what i did: modified the parse args function, the operation object, the send method, the checkout/product resource methods and the queries by adding that internationalization parameter. Then when you call buy sdk from react you call it: client.checkout.create(input, @inContext(country: 'US')
and everything should work.
@Adu88 Thank you! I've actually been working on modifying graphql-js-client
to try to support this correctly - if we're writing our own queries anyways, you really don't need js-buy-sdk, just the graphql-js-client-compiler
and the schema.json file.
@Adu88 Any chance you'd be open to sharing your changes with the community via pull request? There are a number of us who just got bit by presentmentPrices being discontinued this week in #959 and #853
@JeffJassky Here you go i created a repo with the file i use for the buy-sdk client when i make queries to shopify. https://github.com/Adu88/buy-sdk. Feel free to clone it and see the updates/differences i wrote above.
Also from above it's missing this:
and of course when you call that method you've modified from the client you need to call it with your internationalDirective like this :
client.product.fetchMultiple(product_ids, internationalizationDirective),
with the internationalizationDirective value should look like this: '@inContext(country:${selectedCountry.isoCode})' ex: '@inContext(country: RO)'
@Adu88 Based on your code, I fixed some issues that occourred when the internationalizationDirective was not provided. https://github.com/joshuajylin/buy-sdk
Feature details
Is your feature request related to a problem? Please describe. Since the removal of presentment prices and adding the
@inContext
directive, it seems like multi-currency behaviour is not consistent or helpful in the Buy SDK, meaning you have to run custom queries somehow.My current problem that I'm trying to solve: I want to add multi-currency support, and I want to see the cart and line items in the currently selected currency.
Step 1 was figuring out how to set the currency on the cart. After a few rounds and touching the deprecated
presentmentCurrencyCode
, I foundbuyerIdentity.countryCode
which gives me the correct currency on the cart, meaning the total will show in the associated currency.However, the line item totals/prices is NOT returned in the correct currency - they use the default currency.
It was kind of hard to find the place that explains why this is - why would the cart show in two different currencies? My current understanding is that I need to use the new
@inContext
directive to ALSO get the line items in the correct currency.Describe the solution you'd like Do you plan to add support for the
@inContext
directive in the short term, or could you help with a example that shows how to get the cart and all prices/totals in the same currency?Describe alternatives you've considered I have considered trying to do the queries manually, but it requires a bit of a lift to do this since we use the JS SDK everywhere.
Additional context
Sample response: