There could be a lot of places where the variable could be a nullable string in this case, the frontend client could either send NULL or the string value.
In graphql for example we wanted all the users where the company is equals to the input which is a nullable string.
We would write the query as below
query ($company: String) {
Users(where: {company: {_eq: $company}}) {
Id
}
}
That would create an sql where company = "abc" or where company = Null.
When a Null value is given to postgres with equals, it will ignore the condition and return all the Users.
In order to tackle these conditions, postgres supports is not distinct from for equality check for a variable which could be a string or null.
The query would look like this.
query ($company: String) {
Users(where: {company: {_is_not_distinct_from: $company}}) {
Id
}
}
The other way around is the client checks for null and modified the graphql dynamically. I feel there would a lot of logic which will then be needed on the frontend and implementing a support for the aforementioned comparator would be greatly appreciated.
Hey @harshmaur! I'd love to pick up this issue.
@harshmaur,
In Hasura, where: {} will evaluate to = true. That is why when you pass undefined or null in the variable it will return all records.
There is already an issue about it: https://github.com/hasura/graphql-engine/issues/704
Meanwhile, you can change your query.
query ($company: String, isNull: Boolean) {
Users(where: {_and: [{company: {_eq: $company}}, {company: {_is_null: $isNull}}]) {
Id
}
}
and in the variables you do:
{
variables: {
company: data.company,
isNull: !data.company
}
}
This way, when the company is null/undefined, the generated SQL will be
SELECT id FROM users WHERE company = true and company IS NULL;
It will return only the null values, and when the variable is not null the generated SQL will be
SELECT id FROM users WHERE company = "variable text" and company IS NOT NULL;
Most helpful comment
@harshmaur,
In Hasura, where: {} will evaluate to = true. That is why when you pass undefined or null in the variable it will return all records.
There is already an issue about it: https://github.com/hasura/graphql-engine/issues/704
Meanwhile, you can change your query.
and in the variables you do:
This way, when the company is null/undefined, the generated SQL will be
It will return only the null values, and when the variable is not null the generated SQL will be