CMS Development

RDuthie
Member | Elite Partner
Member | Elite Partner

GraphQL variables not working with arrays

Hi. I'm having an issue when trying to pass an array of slugs into an GraphQL query. For context I've got a array of blog post slugs I want GraphQL to filter by. Array would look something like ['value-1', 'value-2'] etc..

 

I tried everything, but for some reason HUBL doesn't translate the array into the filter.

 

The array is a variable defined in HUBL on top of my .graphql file.

Any help would be appreciated. Thanks

 

Example of code:

 

 

# $post_tag_slug: ['seo-paid-media'] //This array would ideally be split from an string received from query params.
query resourceCenterPosts($post_tag_slug: [String]) {
  BLOG {    
    post_collection(limit: 9, filter: {post_tag_slug__in: $post_tag_slug}) {
      items {
        id
        name
      }
    }
  }
}

 

 

 

0 Upvotes
2 Replies 2
KhushbooRevOps
Member

GraphQL variables not working with arrays

Hi @RDuthie ,

If you're having trouble passing an array of slugs into a GraphQL query in HubL, here's a simple approach.

1. When you define an array of slugs in HubL and pass it to your GraphQL query, it might not be correctly translated into the format GraphQL expects.

2. Before passing the array to GraphQL, convert it into a string that GraphQL can parse. You can use HubL to join the array into a comma-separated string.

{% set slug_list = ['value-1', 'value-2'] %}
{% set slug_string = slug_list|join(',') %}

3. Use the string in your GraphQL query instead of the array.

query GetPosts {
posts(slugs: "{{ slug_string }}") {
title
slug
}
}

4. Make sure to test this setup to confirm that GraphQL correctly interprets the slug list.

 

If you need more help, let's talk!

Khushboo Pokhriyal

Growth & Operations

GroRapid Labs

LinkedIn | 9315044754 | Email | Website

RDuthie
Member | Elite Partner
Member | Elite Partner

GraphQL variables not working with arrays

Thanks for replying.

This doesn't seem to work at all. I think there's an issue with HUBL interpreting arrays as variables in .graphql files.

This of course works perfectly fine if I directly pass an array to the filter, but doesn't work if its passed from a parameter in the graphQL query.

RDuthie_0-1723811644993.png

 

0 Upvotes