Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL filter on terms returns all items #4739

Open
SteJW opened this issue Nov 16, 2021 · 0 comments
Open

GraphQL filter on terms returns all items #4739

SteJW opened this issue Nov 16, 2021 · 0 comments
Labels

Comments

@SteJW
Copy link
Contributor

SteJW commented Nov 16, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants