cms icon indicating copy to clipboard operation
cms copied to clipboard

GraphQL filter on terms returns all items

Open SteJW opened this issue 3 years ago • 0 comments

Bug description

I'm having a problem when I search for multiple terms, connected to multiple entries. When I search for "products" that are connected to the slug of multiple terms, the graphQL returns all items because the QueryBuilder doesn't understand the multi array.

How to reproduce

I tried this GraphQL query

{
  entries(collection: "products", filter: {product_type: {in: {slug: ["term1","term2"]}}, locale: {is: "default"}}) {
    data {
      ... on Entry_Products_Products {
        id
        title
        slug
      }
    }
  }
}

I did a quick fix to the whereIn function to solve this problem. It checks if $values is an array and if so, it creates multiple wheres. If it's not an array, it will continue with the old way.

src/Query/Builder.php

public function whereIn($column, $values, $boolean = 'and')
    {
        $values_set = false;
        if(is_array($values)) {
            foreach($values as $field => $field_values) {
                if(is_array($field_values)) {
                    foreach($field_values as $_field_value) {
                        $this->wheres[] = [
                            'type' => 'In',
                            'column' => $column,
                            'values' => [$field => [$_field_value]],
                            'boolean' => 'or',
                        ];
                    }
                    $values_set = true;
                }
            }
        }

        if(!$values_set) {
            $this->wheres[] = [
                'type' => 'In',
                'column' => $column,
                'values' => $values,
                'boolean' => $boolean,
            ];
        }

        return $this;
    }

I don't want to create a pull request for this, because I think the problem begins earlier when preparing the query. Not too familiar with Statamic yet.

Logs

No response

Versions

Statamic 3.2.20 Pro Laravel 8.70.2 PHP 7.4.24

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

SteJW avatar Nov 16 '21 10:11 SteJW