问题
I'm using the shopify_api
gem.
The API doc shows that you can skinny down a ShopifyAPI::Order
response by telling it which fields to return. For example, the following will return only the id
attribute, and the shipping_address
attribute(s)
ShopifyAPI::Order.find(:all, params: {fields: "id,shipping_address"})
shipping_address
happens to be a hash with multiple fields inside it. Is it possible to specify which fields nested within shipping_address
to return? e.g.
ShopifyAPI::Order.find(:all, params: {fields: "id,shipping_address['country_code']"})
This might return something like
#<ShopifyAPI::Order:0x0000010558b348
@attributes={
"id"=>12345678,
"shipping_address"=>#<ShopifyAPI::ShippingAddress:0x0000010558aa88
@attributes={"country_code"=>"US"},
@prefix_options={},
@persisted=false>
},
@prefix_options={},
@persisted=true>
I know I can pick that attributes out myself (order.shipping_address.country_code
), but this is a more in the interests of 'skinnying down' the response.
Bonus Question 1: What are the perceivable advantages of using the fields
parameter?
Bonus Question 2: When (if ever) might Shopify return a nil shipping address?
回答1:
It's usually better to post seperatly for each question, but here's the answers.
- No.
- If you limit the data being returned it will take less time to generate the response, less data to transfer across the wire and less data for you to parse. This means that it should be faster for you to do an operation. This depends on so many different factors that it's hard to say how much of a performance increase you will reciece. My feeling it is minimal unless you are doing a very large number of requests.
- Shipping address may be blank if the order (ie all line items) doesn't require shipping.
来源:https://stackoverflow.com/questions/11382519/does-shopify-order-api-allows-filtering-by-nested-fields