QueryOn

public final class QueryOn<EntryType> : EntryQuery where EntryType : EntryDecodable, EntryType : FieldKeysQueryable

A concrete implementation of EntryQuery which requires that a model class conforming to EntryType be passed in as a generic parameter. The “content_type” parameter of the query will be set using the contentTypeID of the generic parameter conforming to EntryDecodable. You must also implement ResourceQueryable in order to utilize these generic queries.

  • The parameters dictionary that are converted to URLComponents (HTTP parameters/arguments) on the HTTP URL. Useful for debugging.

    Declaration

    Swift

    public var parameters: [String : String]
  • Designated initializer for QueryOn<EntryType>.

    Declaration

    Swift

    public init()
  • Static method for creating a new QueryOn with an operation. This variation for initialization guarantees correct query construction by utilizing the associated FieldKeys type required by ResourceQueryable.

    Example usage:

    let query = QueryOn<Cat>.where(field: .color, .equals("gray"))
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters

    Declaration

    Swift

    public static func `where`(field fieldsKey: EntryType.FieldKeys, _ operation: Query.Operation) -> QueryOn<EntryType>

    Parameters

    field

    The member of the FieldKeys type associated with your type conforming to EntryDecodable & ResourceQueryable that you are performing your select operation against.

    operation

    The query operation used in the query.

    Return Value

    A newly initialized QueryOn query.

  • Instance method for appending a query operation to the receiving QueryOn. This variation for initialization guarantees correct query contruction by utilizing the associated FieldKeys type required by ResourceQueryable.

    Example usage:

    let query = QueryOn<Cat>().where(field: .color, .equals("gray"))
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters

    Declaration

    Swift

    @discardableResult
    public func `where`(field fieldsKey: EntryType.FieldKeys, _ operation: Query.Operation) -> QueryOn<EntryType>

    Parameters

    fieldsKey

    The member of your FieldKeys type associated with your type conforming to EntryDecodable & ResourceQueryable that you are performing your select operation against.

    operation

    The query operation used in the query.

    Return Value

    A reference to the receiving query to enable chaining.

  • Static method for creating a new QueryOn with a select operation: an operation in which only the fields specified in the fieldNames property will be returned in the JSON response. This variation for initializing guarantees correct query contruction by utilizing the Fields CodingKeys required by ResourceQueryable. The “sys” dictionary is always requested by the SDK. Note that if you are using the select operator with an instance QueryOn<EntryType> that your model types must have optional types for properties that you are omitting in the response (by not including them in your selections array). If you are not using the QueryOn type while querying entries, make sure to specify the content type id. Example usage:

    let query = QueryOn<Cat>.select(fieldsNamed: [.bestFriend, .color, .name])
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/select-operator

    Declaration

    Swift

    public static func select(fieldsNamed fieldsKeys: [EntryType.FieldKeys]) -> QueryOn<EntryType>

    Parameters

    fieldsKeys

    An array of FieldKeys associated with the genery EntryType that you are performing your select operation against.

    Return Value

    A newly initialized QueryOn query.

  • Instance method for creating a new QueryOn with a select operation: an operation in which only the fields specified in the fieldNames property will be returned in the JSON response. This variation for initializing guarantees correct query contruction by utilizing the Fields type associated with your type conforming to ResourceQueryable. The “sys” dictionary is always requested by the SDK. Note that if you are using the select operator with an instance QueryOn<EntryType> that your model types must have optional types for properties that you are omitting in the response (by not including them in your selections array). If you are not using the QueryOn type while querying entries, make sure to specify the content type id.

    Example usage:

    let query = QueryOn<Cat>().select(fieldsNamed: [.bestFriend, .color, .name])
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/select-operator

    Declaration

    Swift

    @discardableResult
    public func select(fieldsNamed fieldsKeys: [EntryType.FieldKeys]) -> QueryOn<EntryType>

    Parameters

    fieldsKeys

    An array of FieldKeys associated with the genery EntryType that you are performing your select operation against.

    Return Value

    A reference to the receiving query to enable chaining.

  • Static method for performing searches where linked entries or assets at the specified linking field match the filtering query. For instance, if you want to query all entries of type “cat” where the “bestFriend” field links to cats with name matching “Happy Cat” the code would look like the following:

    let linkQuery = LinkQuery<Cat>.where(field: .name, .matches("Happy Cat"))
    let query = QueryOn<Cat>(whereLinkAtField: .bestFriend, matches: linkQuery)
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references

    Declaration

    Swift

    public static func `where`<LinkType>(linkAtField fieldsKey: EntryType.FieldKeys,
                                         matches linkQuery: LinkQuery<LinkType>) -> QueryOn<EntryType>

    Parameters

    fieldsKey

    The FieldKey for the property which contains a link to another entry.

    linkQuery

    The filter query applied to the linked objects which are being searched.

    Return Value

    A newly initialized QueryOn query.

  • Instance method for for performing searches where Linked objects at the specified linking field match the filtering query. For instance, if you want to query all entries of type “cat” where the “bestFriend” field links to cats with name matching “Happy Cat” the code would look like the following:

    let linkQuery = LinkQuery<Cat>.where(field: .name, .matches("Happy Cat"))
    let query = QueryOn<Cat>(whereLinkAtField: .bestFriend, matches: linkQuery)///
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references

    Declaration

    Swift

    @discardableResult
    public func `where`<LinkType>(linkAtField fieldsKey: EntryType.FieldKeys,
                                  matches linkQuery: LinkQuery<LinkType>) -> QueryOn<EntryType>

    Parameters

    fieldsKey

    The FieldKey for the property which contains a link to another entry or asset.

    linkQuery

    The filter query applied to the linked objects which are being searched.

    Return Value

    A reference to the receiving query to enable chaining.

  • Static method fore creating a query requiring that a specific field of an entry contains a reference to another specific entry.

    let query = QueryOn<Cat>(whereLinkAtField: .bestFriend, hasTargetId: "nyancat")
    
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references

    Declaration

    Swift

    public static func `where`(linkAtField fieldsKey: EntryType.FieldKeys,
                               hasTargetId targetId: String) -> QueryOn<EntryType>

    Parameters

    fieldsKey

    The FieldKey of the property which contains a link to another Entry.

    targetId

    The identifier of the entry or asset being linked to at the specified linking field.

    Return Value

    A newly initialized QueryOn query.

  • Instance method creating a query that requires that an specific field of an entry holds a reference to another specific entry.

    let query = QueryOn<Cat>(whereLinkAtField: .bestFriend, hasTargetId: "nyancat")
    
    client.fetchArray(of: Cat.self, matching: query) { (result: Result<ArrayResponse<Cat>>) in
        switch result {
        case .success(let arrayResponse):
            let cats = arrayResponse.items
            // Do stuff with cats.
        case .failure(let error):
            print(error)
        }
    }
    

    See: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references

    Declaration

    Swift

    @discardableResult
    public func `where`(linkAtField fieldsKey: EntryType.FieldKeys,
                        hasTargetId targetId: String) -> QueryOn<EntryType>

    Parameters

    fieldsKey

    The FieldKey of the property which contains a link to another Entry.

    targetId

    The identifier of the entry or asset being linked to at the specified linking field.

    Return Value

    A reference to the receiving query to enable chaining.