✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Is it Possible to Perform a Slice based on a Value Received in the Same Query?

8 replies
Last updated: Aug 28, 2020
Hi yall,
Can anyone help me with a groq-query? I don't even know if this is possible, but I want to perform a slice based on a number that i recieve in the same query.
I guess this might be a problem as I guess the inner query is performed first?
(code in thread as the query is quite big)
Aug 23, 2020, 1:57 PM

*[_type == 'page'] {
  ...,
  sections[] {
    _type == "newsList" => {
  		...,
    	numberOfNews,
  		"newsItems": *[_type == "newsItem"] | order(publishedDate desc) [0..^.numberOfNews] {
        ...,
        primaryImage {
        	...,
        	asset->
      	},
				seoMetadata {
          ...,
          image {
          	asset->
        	}
        }
      }
		}
  }
}
Aug 23, 2020, 1:57 PM
The ^.numberOfNews just gives me
Subscript ranges must have integer endpoints

Aug 23, 2020, 1:58 PM
Oh, AFAIK, the query itself is static and parsed once.
Aug 28, 2020, 8:30 AM
And therefore this won't be possible ATM.
Aug 28, 2020, 8:31 AM
ahh okok
Aug 28, 2020, 8:34 AM
bummer, 2 seperate requests it is then 🙂
Aug 28, 2020, 8:35 AM
Thanks for your input!
Aug 28, 2020, 8:35 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?