Aug 15, 202410:18 AM - edited Aug 15, 202410:21 AM
Participant | Elite Partner
GraphQL variables not working with arrays
SOLVE
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
}
}
}
}
It’s pretty strict about how variables are structured, and in some cases, array inputs just don’t behave as expected—especially when passed in as variables rather than inline. One thing that helps is double-checking the expected input type in the schema and making sure the array is exactly in that format, both in shape and in how it’s passed. Sometimes wrapping or unwrapping it slightly can make the difference.
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.