Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering Contentful Query on Linked Objects

Tags:

contentful

I'm attempting to utilize Contentful on a current project of mine and I'm trying to understand how to filter my query results based on a field in a linked object.

My top level object contains a Link defined as such:

  "name": "Service_Description",
  "fields": [
{
  "name": "Header",
  "id": "header",
  "type": "Link",
  "linkType": "Entry",
  "required": true,
  "validations": [
    {
      "linkContentType": [
        "offerGeneral"
      ]
    }
  ],
  "localized": false,
  "disabled": false,
  "omitted": false
},

This "header" field links to another content type that has this definition:

  "fields": [
{
  "name": "General",
  "id": "general",
  "type": "Link",
  "linkType": "Entry",
  "required": true,
  "validations": [
    {
      "linkContentType": [
        "genericGeneral"
      ]
    }
  ],
  "localized": false,
  "disabled": false,
  "omitted": false
},

which then links to the lowest level:

"fields": [{
  "name": "TagList",
  "id": "tagList",
  "type": "Array",
  "items": {
    "type": "Link",
    "linkType": "Entry",
    "validations": [
      {
        "linkContentType": [
          "tag"
        ]
      }
    ]
  },
  "validations": []
}

where tagList is an array of tags this piece of content may have.

I want to be able to run a query from the top level object that says get me X number of these "Service_Description" content entries where it contains a tag from a supplied list of tags.

In PostMan, I've been running with this:

https://cdn.contentful.com/spaces/{SPACE_ID}/entries?access_token={ACCESS_TOKEN}&content_type=serviceDescription&include=3

I'm trying to add a filter something like so:

fields.header.fields.general.fields.tagList.sys.id%5Bin%5D={TAG_SYS_ID}

This is clearly incorrect, but I've been struggling with how to walk this relationship to achieve my goal. Perusing the documentation this seems to have something to do with includes, but I'm unsure of how to rectify the problem.

Any direction on how to achieve my goal or if this is possible?

like image 696
Khepri Avatar asked Jun 22 '16 23:06

Khepri


2 Answers

This is now possible, something I believe was solved for in the API based on requests for this functionality. You can see the thread here.

This gist of it is that you have to query on the entries that have linked entries and then include the contentType for those linked entries in the query like so:

contentfulClient.getEntries({
  'content_type': 'location',
  'fields.market.fields.marketName': 'New York',
  'fields.market.sys.contentType.sys.id': 'marketRegion'
})
like image 83
Joshua Tuscan Avatar answered Oct 30 '22 14:10

Joshua Tuscan


Unfortunately what you are requesting is not currently possible in Contentful.

We were facing a very similar issue with nested/referenced content types and support said it wasn't possible.

We ended up writing a very complicated system that allowed us to do what you want. Essentially doing a full text search for the referenced content and then querying all of the parents entries. We then matched the relationships by iterating over the parents to find the relationship.

Sorry it couldn't be easier. Hopefully the devs work on something that improve this complication. We have brought this to their attention.

like image 35
Matt Avatar answered Oct 30 '22 14:10

Matt