1

I'm using WPGraphQL to interact with a vanilla wordpress backend. gatsby-source-wordpress is used as a client.

What I would like to achieve with my query:

  • for a list of category ids, give me a list of posts that belong to each category
  • each list of posts should be filtered (posts not having a certain id, etc), and limited to a certain number. The filter/limit params would be the same for each category.

Now when I'm constructing my query, it seems like I can only filter the outer node (category), but not the attached post node:

// syntax from the gatsby-source-wordpress plugin
// https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-wordpress

query MyQuery {
  allWpCategory(filter: {id: {in: ["dGVybTo0Mg=="]}}) {
    nodes {
      posts { // cannot filter here 
        nodes {
          title
          id
        }
      }
    }
  }
}

I tried adding the filter in different locations, without success. Searched for solutions, but didn't find any specific to this GraphQL implementation. Structurally, this looks like what I'm try to achieve, but it's a different platform altogether.

My questions:

  • is it possible to get this working (it seems like a pretty standard requirement to me)
  • if yes, how
  • if no, what's the limiting factor? (GraphQL, WPGraphQL, gatsby-source-wordpress)

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
panepeter
  • 3,224
  • 1
  • 29
  • 41

1 Answers1

0

I don't have your data structure and can't test. Does something like this work?

query MyQuery {
  allWpCategory(
        filter: {
            posts: {
                elemMatch: {
                    id: {
                        ne: "something"
                    }
                }
            }, {
                id: {
                    in: ["dGVybTo0Mg=="]
                }
            }
        })
    {
        nodes {
            posts { // cannot filter here 
                nodes {
                    title
                    id
                }
            }
        }
    }
}
Stefan
  • 576
  • 7
  • 27
  • Unfortunately, this doesn't do what I need. It returns those categories that have associated posts which match the filter. In your example (ignoring the `id` filter): please return all categories that have at least one post with an `id` not equal to `something`. This makes sense, since the `filter` is happening on the `category` level. – panepeter Mar 31 '22 at 19:09
  • I see. Interesting. Sorry, not good enough with GraphQL. But curious about a solution. I remember stumbling over similar needs. The datasets weren't too big. So I ended up using JavaScrips map/reduce to get what I required. – Stefan Mar 31 '22 at 19:34
  • Yes, I'll probably end up doing the same. Feels pretty weird though, since this seems like a common enough requirement. Thanks a lot anyway for taking the time. Maybe there'll be a working answer eventually. – panepeter Mar 31 '22 at 20:16