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.

Open Todo/TodoVC.swift and add the following code at the end of file:

githubTodo/TodoVC.swift
// Add Todo to Cloud
private func addTodoMutationCloud(title: String){
apollo.perform(mutation: AddTodoMutation(todo: title, isPublic: false)) { (result, error) in
guard let data = result?.data else { return }
let newTodo = data.insertTodos!.returning.compactMap({GetMyTodosQuery.Data.Todo(id: $0.id, title: $0.title, createdAt: $0.createdAt, isCompleted: $0.isCompleted)})
self.todos.insert(contentsOf: newTodo, at: 0)
self.filteredTodos.insert(contentsOf: newTodo, at: 0)
// Update view
DispatchQueue.main.async {
let indexPaths = (0 ..< newTodo.count).map { IndexPath(row: $0, section: 0) }
self.todoTable.beginUpdates()
self.todoTable.insertRows(at: indexPaths , with: .automatic)
self.todoTable.endUpdates()
}
}
}

What does this mutation do?

The mutation inserts into todos table with the $objects variable being passed as one todo type.

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