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

query with filter generates wrong query string #1272

Closed
FloWi opened this issue Jan 25, 2022 · 4 comments
Closed

query with filter generates wrong query string #1272

FloWi opened this issue Jan 25, 2022 · 4 comments

Comments

@FloWi
Copy link

FloWi commented Jan 25, 2022

Hi!

We wanted to create a query where we filter a table by one column.
This is our scala code and the generated query is below.

  val productDetailPageByPublicId: String => SelectionBuilder[RootQuery, Option[ProductDetailPage]] = { publicId =>
    query_root
      .products(
        limit = Some(1),
        where = Some(products_bool_exp(public_id = Some(String_comparison_exp(_eq = Some(publicId))))),
      )(
        (
          products.id
            ~ products.name
            ~ products.image(imageDetails)
            ~ products.slug
            ~ products.public_id
        ).mapN(ProductDetailPage),
      )
      .map(_.headOption)
  }
query Broken2 {
  products(where: {_and: null, _not: null, _or: null, active: null, adjustable: null, public_id: {_eq: "8001", _gt: null, _gte: null, _ilike: null, _in: null, _is_null: null, _like: null, _lt: null, _lte: null, _neq: null, _nilike: null, _nin: null, _nlike: null, _nsimilar: null, _similar: null}}) {
    id
    name
    image {
      url
      height
      width
      description
    }
    slug
    public_id
  }
}

The query doesn't return any results, even though it should. We found out that we can use the property dropNullInputValues on the toRequest function of the SelectionBuilder. If we set this property to true, we get a valid query and also the expected result.

We had two issues actually - for one it isn't well documented (and we didn't find any tests/examples) and on the other hand leaving it to false (the default) the generated query is wrong.

Would you mind showing us how to use the filter properly?

@ghostdogpr
Copy link
Owner

Why is the generated query invalid? Without knowing the schema, I don't see what's wrong with it.

Noted on the documentation of dropNullInputValues.

@FloWi
Copy link
Author

FloWi commented Jan 26, 2022

Hi @ghostdogpr!
Thanks for your quick response - I highly appreciate that!

The generated query is syntactically correct, but we just get an empty result.
The "correct" query would be this.

query NotBrokenAnymore {
  products(where: {public_id: {_eq: "8001"}}) {
    id
    name
    image {
      url
      height
      width
      description
    }
    slug
    public_id
  }
}

I enabled query logging on hasura and executed both queries. See this gist with the hasura log (it's in json format) and the extracted and formatted sql queries.
The issue seems to be with this statement which always evaluates to false.

((NOT ('true')) AND (('false') AND (("public"."products"."public_id") = ((8001)::text))))

Do you have an idea, what might have caused that NOT('true') AND 'false' part of the statement?

@ghostdogpr
Copy link
Owner

I think this is related to how Hasura interpret the null parameters, it must have a different meaning from the absence of parameters. So what you observed is expected, what you probably want is just to get rid of the null parameters.

There are 2 ways for that: one way is passing dropNullInputValues = true and it will drop all of them, but if you want a fine-grain control and only drop it on some types, you can use custom ArgEncoder. There are some examples in the tests, you can check this PR: #923

implicit val encoderWithoutNull: ArgEncoder[CharacterInput] = CharacterInput.encoder.dropNullValues

val query = Query.addCharacter(CharacterInput("name", None, Nil))(Character.name)

@FloWi
Copy link
Author

FloWi commented Jan 26, 2022

Ah, I see. Thanks for the clarification and the example!

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

No branches or pull requests

2 participants