bold-commerce / go-shopify

Go client for the Shopify API
MIT License
326 stars 255 forks source link

Decoding fulfillmentOrders response not working (no errors) #302

Closed krispetkov closed 3 months ago

krispetkov commented 3 months ago

When trying to execute the following query:

query ($id: ID!) {
    order(id: $id) {
        fulfillmentOrders(first: 1, reverse: true) {
            nodes {
                id
            }
        }
    }
}

The returned response is not being decoded correctly to the struct correctly.

Response:

{
    "data": {
        "order": {
            "fulfillmentOrders": {
                "nodes": [
                    {
                        "id": "gid://shopify/FulfillmentOrder/1234567890123"
                    }
                ]
            }
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 4,
            "actualQueryCost": 4,
            "throttleStatus": {
                "maximumAvailable": 2000,
                "currentlyAvailable": 1996,
                "restoreRate": 100
            }
        }
    }
}

Struct:

type getOrderFulfilmentIDResp struct {
        Data struct {
            Order struct {
                FulfillmentOrders struct {
                    Nodes []struct {
                        ID string `json:"id"`
                    } `json:"nodes"`
                } `json:"fulfillmentOrders"`
            } `json:"order"`
        } `json:"data"`
    }

The Nodes array is always empty and no errors are available. I checked that the returned response/json has values, seems that decoding is not working for some reason...

err := sc.client.GraphQL.Query(ctx, getOrderFulfilmentIdQuery, &variables1, &resp1)
    if err != nil {
        sc.logger.Error(err, "getting fulfilment order id for a shopify order")
        return err
    }
image

Tried it with v3 and v4 and getting the same result.

krispetkov commented 3 months ago

Ok, I think I found the reason. It seems you don't have to have the entire response as a structure. It seems some kind of wrapping happens along the way and the Data and Extensions levels are obsolete, they are being added by the logic automagically. So to make it work instead of this struct:

type getOrderFulfilmentIDResp struct {
        Data struct {
            Order struct {
                FulfillmentOrders struct {
                    Nodes []struct {
                        ID string `json:"id"`
                    } `json:"nodes"`
                } `json:"fulfillmentOrders"`
            } `json:"order"`
        } `json:"data"`
    }

you need to use this one:

type getOrderFulfilmentIDResp struct {
        Order struct {
            FulfillmentOrders struct {
                Nodes []struct {
                    ID string `json:"id"`
                } `json:"nodes"`
            } `json:"fulfillmentOrders"`
        } `json:"order"`
    }