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

Optional arguments in hasura not working after migrating to hasura cloud #6660

Closed
lakhansamani opened this issue Mar 15, 2021 · 6 comments
Closed

Comments

@lakhansamani
Copy link

Bug Details

With v1.3.3 following query used to work with an optional parameter

query getUser($isVerified: Boolean) {
  users(where: {
    isVerified: {
      _eq: $isVerified
    }
  }) {
    id
    email
    isVerified
  }
}

In the above query $isVerified is optional when it was not was passed it returned all the fields which is the expected behaviour.

After migrating to Hasura cloud version v2.0.0-alpha.2.cloud.2
The above query gives the following error when $isVerified is not passed.

{
  "errors": [
    {
      "extensions": {
        "path": "$.selectionSet.auth_users.args.where.isVerified._eq",
        "code": "validation-failed"
      },
      "message": "expected a boolean for type \"Boolean\", but found null"
    }
  ]
}
@tirumaraiselvan
Copy link
Contributor

This is a breaking change in v2.0. From the release notes, behaviour changes section:

(Fixes behaviour of pathological dangerous case) The semantics of explicit null values in where filters have changed according to the discussion in issue #704: an explicit null value in a comparison input object will be treated as an error rather than resulting in the expression being evaluated to True. For instance: delete_users(where: {id: {_eq: $userId}}) { name } will yield an error if $userId is null instead of deleting all users.

@lakhansamani
Copy link
Author

@tirumaraiselvan thanks for pointing out the changes.
As I see now one will have to create multiple queries for any such optional parameter, or is there a better alternative?

@lakhansamani
Copy link
Author

Was able to resolve this, thanks @tirumaraiselvan

@bobymicroby
Copy link

Hi @lakhansamani , what was your approach to the problem ?

@lakhansamani
Copy link
Author

lakhansamani commented Mar 21, 2021

@bobymicroby I used template literal here is an example

export const getQuery = (data) => {
   let query = `
    firstName: $firstName
    lastName: $lastName
    gender: $gender
    nationality: $nationality
    phone: $phone
  `;

  // for optional arguments
  if (data.isVerified) {
    query += `\n isVerified: $isVerified`
  }

  // final mutation
  const updateUser = gql`
     mutation updateProfile(
      $id: uuid!
      $firstName: String!
      $lastName: String!
      $gender: String!
      $nationality: String!
      $phone: String!
      $isVerified: Boolean
    ) {
      update_auth_users(
        where: { id: { _eq: $id } }
        _set: {
          ${query}
        }
      ) {
        affected_rows
      }
    }
  `;
   `
}

@bobymicroby
Copy link

@lakhansamani Thanks for the answer!

Since we are using TS & GraphQL generator for type safety, I will probably make $where: user_bool_exp! clause conditional instead of generating different queries.

Thanks again!

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

No branches or pull requests

3 participants