directus

Deep filtering nested relations Directus


How do I use deep in nested relations?

deep: {
    translations: { _filter: { languages_code: 'nl-NL' } },
    variants: { _filter: { translations: { languages_code: 'nl-NL' } } }, // This does not work
}

The object that I try to filter looks as this

I am trying to filter out translations inside the variants aswell. But the above query does not work.

{
    // WORKS: translations: { _filter: { languages_code: locals.language } }
    translations: [
        {
            id: 7,
            products_id: 1,
            languages_code: 'nl-NL', // locals.language = nl-NL
            title: '...',
            description: '...',
            slug: '...',
            variantTitle: null,
        },
    ],
    // DOES NOT WORK: variants: { _filter: { translations: { languages_code: locals.language } } } 
    // I guess it does not work since translations is an array inside variants
    // which is also an array
    variants: [
        {
            id: 1,
            product: 1,
            translations: [
                {
                    id: 1,
                    product_variants_id: 1,
                    languages_code: 'nl-NL',
                    title: 'Aantal',
                },
                {
                    id: 2,
                    product_variants_id: 1,
                    languages_code: 'en-US', // This object should be left out of the result
                    title: 'Amount',
                },
            ],
            products: [2, 3],
        },
    ],
}

Solution

  • If I understand correctly you have a collection that contains variants that contains translations and you try to filter the list of translations when reading the root collection.

    In this case the following query should work (REST):

    deep = {
      "variants": {
        "translations": {
          "_filter": {
            "languages_code": {
              "_eq": "EN"
            }
          }
        }
      }
    }