Refunds can appear twice in an order's GQL response

Topic summary

A GraphQL query issue occurs when fetching order refund data, causing duplicate refund entries to appear in the response.\n\nReproduction steps:\n- Create a test order with two SKUs and complete payment\n- Edit the order to set one SKU’s quantity to zero\n- Initial fetch shows one refund entry (SKU-specific) with totalRefundedSet amount of \

Summarized with AI on November 11. AI used: claude-sonnet-4-5-20250929.

Create a test order containing two skus, and pay it.

Now edit the order, and set the quantity of one of the skus to zero.

Fetch the order: it will contain two line items, and a single refund which is specific to that line item. Here’s the refund:

"refunds": [
    {
      "createdAt": "2024-03-27T22:33:46Z",
      "updatedAt": "2024-03-27T22:33:46Z",
      "note": null,
      "refundLineItems": {
        "nodes": [
          {
            "priceSet": {
              "shopMoney": {
                "amount": "28.95",
                "currencyCode": "USD"
              }
            },
            "quantity": 1,
            "subtotalSet": {
              "shopMoney": {
                "amount": "28.95",
                "currencyCode": "USD"
              }
            },
            "totalTaxSet": {
              "shopMoney": {
                "amount": "2.39",
                "currencyCode": "USD"
              }
            },
            "lineItem": {
              "sku": "CSJAVA2-P-00"
            }
          }
        ]
      },
      "totalRefundedSet": {
        "shopMoney": {
          "amount": "0.0",
          "currencyCode": "USD"
        }
      }
    }
  ],

However, no refund has yet been issued.

So, back in the store, issue a refund for that sku.

Fetch the order. Now the transaction amount is correct, but there are two refund items: the original sku-specific one and a second one which is order wide.

"refunds": [
    {
      "createdAt": "2024-03-27T22:33:46Z",
      "updatedAt": "2024-03-27T22:33:46Z",
      "note": null,
      "refundLineItems": {
        "nodes": [
          {
            "priceSet": {
              "shopMoney": {
                "amount": "28.95",
                "currencyCode": "USD"
              }
            },
            "quantity": 1,
            "subtotalSet": {
              "shopMoney": {
                "amount": "28.95",
                "currencyCode": "USD"
              }
            },
            "totalTaxSet": {
              "shopMoney": {
                "amount": "2.39",
                "currencyCode": "USD"
              }
            },
            "lineItem": {
              "sku": "CSJAVA2-P-00"
            }
          }
        ]
      },
      "totalRefundedSet": {
        "shopMoney": {
          "amount": "0.0",
          "currencyCode": "USD"
        }
      }
    },
    {
      "createdAt": "2024-03-27T22:46:05Z",
      "updatedAt": "2024-03-27T22:46:05Z",
      "note": "",
      "refundLineItems": {
        "nodes": [

        ]
      },
      "totalRefundedSet": {
        "shopMoney": {
          "amount": "31.34",
          "currencyCode": "USD"
        }
      }
    }
  ],

My code is trying to work out how much we sold. It the second case, it correctly assigned the refund to the line item. But then it comes across the second refund transaction. This looks like any other order-wide refund, so it applies it too.

How can I work out what’s going on in this situation?

TIA

Dave