Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/staccx/sanity-query-helper
Helper for using Sanity.io and generating GROQ
https://github.com/staccx/sanity-query-helper
headless sanity
Last synced: 13 days ago
JSON representation
Helper for using Sanity.io and generating GROQ
- Host: GitHub
- URL: https://github.com/staccx/sanity-query-helper
- Owner: staccx
- Created: 2018-06-07T08:19:54.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-09T10:51:37.000Z (almost 2 years ago)
- Last Synced: 2024-10-14T04:36:15.906Z (24 days ago)
- Topics: headless, sanity
- Language: JavaScript
- Homepage:
- Size: 376 KB
- Stars: 21
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-sanity - Sanity Query Helper - Helper for using Sanity.io and generating GROQ (Related projects / Studio Inspiration)
README
# Sanity Query Helper
[![CircleCI](https://circleci.com/gh/staccx/sanityQueryHelper.svg?style=svg)](https://circleci.com/gh/staccx/sanityQueryHelper)
[![Coverage Status](https://coveralls.io/repos/github/staccx/sanityQueryHelper/badge.svg?branch=master)](https://coveralls.io/github/staccx/sanityQueryHelper?branch=master)## Description
GROQ can be hard to grok. While GROQ is a really powerful tool, it can be a bit overkill for your most common Sanity operations. To make it easier to query sanity for your content, sanity-query-helper provides an API which might be easier to understand.## Install
`yarn add sanity-query-helper`
## Usage
Immutable. All functions are chainable (except for send) and return a new helper.
```js
import SanityQueryHelper from "sanity-query-helper"const sanityHelper = new SanityQueryHelper({
sanityOptions: {
projectId: "project-id",
dataset: "myDataSet",
useCdn: true
}
})// Create query
// Filters
const filter = sanityHelper
.ofType("post")
.withFilter("releaseDate") // .compare("releaseDate", SanityQueryHelper.comparisons.greaterOrEqualTo, 1979)
.greaterOrEqualTo(1979)
.send()
.then(useMyData) // 👈 response from sanity// Picks aka Projections
filter
.pick("title")
.send()
.then(useMyData) // 👈 response with projection// Select
const select = projection
.select(0, 10)
.send()
.then(data => doStuff(data)) // 👈 response will have 10 first posts (if that many exists)// Order by
select
.orderBy(releaseYear)
.send(orderedData => use(orderedData))
```