cartLinesAdd deletes lineItems that were created in cartCreate (bug report, storefront API)

When I create a cart with an initial line item, it works as expected. This successfully creates a cart with the initialized line items.

import { gql } from 'graphql-request';

const CREATE_CART = gql`
  mutation CreateCart($merchandiseId: String!) {
    cartCreate(
      input: {  lines: [{merchandiseId: $merchandiseId] }
    ) {
      cart {
        id
        checkoutUrl
        lines(first: 1) {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }
`;

export default CREATE_CART;

When I subsequently add a line item, it removes the initial line item. The initial line item is not in the mutation response, only the recently added line item.

import { gql } from 'graphql-request';

const ADD_LINE_ITEM = gql`
  mutation AddLineItem($cartId: ID!, $merchandiseId: ID!) {
    cartLinesAdd(cartId: $cartId, lines: [{ merchandiseId: $merchandiseId }]) {
      cart {
        checkoutUrl
        lines(first: 2) {
          edges {
            node {
              id
              attributes {
                key
                value
              }
            }
          }
        }
      }
      userErrors {
        field
        message
      }
    }
  }
`;

export default ADD_LINE_ITEM;

If I remove the initial line item from create cart and make two successive calls to addLineItem, it works as expected.

This appears to be a bug in the API.

I’ll need to dig into this a bit more, but when you create a cart and add a line at the same time, the API might be designed to consider this as the initial state of the cart. Therefore, when you make a subsequent call to add another line item, it might be designed to replace the initial state with the new one, hence why the first item is being removed.

The fact that making two successive calls to addLineItem works as expected reinforces this theory. The first call might be setting the initial state of the cart, and the second call is then adding to this state, rather than replacing it.

Thanks a ton Liam!

If this is the expected behavior, is it described somewhere in the docs for future reference?