FormidableLabs / groqd

A schema-unaware, runtime and type-safe query builder for GROQ.
https://commerce.nearform.com/open-source/groqd
MIT License
221 stars 17 forks source link

groq-builder: Added `q.fragment` implementation #250

Closed scottrippey closed 5 months ago

scottrippey commented 6 months ago

Implemented q.fragment method for defining reusable fragments.

Examples

Defining a fragment:

const productFragment = q.fragment<SanitySchema.Product>().project(qProduct => ({
  name: true,
  price: true,
  imageUrls: qProduct.field('images[]').deref().field('url'),
}));

type ProductFragment = InferFragmentType<typeof productFragment>;
// Evaluates to: { name: string, price: number, imageUrls: Array<string> }

Consuming a fragment

You can pass to project:

q.star.filterByType('product').project(productFragment);

You can spread inside a project:

q.star.filterByType('product').project({
  ...productFragment,
  description: true,
  category: true,
});

You can compose multiple fragments by spreading.

TODO

vercel[bot] commented 6 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **groqd** | ⬜️ Ignored ([Inspect](https://vercel.com/formidable-labs/groqd/EDtKWB4ueZJS3yZ6P3zCJCSGGo6M)) | [Visit Preview](https://groqd-git-fragments-formidable-labs.vercel.app) | | Dec 22, 2023 9:20pm |