Ecommerce Shopify WordPress Discussion

Passing nil for an optional parameter throws an error in Shopify SDK

This question is related to Shopify's Buy SDK for iOS. I'm creating a checkout using the checkoutCreate mutation. According to the docs, the email parameter is optional. But if I pass nil to it, the API call fails. let checkoutInput = Storefront.CheckoutCreateInput.create( email: .value(email), // `email` is an optional string and I'm passing `nil` for that parameter lineItems: .value(lineItemInput), note: .value(note), allowPartialAddresses: .value(true) ) The .value(:) case itself accepts an optional string value according to the docs. image from SDK docs I can check for nullability like this and exclude the email field altogether like this. var checkoutInput: Storefront.CheckoutCreateInput! if let email { checkoutInput = Storefront.CheckoutCreateInput.create( email: .value(email), lineItems: .value(lineItemInput), note: .value(note), allowPartialAddresses: .value(true) ) } else { checkoutInput = Storefront.CheckoutCreateInput.create( lineItems: .value(lineItemInput), note: .value(note), allowPartialAddresses: .value(true) ) } But that's not really a scalable solution. This problem exist in other inputs in the SDK as well. For example, in structures like MailingAddressInput, almost all fields are optional. So checking for nil and omitting the field like above would be impossible to do. Anyone who has experience in working with Shopify SDK, would like to know how you handled this in a clean manner. Appreciate any help. Thanks!
I've looked at the API and while it does not state there that the email field is required, I saw this issue open. Which is exactly the scenario you are dealing with. It has been opened in the last month, so this might be a new issue with the API. I don't know if this is you, but if it isn't, it might be worthwhile to monitor it to see what Shopify devs respond. As for your specific problem, I would recommend checking if the email field has value or not and if it doesn't, don't pass it in. Something along the lines of: guard let userEmail = email, !userEmail.isEmpty else { let checkoutInput = Storefront.CheckoutCreateInput.create( lineItems: .value(lineItemInput), note: .value(note), allowPartialAddresses: .value(true) ) } let checkoutInput = Storefront.CheckoutCreateInput.create( email: .value(email), lineItems: .value(lineItemInput), note: .value(note), allowPartialAddresses: .value(true) ) } I'm assuming you can combine the logic here with whatever you have at the moment.

December 28, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.