Fetching data using GROQ in Sanity

How to fetch documents using GROQ (Graph-Relational Object Queries) in Sanity

GROQ

GROQ or Graph Relational Object Query is the query language used for fetching data from Sanity datasets.

It is close to Graphql but has different syntax. Unlike REST API GROQ can fetch only selected part of a document which indeed allow us to minimize the site load.

The Query

We can query the data using the following format.

 const data = await client.fetch(`*[_type == "post"]);
 

The above query will fetch all documents of type post. For a list of fields use curly braces as follows.

 `*[_type == "post"]{title,_createdAt,slug,summary,featured_image}` 

Where clause can be placed after &&

`*[_type == "post" && slug.current =='${params.slug}']
sanity
GROQ
how-to
Javascript
query

Comments

TestInvalid Date

REact

Write your comment