GraphQL Storefront API `malformed cursor` error

Raj13
New Member
5 0 0

Hi,

Can anyone help me figure out why this query throws a `malformed cursor` error when using the `cursor` it just returned?

query getCartProducts($cartId: ID!, $cursor: String) {
  node(id: $cartId) {
    ... on Checkout {
      lineItems(first: 2, after: $cursor) {
        edges {
          cursor
        }
      }
    }
  }
}
{
  "data": {
    "node": null
  },
  "errors": [
    {
      "message": "Malformed cursor",
      "locations": [
        {
          "line": 4,
          "column": 7
        }
      ],
      "path": [
        "node",
        "lineItems"
      ]
    }
  ]
}

Steps to recreate: 

  1. Create a checkout and get the checkout `id`
  2. Use the checkout `id` as the `cartId` variable and run the query
  3. Get the first `cursor` and run the query again with the `cursor` as the `cursor` variable

Storefront Access Token: 

9c83f3512c81adaa9f132e5a2b195545

 

Thanks

Replies 12 (12)

KarlOffenberger
Shopify Partner
1873 184 900

Hi Raj

Looks like a bug on that particular object because testing on others, either no cursor is returned, or even if it is, the query doesn't error out but simply returns an empty edges array.

Thus I wouldn't rely on the cursor's presence in the response for paging, but use pageInfo.hasNextPage instead.

Hope that helps and good catch - might want to report as a possible bug to @Shopify?

Raj13
New Member
5 0 0

Thanks for taking a look Karl, I'm not going crazy then!

I can use `hasNextPage` to see whether there is a next page, but I can't actually get the next page, because that requires a `cursor`.

I've tried contacting the support team but they told me to make a post here. Do you know of any way to get in touch with the dev team?

Thanks

KarlOffenberger
Shopify Partner
1873 184 900

What I meant is that you get a cursor as long as there is a next page. When hasNextPage: false ignore the cursor and making the next call otherwise you end up with the error you're seeing.

Regarding Shopify devs, I suppose they do lurk around the hallows of forums but am not aware of ways to directly contact them - as a dev I'd be scared of users contacting me... the horror the horror 😄

Raj13
New Member
5 0 0

Ah right, so you aren't getting that error until using the `cursor` when there are no more pages left? I'm seeing the error even if there are more pages, I haven't been able to use the `cursor` variable at all

Haha that's true!

KarlOffenberger
Shopify Partner
1873 184 900

Nope, not getting it. That is, I only ever get that error on your specific query on Checkout object. Other objects seem to work fine for the few I tried. And I only get it when using the cursor on after where hasNextPage: false. Have you checked that your checkout has more than 2 items? 

Raj13
New Member
5 0 0

I'm only getting the error on `checkout` too, I've got a similar query running fine on `shop`. Strange, yeah there's definitely more than 2 items. Have you got a checkout `id` I could try?

KarlOffenberger
Shopify Partner
1873 184 900

Hey Raj

Okay this is embarassing, you were right all along. Checkout lineItem paging is broken no matter if you have hasNextPage: true or not. No idea what I did earlier this morning - going to blame myself answering questions before having coffee.

Coffee is always a good idea!

While this is definitely a bug IMO, being on the Checkout object I don't think we'd run in to this issue in practice - just set first:250 lineItems and hope billionaires don't fill up your cart with more until @Shopify fix it 😛

Sorry if I wasted your time!

Raj13
New Member
5 0 0

Haha don't worry about it, I appreciate your help either way! I'm just glad that someone else has confirmed the bug, because I was convinced it was me doing something silly!

That's what I've done in the past, but I thought since I'm being all fancy and using GraphQL instead of JSON, I'll load data when I need to rather than all at once. Plus I can sleep easy knowing that even though its theoretically impossible, those billionaires can't break the site!

John_K1
Shopify Staff
4 0 3

Hi all! 

One of our team members is going to take a deeper look into this, but for now, I can pass along that the query seems to work if you pass the cartId and not the cursor value.

 

To learn more visit the Shopify Help Center or the Community Blog.

KarlOffenberger
Shopify Partner
1873 184 900

Hi John K

Thanks for looking in to this.

Querying with cartId works just fine. It's when the cart contains more line items than we fetch, we cannot page (i.e. after: $cursorFromPreviousResponse).

Steps to reproduce:

  1. Run this query on a cart that has more than 1 line item
    query getCartProducts($cartId: ID!, $cursor: String) {
      node(id: $cartId) {
        ... on Checkout {
          lineItems(first: 1, after: $cursor) {
            pageInfo {
              hasNextPage
              hasPreviousPage
            }
            edges {
              node {
                title
              }
              cursor
            }
          }
        }
      }
    }
    
    // query variables
    {
      "cartId": "xxxxxxxxxxx-your-cart-id"
    }
    
    // response
    {
      "data": {
        "node": {
          "lineItems": {
            "pageInfo": {
              "hasNextPage": true,
              "hasPreviousPage": false
            },
            "edges": [
              {
                "node": {
                  "title": "Pear"
                },
                "cursor": "eyJsYXN0X2lkIjoiMTcwYTBkZGM2Njg2N2VlN2ViODA3MzE1ZjIwYTQ1YjEiLCJsYXN0X3ZhbHVlIjoiMTcwYTBkZGM2Njg2N2VlN2ViODA3MzE1ZjIwYTQ1YjEifQ=="
              }
            ]
          }
        }
      }
    }


     

  2. Run the query again with above cursor from response as input to $cursor
     

    // query variables
    {
    	"cartId": "xxxxxxxxxxx-your-cart-id",
      "cursor": "eyJsYXN0X2lkIjoiMTcwYTBkZGM2Njg2N2VlN2ViODA3MzE1ZjIwYTQ1YjEiLCJsYXN0X3ZhbHVlIjoiMTcwYTBkZGM2Njg2N2VlN2ViODA3MzE1ZjIwYTQ1YjEifQ=="
    }
    
    // response
    {
      "data": {
        "node": null
      },
      "errors": [
        {
          "message": "Malformed cursor",
          "locations": [
            {
              "line": 4,
              "column": 7
            }
          ],
          "path": [
            "node",
            "lineItems"
          ]
        }
      ]
    }

     

Actual Result:

{
  "data": {
    "node": null
  },
  "errors": [
    {
      "message": "Malformed cursor",
      "locations": [
        {
          "line": 4,
          "column": 7
        }
      ],
      "path": [
        "node",
        "lineItems"
      ]
    }
  ]
}

Expected result

{
  "data": {
    "node": {
      "lineItems": {
        "pageInfo": {
          "hasNextPage": true,
          "hasPreviousPage": true
        },
        "edges": [
          {
            "node": {
              "title": "Pear",
              "variant": {
                "title": "M \/ Green \/ Regular"
              }
            },
            "cursor": "xxxxxxxxxxx-next-cursor"
          }
        ]
      }
    }
  }
}

 

Hope that helps!

 

p.s. I could send you a detailed list of a few more bugs in the GQL Admin AP... possibly dupes you're aware of, but worth asking if you want.

yunusdev
Visitor
1 0 0

Hi Pls have you found a walkaround to this?

shivam020
Visitor
1 0 0

Hi, were you able to find a solution?