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
7
votes
1 answer

Can't make search query work in Sanity CMS

I am trying to perform a very basic search query with Sanity CMS. This is how the person schema I've created looks like: export default { title: "Person", name: "person", type: "document", fields: [ { title: "Name", name:…
user1449456
  • 502
  • 1
  • 5
  • 19
6
votes
1 answer

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

I have a dataset with posts, which might have an array of categories. How do I make a GROQ query that selects all posts with a category with the title "Page"? I would think that I could do something like this: *[_type == 'post' &&…
dotnetCarpenter
  • 10,019
  • 6
  • 32
  • 54
6
votes
1 answer

How to fill arrays of references in a single query

I have a schema type Page which has an array of blocks: { title: 'Page', name: 'page', type: 'document', fields: [ ... { title: 'Blocks', name: 'blocks', type: 'array', of: [ {type: 'tileGrid'}, …
Slem
  • 108
  • 1
  • 6
5
votes
2 answers

Resolve reference in Block Content in Sanity.io

I have the Content Block in Sanity like so: export default { title: "Block Content", name: "blockContent", type: "array", of: [ /// stuff here { title: "Book", type: "reference", to: [{ type: "book" }], …
3
votes
1 answer

It it possible to truncate text in a groq query?

I'd like to create an excerpt from a (portable) text field. Is this something that is possible? I know I can get the text value back using pt::text(body) and I can get values such as length from that. Is there any way to cut the text after n…
Designer023
  • 1,902
  • 1
  • 26
  • 43
3
votes
2 answers

How to get just the most recent of all documents

In sanity studio you get a nice list of the most recent version of all your documents. If there is a draft you get that, if not, you get the published one. I need the same list for a few filters and scripts. The following groq does the job but is…
cfm
  • 156
  • 1
  • 2
  • 12
2
votes
0 answers

POST request to Sanity CMS using Next.js API & next-connect resulting in 500 ERROR

I am having trouble making a POST request of user info on a register form to the Sanity CMS.I am using Next.js & next-connect. I am getting a 500 error when making the request. Here is a snippet of code from the register.js front end page: const…
Calathea
  • 21
  • 2
2
votes
2 answers

Sanity GROQ: How do i get drafts for the array of references in a document?

I have a document type page which contains an array of blocks that are the references to other object types. When i fetch the data for page document, it returns draft data for document but not for the references that are in array. I have written…
2
votes
1 answer

How do I query referenced document fields within an array of objects in Sanity with GROQ?

I have a document which has an array of objects of which one of the fields is a reference to another document. The following query returns the referenced document _id and _type only, and I need other fields from those documents. // GROQ…
Anthony
  • 317
  • 1
  • 5
  • 23
2
votes
4 answers

How to filter Sanity posts by category title?

Here what I've done in vision *[_type == "post" && categories == SOCIAL ]{ _id, title } It returned No documents found in dataset production that match query: *[_type == "post" && categories == SOCIAL ]{ _id, title }
2
votes
2 answers

Sanity.io GROQ query for array of objects

I'm learning to code and now I am on the stage of a small pet project with Sanity as a CMS. Long story short, making an API I'm trying to fetch cocktails data with votes for the cocktails. The votes are stored within persons who voted: GROQ…
SirMatters
  • 21
  • 1
  • 3
2
votes
1 answer

Is it possible to query document schema metadata in Sanity/GROQ?

I have a simple singleton document schema defined in my Sanity/NextJS project, to model my "Colophon" page (richText is a custom block field type): export default { title: 'Colophon', name: 'colophon', type: 'document', …
BigglesZX
  • 958
  • 1
  • 12
  • 27
2
votes
2 answers

How to define NOT is sanity.io structure builder?

I'm trying to filter all products without categories (which is array of references) and can't find how to do it. S.listItem() .title('Without category') .id('productsWithoutCategories') .child( S.documentList() .title('Without…
2
votes
1 answer

Order by random in Sanity.io GROQ query not working?

Sanity.io is awesome! But as I'm just beginning, I cannot find some things yet I know in e.g. MySQL. According to the GROQ cheat sheet I should be able to randomly sort and slice (LIMIT + OFFSET in MySQL) by giving this query: // BEWARE! This…
Jos
  • 1,387
  • 1
  • 13
  • 27
2
votes
1 answer

Is it possible to do geospatial queries in sanity.io?

I found out that sanity supports Geopoint type, but I could not find any information if it's possible to do any filter operation on this type. Are geospatial queries possible?
1
2 3 4 5 6