ContentTypeQuery
ContentTypeQuery
ContentTypeQuery lists the content types of a stack. Use it to
- list every content type and its schema, one page at a time
- filter content types by UID, title, description or date
- add the total count or the schema of each Global field to the response
ContentTypeQuery adopts BaseQuery, so it also has the paging, sorting and raw-query methods that the BaseQuery page documents. The Method Index below links them.
Instance State
The SDK creates a ContentTypeQuery only through the query() method of ContentType. Every instance reaches the same methods.
- Call stack.contentType().query() to work across all content types.
- Call stack.contentType(uid: "CONTENT_TYPE_UID").query() to start with a uid equals condition, so find returns at most that content type.
A new query starts with no parameters and no headers. It takes its cachePolicy from the Stack, not from the ContentType you called query() on.
Class-Level Notes
Conditions and parameters stay on the query.
- where(queryableCodingKey:_:) and the BaseQuery filters store conditions in queryParameter. find sends them as one JSON query parameter.
- include(params:) and the BaseQuery paging methods store values in parameters. find sends them as separate query parameters, plus the environment of the stack.
- A second condition on the same key replaces the first. No method removes a stored value. Call query() again for a clean query.
Pagination.
- The SDK sets no limit, so the API applies its own page size. find returns one page and does not fetch the next one.
- Combine skip(theFirst:) and limit(to:) to read the list one page at a time. Add .count through include(params:) to learn the total.
Capability Matrix
| Capability | Supported | Notes / Alternative |
|---|---|---|
| Filter on a key outside QueryableCodingKey | Yes | Use the where(valueAtKeyPath:_:) method with the field path |
| Add metadata to the response | No | ContentTypeQuery has no metadata option |
| Add the _branch key to the response | No dedicated option | Send include_branch through the addURIParam(with:value:) method |
Class-Level Properties
The table below lists the properties of ContentTypeQuery. They come from BaseQuery, and the methods of the class read and write them, so you rarely set them directly.
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| stack | Stack | Read-write | The stack of the ContentType | Stack that sends the request. |
| parameters | [String: Any] | Read-write | [:] | Query parameters that find sends, such as include_count. |
| queryParameter | [String: Any] | Read-write | [:] | Conditions that find sends as the JSON query parameter. |
| headers | [String: String] | Read-write | [:] | Extra headers that find sends. |
| cachePolicy | CachePolicy | Read-write | The cachePolicy of the Stack | Where find reads the result from. |
ContentTypeQuery.ResourceType is ContentTypeModel. The ContentType page describes the model.
ContentTypeQuery.Include
ContentTypeQuery.Include is an OptionSet that you pass to include(params:). Combine values in an array literal, such as [.count, .globalFields]. The table below lists its values.
| Value | Raw value | Query parameter find sends | Effect on the response |
|---|---|---|---|
| .count | 1 | include_count=true | Adds count, the total number of matching content types. |
| .globalFields | 2 | include_global_field_schema=true | Adds the schema of each Global field to the content type schema. |
| .all | 3 | Both parameters above | Combines both values. |
Method Index
Methods of ContentTypeQuery. Available on every ContentTypeQuery.
| Method Name | Returns | Description |
|---|---|---|
| where(queryableCodingKey:_:) | The same content type query, for chaining. | Adds a condition on a typed content type key. |
| include(params:) | The same content type query, for chaining. | Adds the count or the Global field schemas to the response. |
Methods from BaseQuery. Available on every ContentTypeQuery. The BaseQuery page documents each one.
| Method Name | Returns | Description |
|---|---|---|
| find(_:) | Nothing. The completion handler receives the result. | Sends the query and returns one page of content types. |
| where(valueAtKeyPath:_:) | The same query, for chaining. | Adds a condition on any field path. |
| skip(theFirst:) | The same query, for chaining. | Skips a number of content types for pagination. |
| limit(to:) | The same query, for chaining. | Caps the number of content types returned. |
| orderByAscending(keyPath:) | The same query, for chaining. | Sorts the content types in ascending order of a field. |
| orderByDecending(keyPath:) | The same query, for chaining. | Sorts the content types in descending order of a field. |
| addURIParam(with:value:) | The same query, for chaining. | Adds one query parameter to the request. |
| addURIParam(dictionary:) | The same query, for chaining. | Adds several query parameters to the request. |
| addQuery(with:value:) | The same query, for chaining. | Adds one raw condition to the JSON query. |
| addQuery(dictionary:) | The same query, for chaining. | Adds several raw conditions to the JSON query. |
| addValue(_:forHTTPHeaderField:) | The same query, for chaining. | Adds a header to the request. |
Class-Level Snippet
The snippet below lists every content type in title order, with the schemas of its Global fields and the total count.
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType().query()
.include(params: .all)
.orderByAscending(keyPath: "title")
.find { (result: Result<ContentstackResponse<ContentTypeModel>, Error>, response: ResponseType) in
switch result {
case .success(let contentTypes):
print("\(contentTypes.count ?? 0) content types")
for contentType in contentTypes.items {
print(contentType.uid, contentType.schema.count)
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Methods
Each method of the ContentTypeQuery class has its own section below, with its parameters, validation rules, behavior and examples.
include(params:)
include(params:) asks the API to add the total count, the schema of each Global field, or both, to the response.
| Name | Type | Description |
|---|---|---|
| params (required) | ContentTypeQuery.Include | Options to add, such as [.count, .globalFields]. Each option sends one parameter. |
Returns: ContentTypeQuery. The same content type query, for chaining.
Validation
- There is no client-side validation. An empty set, [], stores nothing.
- Errors that do occur come from the find(_:) call that follows, not from include(params:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. include(params:) stores one parameter with the value true in parameters for each option in the set and makes no request.
- .count sends include_count, and the API adds the total number of content types. Read it from the count property of the ContentstackResponse.
- .globalFields sends include_global_field_schema, and the API returns the schema of each Global field along with the content type schema. .all sends both. See Get all content types.
- A later call adds options. It never removes an option that an earlier call stored.
Example
Basic usage: list content types with a count
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType().query()
.include(params: .count)
.find { (result: Result<ContentstackResponse<ContentTypeModel>, Error>, response: ResponseType) in
switch result {
case .success(let contentTypes):
print("Total: \(contentTypes.count ?? 0)")
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Both options: add the count and the Global field schemas
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType().query()
.include(params: [.count, .globalFields])
.find { (result: Result<ContentstackResponse<ContentTypeModel>, Error>, response: ResponseType) in
guard case .success(let contentTypes) = result else { return }
for contentType in contentTypes.items {
print(contentType.title, contentType.schema.count)
}
}where(queryableCodingKey:_:)
where(queryableCodingKey:_:) adds a condition on one typed content type key, so find returns only the content types that match it.
| Name | Type | Description |
|---|---|---|
| queryableCodingKey (required) | ContentTypeModel.QueryableCodingKey | Content type key to test, such as .title. Names the JSON key. |
| operation (required) | Query.Operation | Comparison and value, such as .equals("Blog"). Sets the match rule. |
Returns: ContentTypeQuery. The same content type query, for chaining.
Validation
- There is no client-side validation. The type system limits the key to the five cases of ContentTypeModel.QueryableCodingKey.
- Errors that do occur come from the find(_:) call that follows, not from where(queryableCodingKey:_:).
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. where(queryableCodingKey:_:) passes the raw value of the key to the where(valueAtKeyPath:_:) method of BaseQuery, which stores the condition in queryParameter.
- The keys are .uid, .title, .description, .createdAt (created_at) and .updatedAt (updated_at). The ContentType page lists them.
- The Query page lists the cases of Query.Operation, such as .equals, .notEquals, .includes, .matches and .isGreaterThan.
- A second condition on the same key replaces the first. Conditions on different keys combine, and the API returns the content types that match all of them.
- find(_:) sends every condition as the JSON query parameter of Get all content types.
Example
Basic usage: find content types by title
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType().query()
.where(queryableCodingKey: .title, .matches("^Blog"))
.find { (result: Result<ContentstackResponse<ContentTypeModel>, Error>, response: ResponseType) in
switch result {
case .success(let contentTypes):
print(contentTypes.items.map { $0.uid })
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Several UIDs: read a known set of content types
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType().query()
.where(queryableCodingKey: .uid, .includes(["blog_post", "author"]))
.find { (result: Result<ContentstackResponse<ContentTypeModel>, Error>, response: ResponseType) in
guard case .success(let contentTypes) = result else { return }
print(contentTypes.items.map { $0.title })
}