Questions tagged [groq]

GROQ - Graph Oriented Query Language, is a general purpose query language and can query any collection of unstructured JSON-documents without any up-front configuration.

GROQ - Graph Oriented Query Language

GROQ is developed by SANITY.io as is used to query data either hosted with SANITY or self-hosted. You usually write the queries in Content Studio and display the data in your front-end (vue.js, react, angular etc.).

Join together information from several sets of documents. Stitch together a very specific response the exact fields you need.

  • Follow references
  • Relational joins on key-value data
  • Get exactly the data structures you need by reprojecting attributes
  • Bundle multiple queries in the same request and get it all cached.
  • Query on structured block text

Everything:

*

Movies released after 1979

*[_type == 'movie' && releaseYear > 1979]

… ordered by release year and only certain fields

*[_type == 'movie' && releaseYear >= 1979]
  | order(releaseYear) {
    _id, title, releaseYear
  }

Get actors and join in arrays of the titles of the movies they played in

*[_type == "person"]{
  _id, name,
  "movies":
    *[
      _type == "movie"
      && references(^._id)
    ].title
}
78 questions
0
votes
0 answers

How do I add a inner join as a condition in GROQ?

I would like to add the data (image, slug, title) for the type "galleryHp" from this query: *[_type == "homePage"][0] { "modules": modulesFr, } this query returns a array with several modules, including the galleryHp module but with only the…
thetiti
  • 95
  • 2
  • 7
0
votes
1 answer

How to limit query to 3 most recent posts with sanity?

I am trying to query the 3 most recent article posts but not sure how to set the limit "latestArticles": *[_type == "article" && wasDeleted != true && isDraft != true] | order(publishDate desc){ title, _createdAt, },
Burger Sasha
  • 187
  • 6
  • 17
0
votes
0 answers

can you solve Simple fetch Error in Next.js?

it keeps showing me this error and I've tried to fix it but it still won't budge.. i dont think there is anything wrong with my groq query I tried changing my groq query from sanity but it still wont work
ayomicool
  • 1
  • 2
0
votes
0 answers

Is using parameters with GROQ injection safe?

After a bit of searching, I didn't find anything definitive. Does anyone know if the following example is safe from injection when using GROQ? const query = '*[_type == 'car' && maker == $maker][0]'; const result = await sanityClient.fetch(query, {…
ptf
  • 485
  • 5
  • 14
0
votes
1 answer

Getting a file URL from Sanity out of a blocks array

I'm building a website using Next JS and Sanity for the CMS. Sanity has built-in schemas for images but not for video, so a video needs to be uploaded with the File schema. The docs suggest that to get a file URL to be used on the front-end you…
James Hubert
  • 300
  • 3
  • 15
0
votes
2 answers

GROQ: Query one-to-many relationship with parameter as query input

I have a blog built in NextJS, backed by Sanity. I want to start tagging posts with tags/categories. Each post may have many categories. Category is a reference on post: defineField({ name: 'category', title: 'Category', type: 'array', …
Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106
0
votes
1 answer

GROQ query to find all posts with a specific location

I have set a sanity schema that present a restaurant and the restaurant has an array of locations that accept multiple string(cities) as shown in the picture. I am asking if there is a way to fetch data from sanity using the location array. so for…
0
votes
0 answers

Started making a clone of the delivroo app by using react-native for andorid and sanity as the backend and it shows below errors :

Invariant Violation: Failed to call into JavaScript module method AppRegistry.runApplication(). Module has not been registered as callable. Registered callable JavaScript modules (n = 11): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog,…
0
votes
1 answer

How to fetch multiple documents within a single query?

I want to fetch multiple pages within a single query. My code is causing errors const pagesQuery = groq`{ *[_type == 'homepage']{slug}, *[_type == 'faq']{slug}, *[_type == 'contact']{slug} }`
Burger Sasha
  • 187
  • 6
  • 17
0
votes
0 answers

Sanity Error: Attribute or a string key expected

I have this issue Error: Attribute or a string key expected, I am using Sanity and Nextjs. this is the dir structure pages/[character]/[team].tsx And this is my code : export async function getStaticPaths() { const characters = await…
0
votes
1 answer

Sanity GROQ query to not return nullish values

I was querying Sanity with GROQ and wanted to get response excluding nullish values in case there is no image url is available but if it is then return the image url. Doing like this: defined(image) => {image}, Wanted to get image:"png image url…
Suleman Ahmad
  • 452
  • 1
  • 5
  • 14
0
votes
1 answer

useEffect isnt fetching any data from sanity

i am using sanity with react i made a sanity schema and now i am trying to fetch the data from sanity using react. projects.jsx export default function Projects() { const {projects, setProjects} = useState([]) useEffect(() => { const…
0
votes
1 answer

How to query the next post with GROQ sanity

I want to fetch the data for the next post in order they were created at - currently nextPost returns null `*[_type == "project" && _createdAt > ^._createdAt] | order(_createdAt asc)[0]{ _createdAt, 'slug': slug.current, }`, Full Fetch…
Burger Sasha
  • 187
  • 6
  • 17
0
votes
1 answer

Sanity GROQ - Query an array of objects

I'm trying to learn Sanity and I'm stuck in trying to fetch from an array of an objects with multiple strings. Thank you so much! Schema: { name: "testArray", title: "Test Array", type: "array", of: [ { …
whites
  • 25
  • 4
0
votes
0 answers

How to change the name key of an object in array with Sanity GROQ

I have a schema with array of objects in sanity studio, when I construct the schema then in GROQ playground the name of the schema is available under the _type key, within schema I have an array of objects like…
seven
  • 1,183
  • 14
  • 33