Skip to content

Issue with orderBy and cursor pagination #12

@Emiliano-Bucci

Description

@Emiliano-Bucci

Don't know if this is something related to this plugin (i'd rather say that it belongs to wp-graphql) but still, i prefer to write it here; if is the case, i can move the issue.

I'm having issues when i want to sort elements by a certain META field value and using together the pagination cursor. Example:

For this query:

{
  trekking(
    first: 5
    where: {
      metaQuery: {
        metaArray: [
          {
            type: NUMERIC
            key: "order_field"
            value: "1"
            compare: GREATER_THAN_OR_EQUAL_TO
          }
        ]
      }
    }
  ) {
    pageInfo {
      hasNextPage
      endCursor
    }
    edges {
      cursor
      node {
        slug
        attributes {
          orderField
        }
      }
    }
  }
}

I'm getting this result:

{
  "data": {
    "trekking": {
      "pageInfo": {
        "hasNextPage": false,
        "endCursor": "YXJyYXljb25uZWN0aW9uOjMxMA=="
      },
      "edges": [
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjQzNg==",
          "node": {
            "slug": "anello-del-monte-ebro",
            "attributes": {
              "orderField": 1
            }
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjQyNQ==",
          "node": {
            "slug": "luci-del-nord",
            "attributes": {
              "orderField": 4
            }
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjI5Mg==",
          "node": {
            "slug": "anello-della-vanoise",
            "attributes": {
              "orderField": 3
            }
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjMxMA==",
          "node": {
            "slug": "rocca-dolgisio",
            "attributes": {
              "orderField": 2
            }
          }
        }
      ]
    }
  }
}

I want to order them by the orderField, so, for this query:

{
  "data": {
    "trekking": {
      "pageInfo": {
        "hasNextPage": false,
        "endCursor": "YXJyYXljb25uZWN0aW9uOjMxMA=="
      },
      "edges": [
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjQzNg==",
          "node": {
            "slug": "anello-del-monte-ebro",
            "attributes": {
              "orderField": 1
            }
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjQyNQ==",
          "node": {
            "slug": "luci-del-nord",
            "attributes": {
              "orderField": 4
            }
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjI5Mg==",
          "node": {
            "slug": "anello-della-vanoise",
            "attributes": {
              "orderField": 3
            }
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjMxMA==",
          "node": {
            "slug": "rocca-dolgisio",
            "attributes": {
              "orderField": 2
            }
          }
        }
      ]
    }
  }
}

i get this:

{
  "data": {
    "trekking": {
      "pageInfo": {
        "hasNextPage": true,
        "endCursor": "YXJyYXljb25uZWN0aW9uOjMxMA=="
      },
      "edges": [
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjQzNg==",
          "node": {
            "slug": "anello-del-monte-ebro",
            "attributes": {
              "orderField": 1
            }
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjMxMA==",
          "node": {
            "slug": "rocca-dolgisio",
            "attributes": {
              "orderField": 2
            }
          }
        }
      ]
    }
  }
}

which is what i'm expecting. Now, i want to use the cursor pagination, so i made this query:

{
  trekking(first: 2, after: "YXJyYXljb25uZWN0aW9uOjMxMA==", where: {
    orderby: {
      field: META
      order: ASC
    }
    metaQuery: {
      metaArray: [
        {
          type: NUMERIC
          key: "order_field",
          value: "1",
          compare: GREATER_THAN_OR_EQUAL_TO
        }
      ]
    }
  }) {
    pageInfo {
      hasNextPage
      endCursor
    }
    edges {
      cursor
      node {
        slug
        attributes {
          orderField
        }
      }
    }
  }
}

and i would expect to receive the other 2 items, but instead i get this:

{
  "data": {
    "trekking": {
      "pageInfo": {
        "hasNextPage": false,
        "endCursor": null
      },
      "edges": []
    }
  }
}

i guess this is because the default sort field is the DATE, and the element with the orderField: 2 is the last one, so there're no more items after that last one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions