sanitygroq

Filter query result by field value inside array of objects [Sanity.io & GROQ]


I'm trying to find a product variant inside my list of products(on sanity.io using GROQ), to do so, I have the sku of the variant that I want.

The query I'm using is *[_type == "product" && variants[].sku.current =="kit-kat-wasabi-5" ] But this query returns an empty array. I'm sure that the sku is correct because if I leave the filter aside, and fetch all I can find it. I tried replacing the "==" with match, but the result is the same.

my schemas are

procuct

export default {
  name: 'product',
  title: 'Product',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'Inner Title',
      type: 'string'
    },
    {
      title: 'SKU',
      name: 'sku',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 96
      },
      validation: Rule => Rule.required()
    },
    {
      name: 'titleWebsite',
      title: 'Title Website',
      type: 'localeString'
    },
    {
      name: 'active',
      title: 'Active',
      type: 'boolean'
    },
    {
      name: 'mainImage',
      title: 'Imagem',
      type:"image"
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 96
      }
    },
    {
      title: 'Base Price',
      name: 'basePrice',
      type: 'localeCurrency'
    },
    {
      title: 'Quantidade',
      name: 'qty',
      type: 'number'
    },
   /* {
      title: 'Default variant',
      name: 'defaultProductVariant',
      type: 'productVariant'
    },*/
    {
      title: 'Variants',
      name: 'variants',
      type: 'array',
      of: [
        {
          title: 'Variant',
          type: 'productVariant'
        }
      ]
    },
    {
      title: 'Tags',
      name: 'tags',
      type: 'array',
      of: [
        {
          type: 'string'
        }
      ],
      options: {
        layout: 'tags'
      }
    },
    {
      name: 'vendor',
      title: 'Vendor',
      type: 'reference',
      to: {type: 'vendor'}
    },
    {
      name: 'blurb',
      title: 'Blurb',
      type: 'localeString'
    },

    {
      name: 'categories',
      title: 'Categories',
      type: 'array',
      of: [
        {
          type: 'reference',
          to: {type: 'category'}
        }
      ]
    },
    {
      name: 'body',
      title: 'Body',
      type: 'localeBlockContent'
    }
  ],

  preview: {
    select: {
      title: 'title',
      manufactor: 'manufactor.title',
      media: 'mainImage'
    }
  }
}

And productVariant

export default {
  title: 'Product variant',
  name: 'productVariant',
  type: 'object',
  fields: [
    {
      title: 'Title',
      name: 'title',
      type: 'string'
    },
    {
      title: 'Title Website',
      name: 'titleWebsite',
      type: 'localeString'
    },
    {
      title: 'Weight in grams',
      name: 'grams',
      type: 'number'
    },
    {
      title: 'Price',
      name: 'price',
      type: 'localeCurrency'
    },
    {
      title: 'SKU',
      name: 'sku',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 96
      },
      validation: Rule => Rule.required()
    },
    {
      title: 'Taxable',
      name: 'taxable',
      type: 'boolean'
    },
    {
      name: 'blurb',
      title: 'Blurb',
      type: 'localeString'
    },
    {
      name: 'images',
      title: 'Images',
      type: 'array',
      of: [
        {
          type: 'image',
          options: {
            hotspot: true
          }
        }
      ]
    },
    {
      title: 'Quantidade',
      name: 'qty',
      type: 'number'
    },
    {
      title: 'Bar code',
      name: 'barcode',
      type: 'barcode'
    }
  ]
}


Solution

  • This is a known bug in GROQ when traversing arrays. This will introduce breaking changes if it were to be fixed in GROQ v1. It will therefore be fixed in GROQ v2.

    Here is a bug report explaining the issue: https://github.com/sanity-io/sanity/issues/1557. You can show your interest in this problem here.

    There's a working draft for version two here: https://github.com/sanity-io/groq.

    Regarding your schema, I would consider to change the type of the sku field to be something else, for example a string, and create a new field for the slug which has its source property set to the SKU to be automatically be generated by that field. Documentation for that can be found here: https://www.sanity.io/docs/slug-type.