wordpressgraphqlgatsbywp-graphql

Querying wordpress with meta queries from Gatsby


I'm trying to fetch data from my Wordpress backend using a meta query. I'm using this plugin: https://www.wpgraphql.com/extenstion-plugins/wpgraphql-meta-query/

I can run my query in GraphiQL IDE in Wordpress, but not in Gatsbys GraphiQL tool.

I get this error: Unknown argument "where" on field "Query.allWpPage"

Query:

query test {
  allWpPage(
    where: {metaQuery: {
      relation: OR,
      metaArray: [
        {
          key: "some_value",
          value: null,
          compare: EQUAL_TO
        },
        {key: "some_value",
          value: "536",
          compare: EQUAL_TO
        }
      ]
    }}
  ) {
    edges {
      node {
        id
        uri
      }
    }
  }
}

I've tried deleting the cache directory and rebuilding, didn't help.

And just to clarify, I have no problems running other queries and getting ACL-data and what not. The only problem I have (right now) is exposing the where argument to Gatsby.


Solution

  • where filter is restricted in Gatsby. Here you have a detailed list of comparators, but they are:

    On the other hand, there is a list of filters available. In your case, filter is what you are looking for. Your final query should look like:

    query test {
      allWpPage(
       filter : {uri : {ne : "" }}
      ) {
        edges {
          node {
            id
            uri
          }
        }
      }
    }
    

    Of course, adapt the filter to your needs. elemMatch should work for you either.

    You will need to add each condition for each property of the object you're trying to match.


    Why is where restricted?

    Because it belonged to Sift, a library that Gatsby was using to use MongoDB queries, where where is available. Since Gatsby 2.23.0 (June 2020) this library is not being used anymore. More details at History and Sift:

    For a long time Gatsby used the Sift library through which you can use MongoDB queries in JavaScript.

    Unfortunately Sift did not align with how Gatsby used it and so a custom system was written to slowly replace it. This system was called “fast filters” and as of gatsby@2.23.0 (June 2020) the Sift library is no longer used.