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

Bug: Equality checks on optional parameter don't work when it's undefined. Can't check if undefined. #3523

Closed
rccoe opened this issue Dec 12, 2019 · 8 comments
Labels
c/server Related to server k/question

Comments

@rccoe
Copy link

rccoe commented Dec 12, 2019

I'm running this query:

GetUserFromList($user_id: Int) { list( where: {_and: [{ user_id: { _eq: $user_id }}, {user_id:{_neq:null}}]} ) { user_id } }

and getting these results

"data": { "list": [ { "user_id": 14 }, ...

How can I filter out where the user_id = null? I'm running a mutation using this logic and it's updating everything in the table when user_id = null

Thanks!

@rccoe rccoe closed this as completed Dec 12, 2019
@rccoe
Copy link
Author

rccoe commented Dec 12, 2019

Dupe of #704

@paf31
Copy link
Contributor

paf31 commented Dec 12, 2019

I don't think this is a duplicate, since that issue refers to undefined, not null. However, the part of the manual I think you need is here.

@rccoe
Copy link
Author

rccoe commented Dec 12, 2019

That logic can't be used on parameters. If you do

{$user_id:{_neq:null}}
or
{$user_id:{_is_null: false}}

It evaluates to true when $user_id is undefined, which is counterintuitive and dangerous since using this in mutations could be disastrous. Reopening as I think you're right that it's not a duplicate issue.

@rccoe rccoe reopened this Dec 12, 2019
@paf31
Copy link
Contributor

paf31 commented Dec 12, 2019

What about making the type non-nullable? i.e. GetUserFromList($user_id: Int!) { ... }

@rccoe
Copy link
Author

rccoe commented Dec 12, 2019

The use case is an upsert mutation. I have relationships between 3 tables. The situation is I want to upsert users, and if they've changed orgs, transfer their lists to the new org.

Relationships:
org -> list[]
user -> list[]
org -> user[]

mutation UpsertUser($user: user_insert_input!, $org_id: Int, $user_id: Int) {
  insert_auth_user(
    objects: [$user]
    on_conflict: {
      constraint: user_pkey
      update_columns: [first_name, last_name, email, org_id]
    }
  ) {
    affected_rows
    returning {
      id
    }
  }
  update_list(
  where: {_and: [{ user_id: { _eq: $user_id }}, {$user_id:{_neq:null}}]} 
    _set: { org_id: $org_id }
  ) {
    affected_rows
    returning {
      id
    }
  }
}

In the case of the new user, this will update all lists org_id to $org_id.

Due to this bug, doing what I want either requires 2 calls or including all lists in the $user payload with the new org_id.

@rccoe rccoe changed the title Bug: Null query parameter on foreign key returns all objects Bug: Equality checks on optional parameter don't work when it's undefined. Can't check if undefined. Dec 13, 2019
@paf31
Copy link
Contributor

paf31 commented Dec 13, 2019

I think I understand the problem then. Would something like the following work?

query GetUserFromList($user_id: Int, $allow_nulls: Boolean) {
  list(where: {id: {_eq: $user_id, _is_null: $allow_nulls}}) {
    id
  }
}

You could then query with parameters {"user_id": 42} for a specific user, or {"allow_nulls": true} for a new user, instead of passing {"user_id": null}.

The reason {"user_id": null} isn't working is that, as we see if we substitute the inputs into the query definition, it's the same as asking for:

query Inlined {
  list(where: {id: {_eq: null}}) {
    id
  }
}

which is equivalent to no filter at all. @0x777 might have an opinion on whether that's a bug or not?

@marionschleifer
Copy link
Contributor

@rccoe is your question answered? If yes, we can close this issue 🙂

@marionschleifer
Copy link
Contributor

@rccoe I'm closing this issue. Feel free to re-open if you'd like to add anything 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/server Related to server k/question
Projects
None yet
Development

No branches or pull requests

3 participants