wp-graphql / wp-graphql-tax-query

Adds `tax_query` support to postObject connection queries using WP_Query
46 stars 16 forks source link

Exclude taxonomy conditionally? #20

Closed grazianodev closed 4 years ago

grazianodev commented 4 years ago

Hi,

does this plugin offer a way to exclude some parameters to a GraphQL request conditionally? I have tried a few default things like directives and interpolation but I can't find a way to make it work...

My use case is I have a list of posts that users can filter by selecting some options in the frontend; when the filters are applied, the below query fires. What I would like to achieve is that, e.g. if the user does NOT select any genre, I want to NOT define the $genre variable and to NOT include an array for the genre in the taxArray. This is because if $genre is an empty string or even null, Wordpress will still add it to the tax_query and return unintended results.

As an alternative, so far I modify the server response by filtering in the query args and excluding the genre from the tax_query if the relative parameter is an empty string, but I would prefer to alter the request instead.

PS. By the way, I use graphql_post_object_connection_query_args to filter the query args, but I'm working with posts of a custom post type opps. Is there a filter to work with a certain custom post type only, something like graphql_opps_object_connection_query_args?

Thank you for any help and for the whole WPGraphQL project, great work!

query getOpps( 
  $after: String, 
  $first: Int = 8, 
  $byDeadline: String = "",
  $byStatus: String = "",
  $feeTier: String = "",
  $genre: String = "", 
  $prize: String = ""
) {
  opps( 
    after: $after,
    first: $first,
    where: {
      byDeadline: $byDeadline,
      taxQuery: {
        taxArray: [
          {
            terms: [ $byStatus ],
            taxonomy: OPPSCATEGORY,
            operator: IN,
            field: SLUG
          },
          {
            terms: [ $feeTier ],
            taxonomy: OPPSCATEGORY,
            operator: IN,
            field: SLUG
          },
          {
            terms: [ $genre ],
            taxonomy: OPPSCATEGORY,
            operator: IN,
            field: SLUG
          },
          {
            terms: [ $prize ],
            taxonomy: OPPSCATEGORY,
            operator: IN,
            field: SLUG
          }
        ]
      }
    }
  ) {
    pageInfo {
      endCursor
      hasNextPage
    }
    edges {
      node {
        id
        link
        slug
        title
        contentType {
          node {
            name
          }
        }
      }
    }
  }
}
jasonbahl commented 4 years ago

@grazianodev hey! I would suggest moving the variable up a bit, like so:

query getOpps( 
  $after: String, 
  $first: Int = 8, 
  $where: RootQueryToOppsConnectionWhereArgs
) {
  opps( 
    after: $after,
    first: $first,
    where: $where
  ) {
    pageInfo {
      endCursor
      hasNextPage
    }
    edges {
      node {
        id
        link
        slug
        title
        contentType {
          node {
            name
          }
        }
      }
    }
  }
}

Then, your variables will look like:

[
  first: 10, 
  after: null,
  where: null
]

The variables can be created dynamically by your application and passed in.

So, you could pass just the following, for example:

{
      byDeadline: $byDeadline,
      taxQuery: {
        taxArray: [
          {
            terms: [ $byStatus ],
            taxonomy: OPPSCATEGORY,
            operator: IN,
            field: SLUG
          },
        ]
      }
}

This allows you to build the variables in any way you like, as long as it confirms the the shape of the Input.

jasonbahl commented 4 years ago

I'm going to close this, as I believe the question is answered.

grazianodev commented 4 years ago

Yes, this works. Thanks for your help!

koraysels commented 2 years ago

How did you get this to work? for me it does not work..

I have multiple things that i do not understand:

  1. what is RootQueryToOppsConnectionWhereArgs
  2. what type is the variable you pass to the query. is it a string ? of is it an Object.

how does this work? could you @jasonbahl or anybody give a precise example ? I am using apollo.

jasonbahl commented 2 years ago

@koraysels I would recommend using GraphiQL to explore the schema and see what's possible.

what is RootQueryToOppsConnectionWhereArgs

RootQueryToOppsConnectionWhereArgs is the GraphQL Type in the Schema that defines what fields are possible to use as input when querying Opps from the Root of the Graph.

what type is the variable you pass to the query. is it a string ? of is it an Object.

GraphiQL should help here. It depends on the field you're making a variable. You can turn any input field into a variable when you write your queries. Some fields are scalar fields (Integer, String, Boolean, etc), and some are Input Objects, which have nested fields.

could you @jasonbahl or anybody give a precise example ?

I tried to provide an example here:

https://github.com/wp-graphql/wp-graphql-tax-query/issues/20#issuecomment-599582694