0

I am new to basically everything; js, graphql etc. But I am using WP GraphQL to get content out of my Wordpress installation to populate a app I made in NextJS. I've managed to get WP GraphQL to do what I need it to do, but now I need to figure out how to get javascript, nextjs and apolloClient to do waht I need it to do!

Basically I have a WPGraphQL query that looks like this when I want to get all campgrounds that have the features "fishing" and "laundry":

query{
 campgrounds(
    where: {
      taxQuery: {
        relation: OR,
        taxArray: [
          {
            terms: ["Fishing"],
            taxonomy: FEATURE,
            operator: IN,
            field: SLUG
          },
          {
            terms: ["Laundry"],
            taxonomy: FEATURE,
            operator: IN,
            field: SLUG
          }
        ]
      }
      }
  ){
    edges{
      cursor
      node{
        id
        title
      }
    }
  }
}

The thing is though, I want to be able to dynamically change that taxArray with a selected list of features. I'm guessing the way to do it is to do some kind of array map over the selected objects (which come from a query param such as localhost:3000/http://localhost:3000/camps?rcampfeatures=Cable%2CCampground+Store) and add those to a object of...objects? that consist of something like - pseudo code here - :

let featurearray = {};
campfeatures.map(m => 
featurearray.push(

{
terms: ["${m}"],
taxonomy: FEATURE,
operator: IN,
field: SLUG
}
) 
)

But I guess my issue is....how do I tell GraphQL to insert those objects? The function I tried to build looks like this:

export async function getCampgroundsByFeature(query) {
  const data = await fetchAPI(
    `
    query($string: String){
      campgrounds(
         where: {
           taxQuery: {
             relation: OR,
             taxArray: [
              $string
             ]
           }
           }
       ){
         edges{
           cursor
           node{
             id
             title
           }
         }
       }
     }
    `,
    {
      variables: {
        string: query,
      },
    }
  );
}

but obviously that's not correct.

Thank you for any help you can grant!!! :)

sylargaf
  • 346
  • 1
  • 6
  • 19
  • 1
    perpare and pass entire `where` object as variable – xadm Mar 29 '21 at 21:45
  • 1
    I wrote a plug-in that enhances taxQuery and also adds input fields for custom taxonomy. That might simplify your query. Have a look at it here: https://github.com/kdmurthy/wp-graphql-taxonomies – Dakshinamurthy Karra Apr 29 '21 at 05:41

0 Answers0