The @Query property wrapper loads the data for a GraphQL query when a SwiftUI view is rendered.

A query will usually map one-to-one to a screen of your app. The query can fetch all of the data needed to render that screen in one roundtrip to the server, using @Fragments to provide the data that each child view needs.

Example

private let query = graphql("""
query ToDoListQuery {
  list(id: "abc") {
		items {
			text
		}
	}
}
""")

struct ToDoList: View {
	@Query<ToDoListQuery> var query

	var body: some View {
		switch query.get() {
			case .loading:
				Text("Loading...")
			case .failure(let error):
				Text("Error: \\(error.localizedDescription)")
			case .success(let data):
				List(data?.list?.items ?? [], id: \\.id) { toDoItem in
					Text("\\(toDoItem.text)")
				}
		}
	}
}

Parameters

Property value

The @Query property will have a value with a get function for retrieving the current state of the query:

func get(
	_ variables: O.Variables,
	fetchKey: Any? = nil
) -> Result

The get function returns already fetched data for the query and/or starts fetching new data, and returns a Result value describing the current state of the data.

enum Result {
	case loading
  case failure(Error)
  case success(O.Data?)
}

A Result is returned by get to indicate the state of query.

Convenience API for getting query results

The Relay compiler will generate some nicer extensions to the @Query API that make it easier to pass variables to query.get.