简体   繁体   中英

Querying products using endCursor not working

I want to make pagination function for my application, but when i query data using graphql from wordpress the function is not working I have a simple query:

query MyQuery {
  products(first: 2, last: null, after: null, before: null) {
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
   edges{
    node {
      id
      name
    }
    cursor
  }
  }
}

and i get this

{
  "data": {
    "products": {
      "pageInfo": {
        "hasNextPage": true,
        "hasPreviousPage": false,
        "startCursor": "YXJyYXljb25uZWN0aW9uOjY1",
        "endCursor": "YXJyYXljb25uZWN0aW9uOjM2"
      },
      "edges": [
        {
          "node": {
            "id": "cHJvZHVjdDo2NQ==",
            "name": "WordPress Pennant 1"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjY1"
        },
        {
          "node": {
            "id": "cHJvZHVjdDozNg==",
            "name": "WordPress Pennant"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjM2"
        }
      ]
    }
  },
}

But when call again the api to get next page it doesn't retreive anything. i have query

query MyQuery {
  products(first: 2, last: null, after: "YXJyYXljb25uZWN0aW9uOjM2", before: null) {
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
   edges{
    node {
      id
      name
    }
    cursor
  }
  }
}

And results

{
  "data": {
    "products": {
      "pageInfo": {
        "hasNextPage": true,
        "hasPreviousPage": true,
        "startCursor": null,
        "endCursor": null
      },
      "edges": []
    }
  },
}

I have 20 items in my database. I don't know what I'm doing wrong. Thanks for help

In first request you are fetching 10 items, but receive only 2. Your problem lies there, since this query for some reason doesn't find all 20 items.

Request with endCursor looks good according to these docs: https://www.wpgraphql.com/2020/03/26/forward-and-backward-pagination-with-wpgraphql/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM