1.3.0 • Published 4 months ago

groqd v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

GroqD

GroqD is a GROQ query builder, designed to give the best GROQ developer experience possible.

It gives you the flexibility of GROQ, the runtime safety of Zod, and provides schema-aware auto-completion and type-checking.

GROQ is Sanity's open-source query language.

"It's a powerful and intuitive language that's easy to learn. With GROQ you can describe exactly what information your application needs, join information from several sets of documents, and stitch together a very specific response with only the exact fields you need."

Documentation

Read the official documentation at https://commerce.nearform.com/open-source/groqd/docs/

Usage Example

GroqD uses a strongly-typed chaining syntax to build queries:

import { createGroqBuilder, z } from 'groqd';
// 👇 Import Sanity types, generated by your Sanity project. See "Configuration" docs for more details.
import type * as SanityTypes from "./sanity.types.ts";

// 👇 Create a strongly-typed query builder:
const q = createGroqBuilder<{
  schemaTypes: SanityTypes.AllSanitySchemaTypes
  referenceSymbol: typeof SanityTypes.internalGroqTypeReferenceTo;
}>();

// ✨ Write strongly-typed queries, with auto-complete and runtime safety!
const productsQuery = (
  q.star
   .filterByType("products")
   .order("price desc")
   .slice(0, 10)
   .project(sub => ({
     name: z.string(),
     price: z.number(),
     slug: sub.field("slug.current", z.string()),
     imageUrls: sub.field("images[]").field("asset").deref().field("url", z.string())
   }))
);

Everything in the above query is strongly-typed, according to the Sanity schema defined in your sanity.config.ts. This means you get auto-complete for all strings, including "price desc" and "slug.current"!

The example above generates a GROQ query like this:

*[_type == "products"] | order(price desc) [0...10] {
  name,
  price,
  "slug": slug.current,
  "imageUrls": images[]->url
}

and executing the query will return strongly-typed results:

const results = await runQuery(productsQuery);
/*
 * results: Array<{ 
 *   name: string,
 *   price: number,
 *   slug: string,
 *   imageUrls: Array<string>,
 * }>
 */

Why GroqD over raw GROQ?

Sanity's CLI can generate types from your raw GROQ queries. This works well for simple use-cases.
However, GroqD aims to maximize the developer experience, improve generated types, and ensure scalability. Namely, it adds:

  • Auto-completion - write queries quickly and confidently
  • Runtime validation - ensure data integrity, catch errors early
  • Transformation - map values at runtime, like parsing dates
  • Fragments - create reusable query fragments, and compose queries from other fragments
1.2.0

4 months ago

1.0.2

5 months ago

1.0.1

6 months ago

1.0.0

6 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.1.0

4 months ago

1.3.0

4 months ago

0.15.12

12 months ago

0.15.11

1 year ago

0.15.10

2 years ago

0.15.6

2 years ago

0.15.7

2 years ago

0.15.8

2 years ago

0.15.9

2 years ago

0.15.4

2 years ago

0.15.5

2 years ago

0.15.0

2 years ago

0.15.1

2 years ago

0.15.2

2 years ago

0.15.3

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.13.0

2 years ago

0.14.0

2 years ago

0.13.1

2 years ago

0.10.0

2 years ago

0.9.0

2 years ago

0.8.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.7.0

2 years ago

0.3.0

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.6.1

2 years ago

0.5.2

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago