Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with orderBy and cursor pagination #12

Open
Emiliano-Bucci opened this issue Nov 22, 2019 · 4 comments
Open

Issue with orderBy and cursor pagination #12

Emiliano-Bucci opened this issue Nov 22, 2019 · 4 comments

Comments

@Emiliano-Bucci
Copy link

Emiliano-Bucci commented Nov 22, 2019

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.

@ivanjeremic
Copy link

Same issue here, did you find any solution?

@esamattis
Copy link

This is probably an upstream issue with wp-graphql. I don't have the time to look into this currently but if anybody would like to help the first thing would be to extract the args wp-graphql-meta-query generates for WP_Query so we could write a test case for it.

There are already few numeric ordering tests by meta value

https://github.com/wp-graphql/wp-graphql/blob/e3fa87647f783fc065431d95af23fa986b434d76/tests/wpunit/PostObjectCursorTest.php#L301-L358

But maybe we're missing something 🤔

@ivanjeremic
Copy link

This is probably an upstream issue with wp-graphql. I don't have the time to look into this currently but if anybody would like to help the first thing would be to extract the args wp-graphql-meta-query generates for WP_Query so we could write a test case for it.

There are already few numeric ordering tests by meta value

https://github.com/wp-graphql/wp-graphql/blob/e3fa87647f783fc065431d95af23fa986b434d76/tests/wpunit/PostObjectCursorTest.php#L301-L358

But maybe we're missing something 🤔

I get this error when I try to order on a CPT using TITLE or any other sort field. I will set up a CodeSanbox to show the issue.

@kalinon
Copy link

kalinon commented Aug 4, 2023

This happened to me when i was sorting on a "non indexed" column

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants