The @Mutation property wrapper lets you use a GraphQL mutation to make changes on the server, and tracks the state of the request in your view.

Example

Unlike queries and fragments, you don't define mutations in the same file as the view that uses them. Mutations are not specific to a particular view, so they are defined in their own files.

// ChangeTodoStatus.swift

import Relay

private let mutation = graphql("""
mutation ChangeTodoStatusMutation($input: ChangeTodoStatusInput!) {
  changeTodoStatus(input: $input) {
    todo {
      id
      complete
    }
  }
}
""")

Once the mutation is defined and the Relay compiler has generated the types for the mutation, you can use @Mutation to use the mutation from a SwiftUI view.

// ToDoItem.swift

import SwiftUI
import RelaySwiftUI

private let itemFragment = graphql("""
fragment ToDoItem_item on Item {
	id
  text
	complete
}
""")

struct ToDoItem: View {
  @Fragment<ToDoItem_item> var item
  @Mutation<ChangeTodoStatusMutation> var changeStatus

	var body: some View {
		if let item = item {
			HStack {
				Button {
					changeStatus.commit(variables: .init(
						input: .init(id: item.id, complete: !item.complete)
					))
				} label: {
					Image(systemName: item.complete ? "checkmark.square" : "square")
				}
				.disabled(changeStatus.isInFlight)

				Text("\\(item.text)")
			}
		}
	}
}

Parameters

Property value

The @Mutation property will be a read-only Mutator structure with the following API:

The commit function

The commit function takes several possible parameters, most of them optional:

See Updater functions for more information about how to use the optimisticUpdater and updater parameters.