wp-graphql / wp-graphql-meta-query

WPGraphQL Extension: Adds "meta_query" support to postObject connection queries using WP_Query
https://wpgraphql.com
GNU General Public License v3.0
51 stars 18 forks source link

Issue with orderBy and cursor pagination #12

Open Emiliano-Bucci opened 4 years ago

Emiliano-Bucci commented 4 years ago

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 commented 4 years ago

Same issue here, did you find any solution?

esamattis commented 4 years ago

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 commented 4 years ago

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 commented 1 year ago

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