docs: graphql query with optional `where` can return incorrect results
Bug summary
If a graphql query includes an optional where clause which filters on a relation (e.g. Form(where: { opportunity: { id: { _eq: $opportunityId} } })) and the variable associated with the where clause is undefined (i.e. the where clause should be ignored) that query will always return an empty result set if the relation (in this case Form.opportunity) has no records.
The example schema
- In the following schema, both
ContactListandOpportunityhave one associatedFormobject.
CREATE TABLE "Form" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text
);
CREATE TABLE "Opportunity" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
"formId" uuid NOT NULL REFERENCES "Form" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
);
CREATE TABLE "ContactList" (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
"formId" uuid NOT NULL REFERENCES "Form" ("id") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
);
The example query
Assume the database contains one ContactList record with an associated Form record, but does not contain any Opportunity records.
# Important! the `opportunityId` variable is undefined (i.e. should be ignored)
query FormQuery($opportunityId: uuid){
Form(where: {
opportunity: { id: { _eq: $opportunityId} }
}) {
id
name
}
}
Expected result
- A single
Formobject should be returned (the one associated with theContactList)
Actual result
- An empty array is returned.
Reproduction
A reproduction can be seen here: https://hasura-issue-4214.herokuapp.com/console/api-explorer.
Hi @thefliik, The query
query FormQuery($opportunityId: uuid){
Form(where: {
opportunity: { id: { _eq: $opportunityId} }
}) {
id
name
}
}
translates to fetch Forms whose opportunity's id is a $opportunity. If there are no rows in the Opportunity table, this will give you an empty response since there is no Opportunity for each and every Form.
To fetch Forms whose Opportunity is not null use
where: { opportunity: { id: { _eq: $opportunityId} } }
To fetch Forms whose Opportunity is null use
where: { _not: {opportunity : { id: { _eq: $opportunityId} }} }
See https://github.com/hasura/graphql-engine/issues/476
Hi @rakeshkky, when the variable $opportunityId is undefined, I expected that the query
query FormQuery($opportunityId: uuid){
Form(where: {
opportunity: { id: { _eq: $opportunityId} }
}) {
id
name
}
}
would translate to fetch all forms ? (i.e. opportunity: { id: { _eq: undefined} } is ignored).
This behavior allows/would allow for a query like the following, which can be used to find either a form associated with a ContactList or a form associated with an Opportunity.
query FormQuery($opportunityId: uuid, $contactListId: uuid){
Form(where: {
opportunity: { id: { _eq: $opportunityId} },
contactList: { id: { _eq: $contactListId} },
}) {
id
name
}
}
Currently, this query will always return an empty result set (if there are no opportunities). I would expect that, if a valid id were provided for $contactListId, and $opportunityId was undefined, it would return a single Form.
The situation where the Opportunity table is empty when this query is evaluated seems, admittedly, unlikely to occur outside a development environment. Still, evaluating part of the query's where clause even if it includes an undefined variable seems likely to cause other problems and, to me at least, is unexpected. It also seems like it's providing extra, unnecessary work for the postgres database when graphql-engine asks it to needlessly query on the Opportunity table.
Looking at the graphql spec, it doesn't look like the outcome of this situation is defined. The most related piece I could find is in the Coercing Variable Values section which doesn't actually describe how the server should handle a valid undefined variable.
undefined doesn't exist in GraphQL. Your js client will probably not send the variable to the server when you it is undefined or set it to null (depends on the client).
At the server, if the variable is declared to be nullable (as is the case in your example) the variable having an explicit null or the variable being absent are treated the same, i.e, the variable gets a null value. So your gets translated to this:
query FormQuery {
Form(where: {
opportunity: { id: { _eq: null} }
}) {
id
name
}
}
So the behaviour that you are seeing is because of how we handle null values for _operator fields. When we a see a clause like {_eq: null}, we ignore it. However, in your case, you are doing it inside a relationship. Now your query becomes:
query FormQuery {
Form(where: {
opportunity: {}
}) {
id
name
}
}
which means that you are querying for forms where there exists an opportunity. To get the behaviour that you need you can either construct the where clause dynamically by making it a variable or user the _or and _and operators.
@rikinsk We'll need to document this behaviour.
At the server, if the variable is declared to be nullable (as is the case in your example) the variable having an explicit null or the variable being absent are treated the same, i.e, the variable gets a null value.
Gotcha. This explains what's happening with the query.
To get the behaviour that you need you can either construct the where clause dynamically by making it a variable or user the _or and _and operators.
Makes sense. Thanks so much for the info!
@0x777 Thanks for the detailed explanation. This was very helpful and is very much needed in the documentation. I think the confusion arrises when developers start creating filterable lists. In my case I stated by adding filters via parameters e.g.
query Users($country: String) {
users(where: { country: { _eq: $country }})
}
and these filters worked great for columns on user but not for something like
query Users($country: String, $interests: [String!]) {
users(where: { country: { _eq: $country } user_interests: { topic: { _in: $interests } } })
}
as it ends up filtering out all users that don't have entries in user_interests when $interests is not set (which is not immediately obvious IMO)
As mentioned, using the _and to add dynamic elements to the query proved to be the best solution for me. E.g.
query Users($country: String, $dynamicFilters: [user_bool_exp]) {
users(where: { country: { _eq: $country } _and: dynamicFilters })
}
Hope this simple example proves useful to others in a similar boat.