Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groq-builder projections on field #269

Open
2 tasks done
heggemsnes opened this issue Feb 17, 2024 · 2 comments
Open
2 tasks done

groq-builder projections on field #269

heggemsnes opened this issue Feb 17, 2024 · 2 comments
Labels
bug Something isn't working groq-builder Related to the newer `groq-builder` package invalid This doesn't seem right

Comments

@heggemsnes
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Question

Hi

I have a question regarding a common pattern we have for reusing queries, check out my example repo here: https://github.com/heggemsnes/groq-builder-example

I am wondering how to create a reusable "pageBuilder" pattern. I have tried to add some examples in the repo. 

Basically I want to define a fragment like this:

const sectionsFragment = q
  .fragment<SanityValues["sections"]>()
  .project((q) => ({
    ...q.conditionalByType({
      articleSection: {
        title: true,
        // Using page here for simplicity, but this would be a reference to a article.
        // @ts-expect-error Causes same error.
        articles: q.star.filterByType("page").slice(0, 3).project({
          title: true,
          slug: "slug.current",
        }),
      },
      imageSection: {
        image: true,
      },
    }),
  }));

And use it on a projection kinda like this:

const pageWithSectionsQuery = q.star
  .filterByType("pageWithSections")
  .slice(0)
  .project({
    title: true,
    sections: q.field("sections[]").project(sectionsFragment),
  });


Would love some pointers on how this pattern might be possible as it is quite common for us i.e. for nested blocks in portable text as well.

PS: Sorry for spamming, but just got excited by this package!
@heggemsnes
Copy link
Contributor Author

I am managing to work around this currently by doing this which is not really that good looking :)

import { SectionsType, sectionsFragment } from "./../../lib/queries/sections.query";
import { q } from "@/lib/queries/groq-builder";
import { FragmentInputTypeTag, InferResultType } from "groq-builder";

// @ts-expect-error
export const pageQuery = q.star
  .filterByType("page")
  .filter("slug.current == $slug")
  .slice(0)
  .project({
    title: true,
    sections: q.raw<SectionsType[]>(`sections[]{
      ${sectionsFragment["[CONDITIONAL] [BY_TYPE]"].query}
    }`),
  });

export type PageQuery = InferResultType<typeof pageQuery>;

@scottrippey
Copy link
Member

The sample you posted does look correct! You've created a fragment with a conditional projection. This should be supported, here's the unit tests:
https://github.com/FormidableLabs/groqd/blob/main/packages/groq-builder/src/commands/fragment.test.ts#L141-L198

Can you give me a little more detail on the errors you're experiencing?

@scottrippey scottrippey added bug Something isn't working invalid This doesn't seem right groq-builder Related to the newer `groq-builder` package labels May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working groq-builder Related to the newer `groq-builder` package invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants