0

I am trying to update a table in hasura. I have written the below query to update a row in users table where id(primary key) is equal to the passed id from the variable. If the id matches then update the field account_id which is nullable and unique inside hasura.

const query = `mutation updateAccountId($id: String!, $account_id: String) {
  update_users(where: {id: {_eq: $id}}, _set: {account_id: $account_id}){
    affected_rows
  }
}`;

fetch("https://xyz.hasura.app/v1/graphql", {
  method: "post",
  headers: {
    "content-type": "application/json",
    Authorization: `Bearer ${token}`,
  },
  body: JSON.stringify({
    query,
    variables: {
      id: "P9gc6WanL9YAy7JdD6pEbcfULYt2",
      account_id: "3afc4fds2ds4",
    },
  }),
}).then((response) => {
  console.log(
    response,
  );
});

I'm getting the below error after trying a few times. Don't know if this is a graphql syntax error or something related to hasura.

{
    "errors": [
        {
            "extensions": {
                "code": "validation-failed",
                "path": "$.selectionSet.update_users"
            },
            "message": "'update_users' has no argument named '_set'"
        }
    ]
}
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
mused
  • 79
  • 6
  • GraphQL syntax is fine. Check the parameters of `update_users` - it doesn't look like there's a `_set`. Best to verify your queries in the playground/graphiql before committing them to code. – Michel Floyd Dec 29 '22 at 18:15
  • I tested the query inside hasura graphiql console now, works fine there. But when I'm trying it from the postman or directly from the code it gives the error. – mused Dec 30 '22 at 05:00

2 Answers2

0

Solved. The problem was I didn't enable the permission to update the users table inside hasura that's why I was getting that error.

mused
  • 79
  • 6
0

You need to grant privileges to the particular role, who is hitting the query. You can do that by going to the Data Tab on the Header, and then going inside the corresponding table. Then select Permission and edit permissions in the Update column.

Reference

You will have row and column update permissions there, where you can select without any checks or apply a custom check as per your requirements.

CRUX - In column update permissions make sure to toggle the columns, which you wish to update, or are trying to update through GraphQL query.