Sign up for Hasura Newsletter
Loading...

Query Variables

What is a variable in GraphQL context?

GraphQL has a first-class way to factor dynamic values out of the query, and pass them as a separate dictionary. These values are called variables. In our case, we are defining the object to be inserted as a mutation.

So let's define the graphql mutation to be used. In our TaskFragment.kt, add below method for adding a new Todo

private fun addTodoMutationCloud(title: String) {
// Init Query
addTodoMutation = AddTodoMutation.builder().todo(title).isPublic(false).build()
// Apollo runs query on background thread
Network.apolloClient.mutate(addTodoMutation)?.enqueue(object : ApolloCall.Callback<AddTodoMutation.Data>() {
override fun onFailure(error: ApolloException) {
Log.d("Todo", error.toString() )
}
override fun onResponse(@NotNull response: Response<AddTodoMutation.Data>) {
Log.d("Todo", response.data() )
}
})
}

What does this mutation do?

The mutation inserts into todos table with the $title variable being passed as one query variable. We are logging the result to see the response from the server.

Awesome! We have defined our first graphql mutation.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin